Completed
Push — master ( 427141...dd7e2a )
by Dmitry
05:54
created

Charge::calculatePrice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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