Completed
Push — master ( 428142...9f94b9 )
by Andrii
02:14
created

AbstractPrice::calculateSum()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 6
cts 8
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3.1406
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\price;
12
13
use hiqdev\php\billing\EntityInterface;
14
use hiqdev\php\billing\plan\PlanInterface;
15
use hiqdev\php\billing\target\TargetInterface;
16
use hiqdev\php\billing\type\TypeInterface;
17
use hiqdev\php\units\QuantityInterface;
18
19
/**
20
 * Price.
21
 * @see PriceInterface
22
 *
23
 * @author Andrii Vasyliev <[email protected]>
24
 */
25
abstract class AbstractPrice implements PriceInterface, EntityInterface
26
{
27
    /**
28
     * @var integer
29
     */
30
    protected $id;
31
32
    /**
33
     * @var TypeInterface
34
     */
35
    protected $type;
36
37
    /**
38
     * @var TargetInterface
39
     */
40
    protected $target;
41
42
    /**
43
     * @var PlanInterface
44
     */
45
    protected $plan;
46
47 7 View Code Duplication
    public function __construct(
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...
48
                            $id,
49
        TypeInterface       $type,
50
        TargetInterface     $target,
51
        PlanInterface       $plan = null
52
    ) {
53 7
        $this->id = $id;
54 7
        $this->type = $type;
55 7
        $this->target = $target;
56 7
        $this->plan = $plan;
57 7
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 2
    public function getId()
63
    {
64 2
        return $this->id;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 7
    public function getType()
71
    {
72 7
        return $this->type;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 7
    public function getTarget()
79
    {
80 7
        return $this->target;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 1
    public function getPlan()
87
    {
88 1
        return $this->plan;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     * Default sum calculation method: sum = price * usage.
94
     */
95 1 View Code Duplication
    public function calculateSum(QuantityInterface $quantity)
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...
96
    {
97 1
        $usage = $this->calculateUsage($quantity);
98 1
        if ($usage === null) {
99
            return null;
100
        }
101
102 1
        $price = $this->calculatePrice($quantity);
103 1
        if ($price === null) {
104
            return null;
105
        }
106
107 1
        return $price->multiply($usage->getQuantity());
108
    }
109
110
    public static function create(array $data)
111
    {
112
        return new SinglePrice($data['id'], $data['target']);
0 ignored issues
show
Bug introduced by
The call to SinglePrice::__construct() misses some required arguments starting with $target.
Loading history...
113
    }
114
115
    public function jsonSerialize()
116
    {
117
        return [
118
            'id' => $this->id,
119
            'type' => $this->type,
120
            'target' => $this->target,
121
        ];
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    abstract public function calculateUsage(QuantityInterface $quantity);
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    abstract public function calculatePrice(QuantityInterface $action);
133
}
134