Completed
Push — master ( bab678...d073e6 )
by Andrii
02:26
created

Charge::getTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 QuantityInterface
47
     */
48
    protected $usage;
49
50
    /**
51
     * @var Money
52
     */
53
    protected $sum;
54
55
    /**
56
     * @var DateTimeImmutable
57
     */
58
    protected $time;
59
60
    /**
61
     * @var BillInterface
62
     */
63
    protected $bill;
64 5
65
    public function __construct(
66
                            $id,
67
        ActionInterface     $action,
68
        PriceInterface      $price,
69
        QuantityInterface   $usage,
70
        Money               $sum,
71
        DateTimeImmutable   $time = null,
72
        BillInterface       $bill = null
73 5
    ) {
74 5
        $this->id       = $id;
75 5
        $this->action   = $action;
76 5
        $this->price    = $price;
77 5
        $this->usage    = $usage;
78 5
        $this->sum      = $sum;
79 5
        $this->time     = $time;
80 5
        $this->bill     = $bill;
81
    }
82
83
    public function getId()
84
    {
85
        return $this->id;
86
    }
87 5
88
    public function getAction()
89 5
    {
90
        return $this->action;
91
    }
92
93
    /**
94
     * @return PriceInterface
95 5
     */
96
    public function getPrice()
97 5
    {
98
        return $this->price;
99
    }
100 5
101
    public function getUsage()
102 5
    {
103
        return $this->usage;
104
    }
105 5
106
    public function getSum()
107 5
    {
108
        return $this->sum;
109
    }
110
111
    public function calculatePrice()
112
    {
113
        $usage = $this->usage->getQuantity();
114
115
        return $usage ? $this->sum->divide($usage) : $this->sum;
116
    }
117 1
118
    public function getTime()
119 1
    {
120
        return $this->time;
121
    }
122
123
    public function getBill()
124
    {
125
        return $this->bill;
126
    }
127
128
    public function hasBill()
129
    {
130
        return $this->bill !== null;
131
    }
132
133
    public function setBill(BillInterface $bill)
134
    {
135
        if ($this->hasBill()) {
136
            throw new \Exception('cannot reassign sale bill');
137
        }
138
        $this->bill = $bill;
139
    }
140
141 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...
142
    {
143
        if ($this->id === $id) {
144
            return;
145
        }
146
        if ($this->id !== null) {
147
            throw new \Exception('cannot reassign sale id');
148
        }
149
        $this->id = $id;
150
    }
151
152
    public function jsonSerialize()
153
    {
154
        return get_object_vars($this);
155
    }
156
}
157