DiscountApplicator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 43
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 12 3
A calculate() 0 17 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