Completed
Push — master ( c34bb5...a29fc9 )
by Andrii
04:22
created

GrowingDiscount::every()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
Bug introduced by
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
        Modifier::__construct($addons);
0 ignored issues
show
Bug Best Practice introduced by
The method hiqdev\php\billing\charg...Modifier::__construct() is not static, but was called statically. ( Ignorable by Annotation )

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

36
        Modifier::/** @scrutinizer ignore-call */ 
37
                  __construct($addons);
Loading history...
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
Bug introduced by
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 4
    public function getMax()
59
    {
60 4
        return $this->getAddon(self::MAX);
61
    }
62
63
    public function min($min)
64
    {
65
        $min = new Minimum($min);
66
        $this->getStep()->ensureSameType($min, 'minimum');
0 ignored issues
show
Bug introduced by
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

66
        $this->getStep()->/** @scrutinizer ignore-call */ ensureSameType($min, 'minimum');
Loading history...
67
68
        return $this->addAddon(self::MIN, $min);
69
    }
70
71
    public function max($max)
72
    {
73
        return $this->addAddon(self::MAX, new Maximum($max));
74
    }
75
76 4
    public function getPeriod()
77
    {
78 4
        return $this->getAddon(self::PERIOD);
79
    }
80
81 4
    public function every($string = 1)
82
    {
83 4
        return $this->addAddon(self::PERIOD, Period::fromString($string));
84
    }
85
86 2
    public function calculateSum(ChargeInterface $charge = null): Money
87
    {
88 2
        $sum = parent::calculateSum($charge);
89
90 2
        if ($this->getMax() !== null) {
91
            $max = $this->getMax()->calculateSum($charge);
0 ignored issues
show
Bug introduced by
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

91
            $max = $this->getMax()->/** @scrutinizer ignore-call */ calculateSum($charge);
Loading history...
92
            if ($sum->compare($max) > 0) {
93
                $sum = $max;
94
            }
95
        }
96
97 2
        return $sum;
98
    }
99
100 4
    public function getValue(ChargeInterface $charge = null): Discount
101
    {
102 4
        if ($this->getMax() === null && $this->getTill() === null) {
103
            throw new \Exception("growing discount must be limited with 'max' or 'till'");
104
        }
105 4
        $time = $charge ? $charge->getAction()->getTime() : new DateTimeImmutable();
106 4
        $num = (int) $this->countPeriodsPassed($time);
107
108 4
        return $this->getStep()->calculateFor($num, $this->getMin());
0 ignored issues
show
Bug introduced by
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

108
        return $this->getStep()->/** @scrutinizer ignore-call */ calculateFor($num, $this->getMin());
Loading history...
109
    }
110
111 4
    protected function countPeriodsPassed(DateTimeImmutable $time)
112
    {
113 4
        $since = $this->getSince();
114 4
        if ($since === null) {
115
            throw new \Exception('no since given for growing discount');
116
        }
117
118 4
        $period = $this->getPeriod();
119 4
        if ($period === null) {
120
            throw new \Exception('no period given for growing discount');
121
        }
122
123 4
        return $period->countPeriodsPassed($since->getValue(), $time);
0 ignored issues
show
Bug introduced by
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\charg...fiers\addons\YearPeriod or hiqdev\php\billing\charg...iers\addons\MonthPeriod. ( Ignorable by Annotation )

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

123
        return $period->/** @scrutinizer ignore-call */ countPeriodsPassed($since->getValue(), $time);
Loading history...
Bug introduced by
The method getValue() does not exist on hiqdev\php\billing\charge\modifiers\AddonInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to hiqdev\php\billing\charge\modifiers\AddonInterface. ( Ignorable by Annotation )

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

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