DiscountApplicator::calculate()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.3888
c 0
b 0
f 0
cc 5
nc 7
nop 2
crap 5
1
<?php
2
3
/*
4
 * This file is part of ibrand/discount.
5
 *
6
 * (c) iBrand <https://www.ibrand.cc>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace iBrand\Component\Discount\Applicators;
13
14
use iBrand\Component\Discount\Checkers\ItemTotalRuleChecker;
15
use iBrand\Component\Discount\Contracts\DiscountContract;
16
use iBrand\Component\Discount\Contracts\DiscountSubjectContract;
17
18
/**
19
 * Class DiscountApplicator.
20
 */
21
class DiscountApplicator
22
{
23
    /**
24
     * @param DiscountSubjectContract $subject
25
     * @param DiscountContract        $discount
26
     */
27 2
    public function apply(DiscountSubjectContract $subject, DiscountContract $discount)
28
    {
29 2
        if (count($discount->getActions())) {
30 2
            foreach ($discount->getActions() as $action) {
31 2
                $configuration = json_decode($action->configuration, true);
32
33 2
                app($action->type)->execute($subject, $configuration, $discount);
34
35 2
                $discount->setCouponUsed();
36
            }
37
        }
38 2
    }
39
40
    /**
41
     * calculate order discount.
42
     *
43
     * @param DiscountSubjectContract $subject
44
     * @param DiscountContract        $discount
45
     */
46 1
    public function calculate(DiscountSubjectContract $subject, DiscountContract $discount)
47
    {
48 1
        $discount->orderAmountLimit = 0;
0 ignored issues
show
Bug introduced by
Accessing orderAmountLimit on the interface iBrand\Component\Discoun...tracts\DiscountContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
49
50 1
        if (count($discount->getActions())) {
51 1
            foreach ($discount->getActions() as $action) {
52 1
                $configuration = json_decode($action->configuration, true);
53 1
                app($action->type)->calculate($subject, $configuration, $discount);
54
            }
55
        }
56
57 1
        foreach ($discount->getRules() as $rule) {
58 1
            if (ItemTotalRuleChecker::TYPE == $rule->type) {
59 1
                $discount->orderAmountLimit = json_decode($rule->configuration, true)['amount'];
0 ignored issues
show
Bug introduced by
Accessing orderAmountLimit on the interface iBrand\Component\Discoun...tracts\DiscountContract suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
60
            }
61
        }
62 1
    }
63
}
64