Completed
Push — master ( be711a...1da756 )
by Dmitry
02:41
created

FixedDiscount::buildPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
ccs 3
cts 3
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 hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\charge\Charge;
15
use hiqdev\php\billing\charge\ChargeInterface;
16
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...
17
use hiqdev\php\billing\price\SinglePrice;
18
use hiqdev\php\billing\target\Target;
19
use hiqdev\php\billing\target\TargetInterface;
20
use hiqdev\php\billing\type\Type;
21
use hiqdev\php\units\Quantity;
22
use Money\Money;
23
24
/**
25
 * Fixed discount.
26
 *
27
 * @author Andrii Vasyliev <[email protected]>
28
 */
29
class FixedDiscount extends Modifier
30
{
31
    const VALUE = 'value';
32 13
33
    public function __construct($value, array $addons = [])
34 13
    {
35 13
        parent::__construct($addons);
36 13
        $this->addAddon(self::VALUE, new Discount($value));
37
    }
38 13
39
    public function getNext()
40 13
    {
41
        return $this;
42
    }
43 5
44
    public function getValue(ChargeInterface $charge = null): Discount
45 5
    {
46
        return $this->getAddon(self::VALUE);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getAddon(self::VALUE) returns the type null which is incompatible with the type-hinted return hiqdev\php\billing\charg...difiers\addons\Discount.
Loading history...
47
    }
48 3
49
    public function isAbsolute()
50 3
    {
51
        return $this->getAddon(self::VALUE)->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

51
        return $this->getAddon(self::VALUE)->/** @scrutinizer ignore-call */ isAbsolute();
Loading history...
52
    }
53 5
54
    public function isRelative()
55 5
    {
56
        return !$this->isAbsolute();
57
    }
58 4
59
    public function calculateSum(ChargeInterface $charge = null): Money
60 4
    {
61
        return $this->getValue($charge)->calculateSum($charge);
0 ignored issues
show
Bug introduced by
It seems like $charge can also be of type null; however, parameter $charge of hiqdev\php\billing\charg...iscount::calculateSum() does only seem to accept hiqdev\php\billing\charge\ChargeInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

61
        return $this->getValue($charge)->calculateSum(/** @scrutinizer ignore-type */ $charge);
Loading history...
62
    }
63 4
64
    private function buildPrice(Money $sum, TargetInterface $target)
65 4
    {
66 4
        $type = $this->getType();
67
        $prepaid = Quantity::create('items', 0);
68 4
69
        return new SinglePrice(null, $type, $target, null, $prepaid, $sum);
70
    }
71 4
72
    private function getType()
73 4
    {
74
        return new Type(Type::ANY, 'discount,discount');
75
    }
76 4
77
    public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array
78 4
    {
79
        if ($charge === null) {
80
            return [];
81
        }
82 4
83 4
        $month = $action->getTime()->modify('first day of this month midnight');
84
        if (!$this->checkPeriod($month)) {
85
            return [$charge];
86
        }
87 4
88 4
        $sum = $this->calculateSum($charge);
89 4
        $usage  = Quantity::create('items', 1);
90
        $price = $this->buildPrice($sum, $charge->getPrice()->getTarget());
91 4
92 4
        $discount = new Charge(null, $action, $price, $usage, $sum->multiply(-1));
93
        $discount->setParent($charge);
94 4
95 4
        $reason = $this->getReason();
96
        if ($reason) {
97
            $discount->setComment($reason->getValue());
98
        }
99 4
100
        return [$charge, $discount];
101
    }
102
}
103