Completed
Push — master ( 2bd21a...de5d06 )
by Dmitry
03:00
created

Calculator   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 79.17%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 122
ccs 38
cts 48
cp 0.7917
rs 10
c 0
b 0
f 0
wmc 17

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findSales() 0 22 5
A __construct() 0 8 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
     * @var \DateTimeImmutable|null
36
     */
37
    private $chargingTime;
38
39
    /**
40
     * @param SaleRepositoryInterface|null $saleRepository
41
     * @param PlanRepositoryInterface $planRepository
42
     * @param \DateTimeImmutable|null $chargingTime
43
     * @throws \Exception
44
     */
45 2
    public function __construct(
46
        ?SaleRepositoryInterface $saleRepository,
47
        ?PlanRepositoryInterface $planRepository,
48
        \DateTimeImmutable $chargingTime = null
49
    ) {
50 2
        $this->saleRepository = $saleRepository;
51 2
        $this->planRepository = $planRepository;
52 2
        $this->chargingTime = $chargingTime ?? new \DateTimeImmutable();
53 2
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 2
    public function calculateCharges(OrderInterface $order)
59
    {
60 2
        $plans = $this->findPlans($order);
61 2
        $charges = [];
62 2
        foreach ($order->getActions() as $actionKey => $action) {
63 2
            if (empty($plans[$actionKey])) {
64
                /* XXX not sure... think more
65
                throw new FailedFindPlan();
66
                 */
67
                continue;
68
            }
69
70 2
            $charges[$actionKey] = $plans[$actionKey]->calculateCharges($action);
71
        }
72
73 2
        return $charges;
74
    }
75
76
    /**
77
     * @param OrderInterface $order
78
     * @return PlanInterface[]|Plan
79
     * @throws \Exception
80
     */
81 2
    public function findPlans(OrderInterface $order)
82
    {
83 2
        $sales = $this->findSales($order);
84 2
        $plans = [];
85 2
        $lookPlanIds = [];
86 2
        foreach ($order->getActions() as $actionKey => $action) {
87 2
            if (empty($sales[$actionKey])) {
88
                /// it is ok when no sale found for upper resellers
89
                /// 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...
90
                $plans[$actionKey] = null;
91
            } else {
92 2
                $sale = $sales[$actionKey];
93
                /** @var Plan|PlanInterface[] $plan */
94 2
                $plan = $sale->getPlan();
95 2
                if ($plan->hasPrices()) {
96 2
                    $plans[$actionKey] = $plan;
97
                } else {
98 2
                    $lookPlanIds[$actionKey] = $plan->getId();
99
                }
100
            }
101
        }
102
103 2
        if ($lookPlanIds) {
104
            $foundPlans = $this->planRepository->findByIds($lookPlanIds);
105
            foreach ($foundPlans as $actionKey => $plan) {
106
                $foundPlans[$plan->getId()] = $plan;
107
            }
108
            foreach ($lookPlanIds as $actionKey => $planId) {
109
                if (empty($foundPlans[$planId])) {
110
                    throw new \Exception('not found plan');
111
                }
112
                $plans[$actionKey] = $foundPlans[$planId];
113
            }
114
        }
115
116 2
        return $plans;
117
    }
118
119
    /**
120
     * @param OrderInterface $order
121
     * @return SaleInterface[]|Sale
122
     */
123 2
    public function findSales(OrderInterface $order)
124
    {
125 2
        $sales = [];
126 2
        $lookActions = [];
127 2
        foreach ($order->getActions() as $actionKey => $action) {
128 2
            $sale = $action->getSale();
129 2
            if ($sale) {
130
                $sales[$actionKey] = $sale;
131
            } else {
132 2
                $lookActions[$actionKey] = $action;
133
            }
134
        }
135
136 2
        if ($lookActions) {
137 2
            $lookOrder = new Order(null, $order->getCustomer(), $lookActions);
138 2
            $foundSales = $this->saleRepository->findByOrder($lookOrder);
139 2
            foreach ($foundSales as $actionKey => $plan) {
140 2
                $sales[$actionKey] = $plan;
141
            }
142
        }
143
144 2
        return $sales;
145
    }
146
}
147