Sale   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 60.71%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 35
c 1
b 1
f 0
dl 0
loc 121
rs 10
ccs 17
cts 28
cp 0.6071
wmc 18

13 Methods

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