Issues (113)

src/action/AbstractAction.php (1 issue)

Labels
Severity
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 DateInterval;
14
use DateTimeImmutable;
15
use hiqdev\php\billing\customer\CustomerInterface;
16
use hiqdev\php\billing\Exception\CannotReassignException;
17
use hiqdev\php\billing\sale\SaleInterface;
18
use hiqdev\php\billing\target\TargetInterface;
19
use hiqdev\php\billing\type\TypeInterface;
0 ignored issues
show
The type hiqdev\php\billing\type\TypeInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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