Completed
Push — master ( da296e...3ed755 )
by Dmitry
03:50
created

AbstractPrice::calculateCharges()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
cc 3
eloc 5
nc 3
nop 1
crap 3.072
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\price;
12
13
use hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\charge\ChargeModifier;
15
use hiqdev\php\billing\EntityInterface;
16
use hiqdev\php\billing\plan\PlanInterface;
17
use hiqdev\php\billing\target\TargetInterface;
18
use hiqdev\php\billing\type\TypeInterface;
19
use hiqdev\php\units\QuantityInterface;
20
use Money\Money;
21
22
/**
23
 * Price.
24
 * @see PriceInterface
25
 * By default Price is applicable when same target and same type as Action.
26
 * But it can be different e.g. same price for all targets when certain type.
27
 *
28
 * @author Andrii Vasyliev <[email protected]>
29
 */
30
abstract class AbstractPrice implements PriceInterface, EntityInterface
31
{
32
    /**
33
     * @var integer
34
     */
35
    protected $id;
36
37
    /**
38
     * @var TypeInterface
39
     */
40
    protected $type;
41
42
    /**
43
     * @var TargetInterface
44
     */
45
    protected $target;
46
47
    /**
48
     * @var PlanInterface
49
     */
50
    protected $plan;
51
52 13
    public function __construct(
53
                            $id,
54
        TypeInterface       $type,
55
        TargetInterface     $target,
56
        PlanInterface       $plan = null
57
    ) {
58 13
        $this->id = $id;
59 13
        $this->type = $type;
60 13
        $this->target = $target;
61 13
        $this->plan = $plan;
62 13
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 2
    public function getId()
68
    {
69 2
        return $this->id;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 12
    public function getType()
76
    {
77 12
        return $this->type;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 12
    public function getTarget()
84
    {
85 12
        return $this->target;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 1
    public function getPlan()
92
    {
93 1
        return $this->plan;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     * Default sum calculation method: sum = price * usage.
99
     */
100 4
    public function calculateSum(QuantityInterface $quantity): ?Money
101
    {
102 4
        $usage = $this->calculateUsage($quantity);
103 4
        if ($usage === null) {
104
            return null;
105
        }
106
107 4
        $price = $this->calculatePrice($quantity);
108 4
        if ($price === null) {
109
            return null;
110
        }
111
112
        /// TODO add configurable rounding mode later
113 4
        return $price->multiply($usage->getQuantity(), Money::ROUND_UP);
114
    }
115
116
    public static function create(array $data)
117
    {
118
        return new SinglePrice($data['id'], $data['target']);
119
    }
120
121
    public function jsonSerialize()
122
    {
123
        return [
124
            'id' => $this->id,
125
            'type' => $this->type,
126
            'target' => $this->target,
127
        ];
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 10
    public function isApplicable(ActionInterface $action): bool
134
    {
135
        /* sorry, debugging facility
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
136
         * var_dump([
137
            'action.target'     => $action->getTarget(),
138
            'this.target'       => $this->getTarget(),
139
            'action.type'       => $action->getType(),
140
            'this.type'         => $this->getType(),
141
            'target matches'    => $action->getTarget()->matches($this->getTarget()),
142
            'type matches'      => $action->getType()->matches($this->getType()),
143
        ]);*/
144 10
        return $action->getTarget()->matches($this->getTarget()) &&
145 10
               $action->getType()->matches($this->getType());
146
    }
147
148
    /** {@inheritdoc} */
149 4
    public function calculateCharges(ActionInterface $action): array
150
    {
151 4
        $charge = $action->calculateCharge($this);
152 4
        if ($this instanceof ChargeModifier) {
153
            return $this->modifyCharge($charge, $action);
154
        }
155
156 4
        return $charge ? [$charge] : [];
157
    }
158
}
159