Completed
Push — master ( f79ff7...908aca )
by Dmitry
15:25 queued 10:35
created

src/charge/modifiers/GrowingDiscount.php (6 issues)

Labels
Severity
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\charge\modifiers;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\charge\ChargeInterface;
15
use hiqdev\php\billing\charge\modifiers\addons\Discount;
0 ignored issues
show
This use statement conflicts with another class in this namespace, hiqdev\php\billing\charge\modifiers\Discount. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
use hiqdev\php\billing\charge\modifiers\addons\Maximum;
17
use hiqdev\php\billing\charge\modifiers\addons\Minimum;
18
use hiqdev\php\billing\charge\modifiers\addons\Period;
19
use hiqdev\php\billing\charge\modifiers\addons\Step;
20
use Money\Money;
21
22
/**
23
 * Growing discount.
24
 *
25
 * @author Andrii Vasyliev <[email protected]>
26
 */
27
class GrowingDiscount extends FixedDiscount
28
{
29
    const PERIOD = 'period';
30
    const STEP = 'step';
31
    const MIN = 'min';
32
    const MAX = 'max';
33
34 5
    public function __construct($step, $min = null, array $addons = [])
35
    {
36 5
        parent::__construct($step, $addons);
37 5
        $this->addAddon(self::STEP, new Step($step));
38 5
        if ($min) {
39
            $this->min($min);
40
        }
41 5
    }
42
43 2
    public function isAbsolute()
44
    {
45 2
        return $this->getStep()->isAbsolute();
0 ignored issues
show
The method isAbsolute() does not exist on hiqdev\php\billing\charge\modifiers\AddonInterface. It seems like you code against a sub-type of hiqdev\php\billing\charge\modifiers\AddonInterface such as hiqdev\php\billing\charg...difiers\addons\Discount. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        return $this->getStep()->/** @scrutinizer ignore-call */ isAbsolute();
Loading history...
46
    }
47
48 4
    public function getStep()
49
    {
50 4
        return $this->getAddon(self::STEP);
51
    }
52
53 4
    public function getMin()
54
    {
55 4
        return $this->getAddon(self::MIN);
56
    }
57
58 2
    public function getMax()
59
    {
60 2
        if (!$this->hasAddon(self::MAX)) {
61 2
            $this->max('100%');
62
        }
63
64 2
        return $this->getAddon(self::MAX);
65
    }
66
67
    public function min($min)
68
    {
69
        $min = new Minimum($min);
70
        $this->getStep()->ensureSameType($min, 'minimum');
0 ignored issues
show
The method ensureSameType() does not exist on hiqdev\php\billing\charge\modifiers\AddonInterface. It seems like you code against a sub-type of hiqdev\php\billing\charge\modifiers\AddonInterface such as hiqdev\php\billing\charg...difiers\addons\Discount. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        $this->getStep()->/** @scrutinizer ignore-call */ ensureSameType($min, 'minimum');
Loading history...
71
72
        return $this->addAddon(self::MIN, $min);
73
    }
74
75 2
    public function max($max)
76
    {
77 2
        return $this->addAddon(self::MAX, new Maximum($max));
78
    }
79
80 4
    public function getPeriod()
81
    {
82 4
        return $this->getAddon(self::PERIOD);
83
    }
84
85 4
    public function every($string = 1)
86
    {
87 4
        return $this->addAddon(self::PERIOD, Period::fromString($string));
88
    }
89
90 2
    public function calculateSum(ChargeInterface $charge = null): Money
91
    {
92 2
        $sum = parent::calculateSum($charge);
93
94 2
        if ($this->getMax() !== null) {
95 2
            $max = $this->getMax()->calculateSum($charge);
0 ignored issues
show
The method calculateSum() does not exist on hiqdev\php\billing\charge\modifiers\AddonInterface. It seems like you code against a sub-type of hiqdev\php\billing\charge\modifiers\AddonInterface such as hiqdev\php\billing\charg...difiers\addons\Discount. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
            $max = $this->getMax()->/** @scrutinizer ignore-call */ calculateSum($charge);
Loading history...
96 2
            if ($sum->compare($max) > 0) {
97
                $sum = $max;
98
            }
99
        }
100
101 2
        return $sum;
102
    }
103
104 4
    public function getValue(ChargeInterface $charge = null): Discount
105
    {
106 4
        $time = $charge ? $charge->getAction()->getTime() : new DateTimeImmutable();
107 4
        $num = (int) $this->countPeriodsPassed($time);
108
109 4
        return $this->getStep()->calculateFor($num, $this->getMin());
0 ignored issues
show
The method calculateFor() does not exist on hiqdev\php\billing\charge\modifiers\AddonInterface. It seems like you code against a sub-type of hiqdev\php\billing\charge\modifiers\AddonInterface such as hiqdev\php\billing\charge\modifiers\addons\Step. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        return $this->getStep()->/** @scrutinizer ignore-call */ calculateFor($num, $this->getMin());
Loading history...
110
    }
111
112 4
    protected function countPeriodsPassed(DateTimeImmutable $time)
113
    {
114 4
        $since = $this->getSince();
115 4
        if ($since === null) {
116
            throw new \Exception('no since given for growing discount');
117
        }
118
119 4
        $period = $this->getPeriod();
120 4
        if ($period === null) {
121
            throw new \Exception('no period given for growing discount');
122
        }
123
124 4
        return $period->countPeriodsPassed($since->getValue(), $time);
0 ignored issues
show
The method countPeriodsPassed() does not exist on hiqdev\php\billing\charge\modifiers\AddonInterface. It seems like you code against a sub-type of hiqdev\php\billing\charge\modifiers\AddonInterface such as hiqdev\php\billing\charge\modifiers\addons\Period. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

124
        return $period->/** @scrutinizer ignore-call */ countPeriodsPassed($since->getValue(), $time);
Loading history...
125
    }
126
}
127