Sale   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Test Coverage

Coverage 60.71%

Importance

Changes 3
Bugs 1 Features 2
Metric Value
eloc 34
c 3
b 1
f 2
dl 0
loc 119
ccs 17
cts 28
cp 0.6071
rs 10
wmc 17

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A cancelClosing() 0 3 1
A getData() 0 3 1
A getTarget() 0 3 1
A setId() 0 9 3
A jsonSerialize() 0 3 1
A getCustomer() 0 3 1
A getCloseTime() 0 3 1
A getTime() 0 3 1
A hasId() 0 3 1
A close() 0 11 3
A getId() 0 3 1
A getPlan() 0 3 1
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): void
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
121
    public function setId($id)
122
    {
123
        if ((string) $this->id === (string) $id) {
124
            return;
125
        }
126
        if ($this->hasId()) {
127
            throw new CannotReassignException('sale id');
128
        }
129
        $this->id = $id;
130
    }
131
132
    public function getData()
133
    {
134
        return $this->data;
135
    }
136
137
    public function jsonSerialize(): array
138
    {
139
        return array_filter(get_object_vars($this));
140
    }
141
142
    public function cancelClosing(): void
143
    {
144
        $this->closeTime = null;
145
    }
146
}
147