Completed
Push — master ( 8530e7...2147ca )
by Andrii
02:54
created

Calculator::findSales()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 13
cts 14
cp 0.9286
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 15
nc 6
nop 1
crap 5.009
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\order;
12
13
use hiqdev\php\billing\plan\PlanRepositoryInterface;
14
use hiqdev\php\billing\sale\SaleRepositoryInterface;
15
16
/**
17
 * @author Andrii Vasyliev <[email protected]>
18
 */
19
class Calculator implements CalculatorInterface
20
{
21
    /**
22
     * @var SaleRepositoryInterface
23
     */
24
    private $saleRepository;
25
26
    /**
27
     * @var PlanRepositoryInterface
28
     */
29
    private $planRepository;
30
31
    /**
32
     * @param PlanRepositoryInterface $planRepository
33
     */
34 2
    public function __construct(
35
        SaleRepositoryInterface $saleRepository,
36
        PlanRepositoryInterface $planRepository = null
37
    ) {
38 2
        $this->saleRepository = $saleRepository;
39 2
        $this->planRepository = $planRepository;
40 2
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function calculateCharges(OrderInterface $order)
46
    {
47 2
        $plans = $this->findPlans($order);
48 2
        $charges = [];
49 2 View Code Duplication
        foreach ($order->getActions() as $actionKey => $action) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50 2
            if (empty($plans[$actionKey])) {
51
                /* XXX not sure... think more
52
                throw new FailedFindPlan();
53
                 */
54
                continue;
55
            }
56 2
            $charges[$actionKey] = $plans[$actionKey]->calculateCharges($action);
57
        }
58
59 2
        return $charges;
60
    }
61
62 2
    public function findPlans(OrderInterface $order)
63
    {
64 2
        $sales = $this->findSales($order);
65 2
        $plans = [];
66 2
        $lookPlanIds = [];
67 2
        foreach ($order->getActions() as $actionKey => $action) {
68 2
            if (empty($sales[$actionKey])) {
69
                throw new \Exception('not found sale');
70
            }
71 2
            $sale = $sales[$actionKey];
72 2
            $plan = $sale->getPlan();
73 2
            if ($plan->hasPrices()) {
74 2
                $plans[$actionKey] = $plan;
75
            } else {
76 2
                $lookPlanIds[$actionKey] = $plan->getId();
77
            }
78
        }
79
80 2
        if ($lookPlanIds) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $lookPlanIds of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
81
            $foundPlans = $this->planRepository->findByIds($lookPlanIds);
82
            foreach ($foundPlans as $actionKey => $plan) {
83
                $foundPlans[$plan->getId()] = $plan;
84
            }
85
            foreach ($lookPlanIds as $actionKey => $planId) {
86
                if (empty($foundPlans[$planId])) {
87
                    throw new \Exception('not found plan');
88
                }
89
                $plans[$actionKey] = $foundPlans[$planId];
90
            }
91
        }
92
93 2
        return $plans;
94
    }
95
96 2
    public function findSales(OrderInterface $order)
97
    {
98 2
        $sales = [];
99 2
        $lookActions = [];
100 2
        foreach ($order->getActions() as $actionKey => $action) {
101 2
            $sale = $action->getSale();
102 2
            if ($sale) {
103
                $sales[$actionKey] = $sale;
104
            } else {
105 2
                $lookActions[$actionKey] = $action;
106
            }
107
        }
108
109 2
        if ($lookActions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $lookActions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
110 2
            $lookOrder = new Order(null, $order->getCustomer(), $lookActions);
111 2
            $foundSales = $this->saleRepository->findByOrder($lookOrder);
112 2
            foreach ($foundSales as $actionKey => $plan) {
113 2
                $sales[$actionKey] = $plan;
114
            }
115
        }
116
117 2
        return $sales;
118
    }
119
}
120