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

Leasing::getTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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\ChargeInterface;
15
use hiqdev\php\billing\formula\FormulaSemanticsError;
16
use hiqdev\php\billing\price\SinglePrice;
17
use hiqdev\php\billing\target\Target;
18
use hiqdev\php\billing\type\Type;
19
use hiqdev\php\units\Quantity;
20
use Money\Money;
21
22
/**
23
 * Leasing.
24
 *
25
 * @author Andrii Vasyliev <[email protected]>
26
 */
27
class Leasing extends Modifier
28
{
29 6
    public function getNext()
30
    {
31 6
        return $this;
32
    }
33
34
    public function isAbsolute()
35
    {
36
        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

36
        return $this->getAddon(self::VALUE)->/** @scrutinizer ignore-call */ isAbsolute();
Loading history...
Bug introduced by
The constant hiqdev\php\billing\charge\modifiers\Leasing::VALUE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
37
    }
38
39
    public function isRelative()
40
    {
41
        return !$this->isAbsolute();
42
    }
43
44
    public function calculateSum(ChargeInterface $charge = null): Money
45
    {
46
        return $this->getValue($charge)->calculateSum($charge);
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on hiqdev\php\billing\charge\modifiers\Leasing. ( Ignorable by Annotation )

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

46
        return $this->/** @scrutinizer ignore-call */ getValue($charge)->calculateSum($charge);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
    }
48
49
    public function buildPrice(Money $sum)
50
    {
51
        $type = $this->getType();
52
        $target = $this->getTarget();
53
        $prepaid = Quantity::items(0);
54
55
        return new SinglePrice(null, $type, $target, null, $prepaid, $sum);
56
    }
57
58
    public function getType()
59
    {
60
        return new Type(Type::ANY, 'discount');
61
    }
62
63
    public function getTarget()
64
    {
65
        return new Target(Target::ANY, Target::ANY);
66
    }
67
68 1
    public function till($dummy)
0 ignored issues
show
Unused Code introduced by
The parameter $dummy is not used and could be removed. ( Ignorable by Annotation )

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

68
    public function till(/** @scrutinizer ignore-unused */ $dummy)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
    {
70 1
        throw new FormulaSemanticsError('till can not be defined for leasing');
71
    }
72
73 1
    public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array
74
    {
75 1
        if ($charge === null) {
76
            $charge = $this->calculateCharge($charge, $action);
0 ignored issues
show
Bug introduced by
The method calculateCharge() does not exist on hiqdev\php\billing\charge\modifiers\Leasing. Did you maybe mean calculateSum()? ( Ignorable by Annotation )

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

76
            /** @scrutinizer ignore-call */ 
77
            $charge = $this->calculateCharge($charge, $action);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
        }
78
79 1
        $this->ensureIsValid();
80
81 1
        $month = $action->getTime()->modify('first day of this month midnight');
82 1
        if (!$this->checkPeriod($month)) {
83
            return [];
84
        }
85
86 1
        $reason = $this->getReason();
87 1
        if ($reason) {
88
            $charge->setComment($reason->getValue());
0 ignored issues
show
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

88
            $charge->setComment($reason->/** @scrutinizer ignore-call */ getValue());
Loading history...
89
        }
90
91 1
        return [$charge];
92
    }
93
94 1
    protected function ensureIsValid()
95
    {
96 1
        $since = $this->getSince();
97 1
        if ($since === null) {
98
            throw new FormulaSemanticsError('no since given for leasing');
99
        }
100
101 1
        $term = $this->getTerm();
102 1
        if ($term === null) {
103
            throw new FormulaSemanticsError('no term given for leasing');
104
        }
105 1
    }
106
}
107