Completed
Push — master ( cc81ea...b18114 )
by Andrii
04:47
created

Charge::setId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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