Completed
Push — master ( 2bd21a...de5d06 )
by Dmitry
03:00
created

AbstractAction::setFinished()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
246
         * var_dump([
247
            'unit'      => $usage->getUnit()->getName(),
248
            'quantity'  => $usage->getQuantity(),
249
            'price'     => $price->calculatePrice($usage)->getAmount(),
250
            'sum'       => $sum->getAmount(),
251
        ]);*/
252
253 13
        return new Charge(null, $this, $price, $usage, $sum);
254
    }
255
}
256