Issues (113)

src/charge/Charge.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\charge;
12
13
use hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\bill\BillInterface;
15
use hiqdev\php\billing\event\EventAwareTrait;
16
use hiqdev\php\billing\Exception\CannotReassignException;
17
use hiqdev\php\billing\price\PriceInterface;
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
use Money\Money;
22
23
/**
24
 * Charge.
25
 *
26
 * [[Action]] is charged with a number of [[Charge]]s.
27
 *
28
 * @author Andrii Vasyliev <[email protected]>
29
 */
30
class Charge implements ChargeInterface
31
{
32
    use EventAwareTrait;
33
34
    /** @var int */
35
    protected $id;
36
37
    /** @var TypeInterface */
38
    protected $type;
39
40
    /** @var TargetInterface */
41
    protected $target;
42
43
    /** @var ActionInterface */
44
    protected $action;
45
46
    /** @var PriceInterface */
47
    protected $price;
48
49
    /** @var QuantityInterface */
50
    protected $usage;
51
52
    /** @var Money */
53
    protected $sum;
54
55
    /** @var BillInterface */
56
    protected $bill;
57
58
    /** @var ChargeState */
59
    protected $state;
60
61
    /** @var string */
62
    protected $comment;
63
64
    /** @var ChargeInterface|null */
65
    protected $parent;
66
67 18
    public function __construct(
68
                            $id,
69
        TypeInterface $type,
70
        TargetInterface $target,
71
        ActionInterface $action,
72
        ?PriceInterface $price,
73
        QuantityInterface $usage,
74
        Money $sum,
75
        BillInterface $bill = null
76
    ) {
77 18
        $this->id       = $id;
78 18
        $this->type     = $type;
79 18
        $this->target   = $target;
80 18
        $this->action   = $action;
81 18
        $this->price    = $price;
82 18
        $this->usage    = $usage;
83 18
        $this->sum      = $sum;
84 18
        $this->bill     = $bill;
85 18
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getUniqueString(): string
91
    {
92
        return implode('-', [
93
            'currency'  => $this->sum->getCurrency()->getCode(),
94
            'buyer'     => $this->action->getCustomer()->getUniqueId(),
95
            'target'    => $this->target->getUniqueId(),
96
            'type'      => $this->type->getUniqueId(),
97
            'time'      => $this->action->getTime()->format('c'),
98
            'parent'    => '(' . ($this->parent !== null ? $this->parent->getUniqueString() : null) . ')',
99
        ]);
100
    }
101
102
    public function hasId(): bool
103
    {
104
        return $this->id !== null;
105
    }
106
107 1
    public function getId()
108
    {
109 1
        return $this->id;
110
    }
111
112 9
    public function getType(): TypeInterface
113
    {
114 9
        return $this->type;
115
    }
116
117 7
    public function getTarget(): TargetInterface
118
    {
119 7
        return $this->target;
120
    }
121
122 12
    public function getAction(): ActionInterface
123
    {
124 12
        return $this->action;
125
    }
126
127 7
    public function getPrice(): ?PriceInterface
128
    {
129 7
        return $this->price;
130
    }
131
132 14
    public function getUsage(): QuantityInterface
133
    {
134 14
        return $this->usage;
135
    }
136
137 14
    public function getSum(): Money
138
    {
139 14
        return $this->sum;
140
    }
141
142
    public function calculatePrice(): Money
143
    {
144
        $usage = $this->usage->getQuantity();
145
146
        return $usage ? $this->sum->divide(sprintf('%.14F', $usage)) : $this->sum;
147
    }
148
149 1
    public function getBill(): ?BillInterface
150
    {
151 1
        return $this->bill;
152
    }
153
154
    public function hasBill()
155
    {
156
        return $this->bill !== null;
157
    }
158
159
    public function setBill(BillInterface $bill): ChargeInterface
160
    {
161
        /*if ($this->hasBill()) {
162
            throw new CannotReassignException('sale bill');
163
        }*/
164
        $this->bill = $bill;
165
166
        return $this;
167
    }
168
169
    public function getState(): ?ChargeState
170
    {
171
        return $this->state;
172
    }
173
174
    public function setFinished(): ChargeInterface
175
    {
176
        $this->state = ChargeState::finished();
177
178
        return $this;
179
    }
180
181
    public function isFinished(): ?bool
182
    {
183
        return $this->state === null ? null : $this->state->isFinished();
184
    }
185
186 1
    public function getComment(): ?string
187
    {
188 1
        return $this->comment;
189
    }
190
191
    public function setComment(string $comment): ChargeInterface
192
    {
193
        $this->comment = $comment;
194
195
        return $this;
196
    }
197
198
    public function setId($id): ChargeInterface
199
    {
200
        if ((string) $this->id === (string) $id) {
201
            return $this;
202
        }
203
        if ($this->id !== null) {
204
            throw new CannotReassignException('charge id');
205
        }
206
        $this->id = $id;
207
208
        return $this;
209
    }
210
211
    public function jsonSerialize(): array
212
    {
213
        return array_filter(get_object_vars($this));
214
    }
215
216
    public function getParent(): ?ChargeInterface
217
    {
218
        return $this->parent;
219
    }
220
221
    /**
222
     * @throws CannotReassignException if parent is already set
223
     */
224
    public function setParent(ChargeInterface $parent): self
225
    {
226
        if ($this->parent !== null) {
227 4
            throw new CannotReassignException('charge parent');
228
        }
229 4
230
        $this->parent = $parent;
231
232
        return $this;
233 4
    }
234
235 4
    /**
236
     * Forcefully changes parent charge
237
     */
238
    public function overwriteParent(?ChargeInterface $parent): ChargeInterface
239
    {
240
        $this->parent = $parent;
241
242
        return $this;
243
    }
244
}
245