Completed
Push — master ( b28d3c...039260 )
by Dmitry
02:38
created

EnumPrice::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 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\plan\PlanInterface;
14
use hiqdev\php\billing\target\TargetInterface;
15
use hiqdev\php\billing\type\TypeInterface;
16
use hiqdev\php\units\QuantityInterface;
17
use hiqdev\php\units\UnitInterface;
18
use Money\Currency;
19
use Money\Money;
20
21
/**
22
 * Enum Price:
23
 * - holds sums list: amount => total sum for the quantity NOT price per unit
24
 * - listed quantities only else exception.
25
 * @see PriceInterface
26
 *
27
 * @author Andrii Vasyliev <[email protected]>
28
 */
29
class EnumPrice extends AbstractPrice
30
{
31
    /**
32
     * @var UnitInterface
33
     */
34
    protected $unit;
35
36
    /**
37
     * @var Currency
38
     */
39
    protected $currency;
40
41
    /**
42
     * @var array quantity => total sum for the quantity
43
     */
44
    protected $sums;
45
46 2
    public function __construct(
47
                            $id,
48
        TypeInterface       $type,
49
        TargetInterface     $target,
50
        PlanInterface       $plan = null,
51
        UnitInterface       $unit,
52
        Currency            $currency,
53
        array               $sums
54
    ) {
55 2
        parent::__construct($id, $type, $target, $plan);
56 2
        $this->unit = $unit;
57 2
        $this->currency = $currency;
58 2
        $this->sums = $sums;
59 2
    }
60
61 1
    public function getUnit()
62
    {
63 1
        return $this->unit;
64
    }
65
66 1
    public function getCurrency()
67
    {
68 1
        return $this->currency;
69
    }
70
71 1
    public function getSums()
72
    {
73 1
        return $this->sums;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public function calculateSum(QuantityInterface $quantity): ?Money
80
    {
81 1
        $usage = $this->calculateUsage($quantity)->getQuantity();
82
83 1
        foreach ($this->sums as $value => $price) {
84 1
            if ((string) $value === (string) $usage) {
85 1
                return new Money($price, $this->currency);
86
            }
87
        }
88
89
        throw new FailedCalculatePriceException('not enumed quantity: ' . $usage);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function calculatePrice(QuantityInterface $quantity): ?Money
96
    {
97
        $sum = $this->calculateSum($quantity);
98
        if ($sum === null) {
99
            return null;
100
        }
101
102
        $usage = $this->calculateUsage($quantity);
103
        if ($usage === null) {
104
            return null;
105
        }
106
107
        return $sum->divide($usage->getQuantity());
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 1
    public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface
114
    {
115 1
        return $quantity->convert($this->unit);
116
    }
117
118
    public function jsonSerialize()
119
    {
120
        return array_merge(parent::jsonSerialize(), [
0 ignored issues
show
Best Practice introduced by
The expression return array_merge(paren...unit' => $this->unit)); seems to be an array, but some of its elements' types (array) are incompatible with the return type of the parent method hiqdev\php\billing\price...actPrice::jsonSerialize of type array<string,integer|hiq...target\TargetInterface>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
121
            'currency' => $this->currency,
122
            'sums' => $this->sums,
123
            'unit' => $this->unit
124
        ]);
125
    }
126
}
127