Completed
Push — master ( 888950...2eb122 )
by Andrii
05:25
created

AbstractPrice   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 15
lcom 3
cbo 6
dl 0
loc 128
rs 10
c 0
b 0
f 0
ccs 27
cts 36
cp 0.75

10 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
A calculateCharges() 0 9 3
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 13
    public function __construct(
52
                            $id,
53
        TypeInterface       $type,
54
        TargetInterface     $target,
55
        PlanInterface       $plan = null
56
    ) {
57 13
        $this->id = $id;
58 13
        $this->type = $type;
59 13
        $this->target = $target;
60 13
        $this->plan = $plan;
61 13
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2
    public function getId()
67
    {
68 2
        return $this->id;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 12
    public function getType()
75
    {
76 12
        return $this->type;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 12
    public function getTarget()
83
    {
84 12
        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 4
    public function calculateSum(QuantityInterface $quantity): ?Money
100
    {
101 4
        $usage = $this->calculateUsage($quantity);
102 4
        if ($usage === null) {
103
            return null;
104
        }
105
106 4
        $price = $this->calculatePrice($quantity);
107 4
        if ($price === null) {
108
            return null;
109
        }
110
111
        /// TODO add configurable rounding mode later
112 4
        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 10
    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 10
        return $action->getTarget()->matches($this->getTarget()) &&
144 10
               $action->getType()->matches($this->getType());
145
    }
146
147 4
    public function calculateCharges(ActionInterface $action)
148
    {
149 4
        $charge = $action->calculateCharge($this);
150 4
        if ($this instanceof ChargeModifier) {
0 ignored issues
show
Bug introduced by
The class hiqdev\php\billing\price\ChargeModifier does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
151
            return $this->modifyCharge($charge, $action);
152
        }
153
154 4
        return $charge ? [$charge] : [];
155
    }
156
}
157