Completed
Push — master ( a4f70a...5b7a0f )
by Andrii
05:16
created

Charge::setBill()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
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\charge;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\action\ActionInterface;
15
use hiqdev\php\billing\bill\BillInterface;
16
use hiqdev\php\billing\price\PriceInterface;
17
use hiqdev\php\billing\target\TargetInterface;
18
use hiqdev\php\units\QuantityInterface;
19
use Money\Money;
20
21
/**
22
 * Charge.
23
 *
24
 * [[Action]] is charged with a number of [[Charge]]s.
25
 *
26
 * @author Andrii Vasyliev <[email protected]>
27
 */
28
class Charge implements ChargeInterface
29
{
30
    /**
31
     * @var int
32
     */
33
    protected $id;
34
35
    /**
36
     * @var ActionInterface
37
     */
38
    protected $action;
39
40
    /**
41
     * @var PriceInterface
42
     */
43
    protected $price;
44
45
    /**
46
     * @var TargetInterface
47
     */
48
    protected $target;
49
50
    /**
51
     * @var QuantityInterface
52
     */
53
    protected $usage;
54
55
    /**
56
     * @var Money
57
     */
58
    protected $sum;
59
60
    /**
61
     * @var DateTimeImmutable
62
     */
63
    protected $time;
64
65
    /**
66
     * @var BillInterface
67
     */
68
    protected $bill;
69
70 2
    public function __construct(
71
                            $id,
72
        ActionInterface     $action = null,
73
        PriceInterface      $price = null,
74
        TargetInterface     $target = null,
75
        QuantityInterface   $usage,
76
        Money               $sum,
77
        DateTimeImmutable   $time = null,
78
        BillInterface       $bill = null
79
    ) {
80 2
        $this->id       = $id;
81 2
        $this->action   = $action;
82 2
        $this->price    = $price;
83 2
        $this->target   = $target;
84 2
        $this->usage    = $usage;
85 2
        $this->sum      = $sum;
86 2
        $this->time     = $time;
87 2
        $this->bill     = $bill;
88 2
    }
89
90
    public function getId()
91
    {
92
        return $this->id;
93
    }
94
95 2
    public function getAction()
96
    {
97 2
        return $this->action;
98
    }
99
100 2
    public function getTarget()
101
    {
102 2
        return $this->target;
103
    }
104
105
    /**
106
     * @return PriceInterface
107
     */
108 2
    public function getPrice()
109
    {
110 2
        return $this->price;
111
    }
112
113 2
    public function getUsage()
114
    {
115 2
        return $this->usage;
116
    }
117
118 2
    public function getSum()
119
    {
120 2
        return $this->sum;
121
    }
122
123
    public function calculatePrice()
124
    {
125
        $usage = $this->usage->getQuantity();
126
127
        return $usage ? $this->sum->divide($usage) : $this->sum;
128
    }
129
130
    public function getTime()
131
    {
132
        return $this->time;
133
    }
134
135
    public function getBill()
136
    {
137
        return $this->bill;
138
    }
139
140
    public function hasBill()
141
    {
142
        return $this->bill !== null;
143
    }
144
145
    public function setBill(BillInterface $bill)
146
    {
147
        if ($this->hasBill()) {
148
            throw new \Exception('cannot reassign sale bill');
149
        }
150
        $this->bill = $bill;
151
    }
152
153 View Code Duplication
    public function setId($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        if ($this->id === $id) {
156
            return;
157
        }
158
        if ($this->id !== null) {
159
            throw new \Exception('cannot reassign sale id');
160
        }
161
        $this->id = $id;
162
    }
163
164
    public function jsonSerialize()
165
    {
166
        return get_object_vars($this);
167
    }
168
}
169