Completed
Push — master ( 8530e7...2147ca )
by Andrii
02:54
created

AbstractPrice   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 74.19%

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 5
dl 0
loc 118
ccs 23
cts 31
cp 0.7419
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getId() 0 4 1
A getType() 0 4 1
A getTarget() 0 4 1
A getPlan() 0 4 1
A calculateSum() 0 15 3
A create() 0 4 1
A jsonSerialize() 0 8 1
A isApplicable() 0 14 2
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\EntityInterface;
15
use hiqdev\php\billing\plan\PlanInterface;
16
use hiqdev\php\billing\target\TargetInterface;
17
use hiqdev\php\billing\type\TypeInterface;
18
use hiqdev\php\units\QuantityInterface;
19
use Money\Money;
20
21
/**
22
 * Price.
23
 * @see PriceInterface
24
 * By default Price is applicable when same target and same type as Action.
25
 * But it can be different e.g. same price for all targets when certain type.
26
 *
27
 * @author Andrii Vasyliev <[email protected]>
28
 */
29
abstract class AbstractPrice implements PriceInterface, EntityInterface
30
{
31
    /**
32
     * @var integer
33
     */
34
    protected $id;
35
36
    /**
37
     * @var TypeInterface
38
     */
39
    protected $type;
40
41
    /**
42
     * @var TargetInterface
43
     */
44
    protected $target;
45
46
    /**
47
     * @var PlanInterface
48
     */
49
    protected $plan;
50
51 7
    public function __construct(
52
                            $id,
53
        TypeInterface       $type,
54
        TargetInterface     $target,
55
        PlanInterface       $plan = null
56
    ) {
57 7
        $this->id = $id;
58 7
        $this->type = $type;
59 7
        $this->target = $target;
60 7
        $this->plan = $plan;
61 7
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2
    public function getId()
67
    {
68 2
        return $this->id;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 8
    public function getType()
75
    {
76 8
        return $this->type;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 8
    public function getTarget()
83
    {
84 8
        return $this->target;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 1
    public function getPlan()
91
    {
92 1
        return $this->plan;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     * Default sum calculation method: sum = price * usage.
98
     */
99 1
    public function calculateSum(QuantityInterface $quantity): ?Money
100
    {
101 1
        $usage = $this->calculateUsage($quantity);
102 1
        if ($usage === null) {
103
            return null;
104
        }
105
106 1
        $price = $this->calculatePrice($quantity);
107 1
        if ($price === null) {
108
            return null;
109
        }
110
111
        /// TODO add configurable rounding mode later
112 1
        return $price->multiply($usage->getQuantity(), Money::ROUND_UP);
113
    }
114
115
    public static function create(array $data)
116
    {
117
        return new SinglePrice($data['id'], $data['target']);
118
    }
119
120
    public function jsonSerialize()
121
    {
122
        return [
123
            'id' => $this->id,
124
            'type' => $this->type,
125
            'target' => $this->target,
126
        ];
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 6
    public function isApplicable(ActionInterface $action): bool
133
    {
134
        /* 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...
135
         * var_dump([
136
            'action.target'     => $action->getTarget(),
137
            'this.target'       => $this->getTarget(),
138
            'action.type'       => $action->getType(),
139
            'this.type'         => $this->getType(),
140
            'target matches'    => $action->getTarget()->matches($this->getTarget()),
141
            'type matches'      => $action->getType()->matches($this->getType()),
142
        ]);*/
143 6
        return $action->getTarget()->matches($this->getTarget()) &&
144 6
               $action->getType()->matches($this->getType());
145
    }
146
}
147