Completed
Push — master ( 01baf8...7b3b62 )
by Dmitry
03:46
created

Calculator::calculateCharges()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 1
dl 0
loc 20
ccs 8
cts 10
cp 0.8
crap 5.2
rs 9.6111
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\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
     */
39 2
    public function __construct(
40
        ?SaleRepositoryInterface $saleRepository,
41
        ?PlanRepositoryInterface $planRepository
42
    ) {
43 2
        $this->saleRepository = $saleRepository;
44 2
        $this->planRepository = $planRepository;
45 2
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 2
    public function calculateCharges(OrderInterface $order)
51
    {
52 2
        $plans = $this->findPlans($order);
53 2
        $charges = [];
54 2
        foreach ($order->getActions() as $actionKey => $action) {
55 2
            if (empty($plans[$actionKey])) {
56
                /* XXX not sure... think more
57
                throw new FailedFindPlan();
58
                 */
59
                continue;
60
            }
61
62
            // If Sale occurred after Action, the Action MUST NOT get charged.
63 2
            if ($action->getSale() !== null && $action->getSale()->getTime() > $action->getTime()) {
64
                continue;
65
            }
66 2
            $charges[$actionKey] = $plans[$actionKey]->calculateCharges($action);
67
        }
68
69 2
        return $charges;
70
    }
71
72
    /**
73
     * @param OrderInterface $order
74
     * @return PlanInterface[]|Plan
75
     * @throws \Exception
76
     */
77 2
    public function findPlans(OrderInterface $order)
78
    {
79 2
        $sales = $this->findSales($order);
80 2
        $plans = [];
81 2
        $lookPlanIds = [];
82 2
        foreach ($order->getActions() as $actionKey => $action) {
83 2
            if (empty($sales[$actionKey])) {
84
                /// it is ok when no sale found for upper resellers
85
                /// 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...
86
                $plans[$actionKey] = null;
87
            } else {
88 2
                $sale = $sales[$actionKey];
89
                /** @var Plan|PlanInterface[] $plan */
90 2
                $plan = $sale->getPlan();
91 2
                if ($plan->hasPrices()) {
92 2
                    $plans[$actionKey] = $plan;
93
                } else {
94 2
                    $lookPlanIds[$actionKey] = $plan->getId();
95
                }
96
            }
97
        }
98
99 2
        if ($lookPlanIds) {
100
            $foundPlans = $this->planRepository->findByIds($lookPlanIds);
101
            foreach ($foundPlans as $actionKey => $plan) {
102
                $foundPlans[$plan->getId()] = $plan;
103
            }
104
            foreach ($lookPlanIds as $actionKey => $planId) {
105
                if (empty($foundPlans[$planId])) {
106
                    throw new \Exception('not found plan');
107
                }
108
                $plans[$actionKey] = $foundPlans[$planId];
109
            }
110
        }
111
112 2
        return $plans;
113
    }
114
115
    /**
116
     * @param OrderInterface $order
117
     * @return SaleInterface[]|Sale
118
     */
119 2
    public function findSales(OrderInterface $order)
120
    {
121 2
        $sales = [];
122 2
        $lookActions = [];
123 2
        foreach ($order->getActions() as $actionKey => $action) {
124 2
            $sale = $action->getSale();
125 2
            if ($sale) {
126
                $sales[$actionKey] = $sale;
127
            } else {
128 2
                $lookActions[$actionKey] = $action;
129
            }
130
        }
131
132 2
        if ($lookActions) {
133 2
            $lookOrder = new Order(null, $order->getCustomer(), $lookActions);
134 2
            $foundSales = $this->saleRepository->findByOrder($lookOrder);
135 2
            foreach ($foundSales as $actionKey => $plan) {
136 2
                $sales[$actionKey] = $plan;
137
            }
138
        }
139
140 2
        return $sales;
141
    }
142
}
143