Passed
Pull Request — master (#73)
by
unknown
16:36 queued 01:36
created

AbstractAction::getUsageInterval()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
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\action;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\customer\CustomerInterface;
15
use hiqdev\php\billing\Exception\CannotReassignException;
16
use hiqdev\php\billing\sale\SaleInterface;
17
use hiqdev\php\billing\target\TargetInterface;
18
use hiqdev\php\billing\type\TypeInterface;
19
use hiqdev\php\units\QuantityInterface;
20
21
/**
22
 * Chargeable Action.
23
 *
24
 * @see ActionInterface
25
 *
26
 * @author Andrii Vasyliev <[email protected]>
27
 */
28
abstract class AbstractAction implements ActionInterface
29
{
30
    /** @var int */
31
    protected $id;
32
33
    /** @var TypeInterface */
34
    protected $type;
35
36
    /** @var TargetInterface */
37
    protected $target;
38
39
    /** @var QuantityInterface */
40
    protected $quantity;
41
42
    /** @var CustomerInterface */
43
    protected $customer;
44
45
    /** @var DateTimeImmutable */
46
    protected $time;
47
48
    /** @var SaleInterface */
49
    protected $sale;
50
51
    /** @var ActionState */
52
    protected $state;
53
54
    /** @var ActionInterface */
55
    protected $parent;
56
57
    /**
58
     * @param SaleInterface $sale
59
     * @param ActionInterface $parent
60
     */
61
    public function __construct(
62 31
        $id,
63
        TypeInterface $type,
64
        TargetInterface $target,
65
        QuantityInterface $quantity,
66
        CustomerInterface $customer,
67
        DateTimeImmutable $time,
68
        SaleInterface $sale = null,
69
        ActionState $state = null,
70
        ActionInterface $parent = null
71
    ) {
72
        $this->id       = $id;
73 31
        $this->type     = $type;
74 31
        $this->target   = $target;
75 31
        $this->quantity = $quantity;
76 31
        $this->customer = $customer;
77 31
        $this->time     = $time;
78 31
        $this->sale     = $sale;
79 31
        $this->state    = $state;
80 31
        $this->parent   = $parent;
81 31
    }
82 31
83
    /**
84
     * Provides unique string.
85
     * Can be used to compare or aggregate actions.
86
     */
87
    public function getUniqueString(): string
88
    {
89
        $parts = [
90
            'buyer'     => $this->customer->getUniqueId(),
91
            'target'    => $this->target ? $this->target->getUniqueId() : null,
92
            'type'      => $this->type->getUniqueId(),
93
            'time'      => $this->time->format('c'),
94
        ];
95
96
        return implode('-', $parts);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getId()
103 6
    {
104
        return $this->id;
105 6
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function getCustomer(): CustomerInterface
111 3
    {
112
        return $this->customer;
113 3
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getTarget(): TargetInterface
119 23
    {
120
        return $this->target;
121 23
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getType(): TypeInterface
127 23
    {
128
        return $this->type;
129 23
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function getQuantity(): QuantityInterface
135 21
    {
136
        return $this->quantity;
137 21
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function getSale(): ?SaleInterface
143 4
    {
144
        return $this->sale;
145 4
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function getTime(): DateTimeImmutable
151 16
    {
152
        return $this->time;
153 16
    }
154
155
    public function setTime(DateTimeImmutable $time)
156
    {
157
        $this->time = $time;
158
    }
159
160
    public function getState(): ?ActionState
161
    {
162
        return $this->state;
163
    }
164
165
    public function setFinished(): void
166
    {
167
        $this->state = ActionState::finished();
168
    }
169
170
    public function isNotActive(): ?bool
171 4
    {
172
        return $this->state === null ? null : $this->state->isNotActive();
173 4
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function getParent(): ?ActionInterface
179
    {
180
        return $this->parent;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function hasParent()
187
    {
188
        return $this->parent !== null;
189
    }
190
191
    public function hasId()
192 4
    {
193
        return $this->id !== null;
194 4
    }
195
196
    public function setId($id)
197 4
    {
198
        if ((string) $this->id === (string) $id) {
199 4
            return;
200 4
        }
201
        if ($this->hasId()) {
202 4
            throw new CannotReassignException("action id old:{$this->id} new: $id");
203 4
        }
204
        $this->id = $id;
205 4
    }
206 4
207
    public function hasSale()
208 8
    {
209
        return $this->sale !== null;
210 8
    }
211
212
    public function setSale(SaleInterface $sale)
213 8
    {
214
        if ($this->hasSale()) {
215 8
            throw new CannotReassignException('action sale');
216
        }
217
        $this->sale = $sale;
218 8
    }
219 8
220
    /**
221
     * {@inheritdoc}
222
     */
223
    public function jsonSerialize(): array
224
    {
225
        return array_filter(get_object_vars($this));
226
    }
227
228
    public function getUsageInterval(): UsageInterval
229
    {
230
        if ($this->getSale() === null) {
231
            return UsageInterval::wholeMonth($this->getTime());
232
        }
233
234
        return UsageInterval::withinMonth($this->getTime(), $this->getSale()->getTime(), $this->getSale()->getCloseTime());
235
    }
236
}
237