Passed
Push — master ( de5d06...687d03 )
by Dmitry
02:37
created

Calculator   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 78.72%

Importance

Changes 0
Metric Value
wmc 17
eloc 48
dl 0
loc 115
ccs 37
cts 47
cp 0.7872
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findSales() 0 22 5
A __construct() 0 6 1
A calculateCharges() 0 16 3
B findPlans() 0 36 8
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\Plan;
14
use hiqdev\php\billing\plan\PlanInterface;
15
use hiqdev\php\billing\plan\PlanRepositoryInterface;
16
use hiqdev\php\billing\sale\Sale;
17
use hiqdev\php\billing\sale\SaleInterface;
18
use hiqdev\php\billing\sale\SaleRepositoryInterface;
19
20
/**
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class Calculator implements CalculatorInterface
24
{
25
    /**
26
     * @var SaleRepositoryInterface
27
     */
28
    private $saleRepository;
29
30
    /**
31
     * @var PlanRepositoryInterface
32
     */
33
    private $planRepository;
34
35
    /**
36
     * @param SaleRepositoryInterface|null $saleRepository
37
     * @param PlanRepositoryInterface $planRepository
38
     * @throws \Exception
39
     */
40 2
    public function __construct(
41
        ?SaleRepositoryInterface $saleRepository,
42
        ?PlanRepositoryInterface $planRepository
43
    ) {
44 2
        $this->saleRepository = $saleRepository;
45 2
        $this->planRepository = $planRepository;
46 2
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 2
    public function calculateCharges(OrderInterface $order)
52
    {
53 2
        $plans = $this->findPlans($order);
54 2
        $charges = [];
55 2
        foreach ($order->getActions() as $actionKey => $action) {
56 2
            if (empty($plans[$actionKey])) {
57
                /* XXX not sure... think more
58
                throw new FailedFindPlan();
59
                 */
60
                continue;
61
            }
62
63 2
            $charges[$actionKey] = $plans[$actionKey]->calculateCharges($action);
64
        }
65
66 2
        return $charges;
67
    }
68
69
    /**
70
     * @param OrderInterface $order
71
     * @return PlanInterface[]|Plan
72
     * @throws \Exception
73
     */
74 2
    public function findPlans(OrderInterface $order)
75
    {
76 2
        $sales = $this->findSales($order);
77 2
        $plans = [];
78 2
        $lookPlanIds = [];
79 2
        foreach ($order->getActions() as $actionKey => $action) {
80 2
            if (empty($sales[$actionKey])) {
81
                /// it is ok when no sale found for upper resellers
82
                /// throw new \Exception('not found sale');
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
                $plans[$actionKey] = null;
84
            } else {
85 2
                $sale = $sales[$actionKey];
86
                /** @var Plan|PlanInterface[] $plan */
87 2
                $plan = $sale->getPlan();
88 2
                if ($plan->hasPrices()) {
89 2
                    $plans[$actionKey] = $plan;
90
                } else {
91 2
                    $lookPlanIds[$actionKey] = $plan->getId();
92
                }
93
            }
94
        }
95
96 2
        if ($lookPlanIds) {
97
            $foundPlans = $this->planRepository->findByIds($lookPlanIds);
98
            foreach ($foundPlans as $actionKey => $plan) {
99
                $foundPlans[$plan->getId()] = $plan;
100
            }
101
            foreach ($lookPlanIds as $actionKey => $planId) {
102
                if (empty($foundPlans[$planId])) {
103
                    throw new \Exception('not found plan');
104
                }
105
                $plans[$actionKey] = $foundPlans[$planId];
106
            }
107
        }
108
109 2
        return $plans;
110
    }
111
112
    /**
113
     * @param OrderInterface $order
114
     * @return SaleInterface[]|Sale
115
     */
116 2
    public function findSales(OrderInterface $order)
117
    {
118 2
        $sales = [];
119 2
        $lookActions = [];
120 2
        foreach ($order->getActions() as $actionKey => $action) {
121 2
            $sale = $action->getSale();
122 2
            if ($sale) {
123
                $sales[$actionKey] = $sale;
124
            } else {
125 2
                $lookActions[$actionKey] = $action;
126
            }
127
        }
128
129 2
        if ($lookActions) {
130 2
            $lookOrder = new Order(null, $order->getCustomer(), $lookActions);
131 2
            $foundSales = $this->saleRepository->findByOrder($lookOrder);
132 2
            foreach ($foundSales as $actionKey => $plan) {
133 2
                $sales[$actionKey] = $plan;
134
            }
135
        }
136
137 2
        return $sales;
138
    }
139
}
140