-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSale.php
executable file
·146 lines (122 loc) · 2.95 KB
/
Sale.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* PHP Billing Library
*
* @link https://github.com/hiqdev/php-billing
* @package php-billing
* @license BSD-3-Clause
* @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
*/
namespace hiqdev\php\billing\sale;
use DateTimeImmutable;
use hiqdev\php\billing\customer\CustomerInterface;
use hiqdev\php\billing\Exception\CannotReassignException;
use hiqdev\php\billing\Exception\ConstraintException;
use hiqdev\php\billing\Exception\InvariantException;
use hiqdev\php\billing\plan\PlanInterface;
use hiqdev\php\billing\target\TargetInterface;
/**
* Sale.
*
* @author Andrii Vasyliev <[email protected]>
*/
class Sale implements SaleInterface
{
/**
* @var int
*/
protected $id;
/**
* @var TargetInterface
*/
protected $target;
/**
* @var CustomerInterface
*/
protected $customer;
/**
* @var PlanInterface|null
*/
protected $plan;
/**
* @var DateTimeImmutable
*/
protected $time;
protected ?DateTimeImmutable $closeTime = null;
protected ?array $data = null;
public function __construct(
$id,
TargetInterface $target,
CustomerInterface $customer,
?PlanInterface $plan = null,
?DateTimeImmutable $time = null,
?array $data = null,
) {
$this->id = $id;
$this->target = $target;
$this->customer = $customer;
$this->plan = $plan;
$this->time = $time ?? new DateTimeImmutable();
$this->data = $data;
}
public function getId()
{
return $this->id;
}
public function getTarget()
{
return $this->target;
}
public function getCustomer()
{
return $this->customer;
}
public function getPlan()
{
return $this->plan;
}
public function getTime()
{
return $this->time;
}
public function hasId()
{
return $this->id !== null;
}
public function getCloseTime(): ?DateTimeImmutable
{
return $this->closeTime;
}
public function close(DateTimeImmutable $closeTime): void
{
if ($this->closeTime !== null) {
throw new InvariantException('Sale is already closed');
}
if ($closeTime < $this->time) {
throw new ConstraintException('Sale close time MUST be greater than open time');
}
$this->closeTime = $closeTime;
}
public function setId($id)
{
if ((string) $this->id === (string) $id) {
return;
}
if ($this->hasId()) {
throw new CannotReassignException('sale id');
}
$this->id = $id;
}
public function getData(): ?array
{
return !empty($this->data) ? $this->data : null;
}
public function jsonSerialize(): array
{
return array_filter(get_object_vars($this));
}
public function cancelClosing(): void
{
$this->closeTime = null;
}
}