Passed
Push — master ( 364662...9798b7 )
by Andrii
02:56
created

Calculator   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Test Coverage

Coverage 79.27%

Importance

Changes 9
Bugs 0 Features 1
Metric Value
eloc 82
dl 0
loc 194
ccs 65
cts 82
cp 0.7927
rs 9.76
c 9
b 0
f 1
wmc 33

7 Methods

Rating   Name   Duplication   Size   Complexity  
A calculatePrice() 0 20 5
A calculatePlan() 0 11 3
A findSales() 0 22 5
A calculateCharge() 0 28 4
A calculateOrder() 0 13 3
A __construct() 0 8 1
C findPlans() 0 43 12
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\action\Action;
14
use hiqdev\php\billing\action\ActionInterface;
15
use hiqdev\php\billing\action\TemporaryActionInterface;
16
use hiqdev\php\billing\charge\Charge;
17
use hiqdev\php\billing\charge\ChargeInterface;
18
use hiqdev\php\billing\charge\ChargeModifier;
19
use hiqdev\php\billing\charge\GeneralizerInterface;
20
use hiqdev\php\billing\plan\Plan;
21
use hiqdev\php\billing\plan\PlanInterface;
22
use hiqdev\php\billing\plan\PlanRepositoryInterface;
23
use hiqdev\php\billing\price\PriceInterface;
24
use hiqdev\php\billing\sale\Sale;
25
use hiqdev\php\billing\sale\SaleInterface;
26
use hiqdev\php\billing\sale\SaleRepositoryInterface;
27
28
/**
29
 * Calculator calculates charges for given order or action.
30
 *
31
 * @author Andrii Vasyliev <[email protected]>
32
 */
33
class Calculator implements CalculatorInterface
34
{
35
    /**
36
     * @var GeneralizerInterface
37
     */
38
    protected $generalizer;
39
    /**
40
     * @var SaleRepositoryInterface
41
     */
42
    private $saleRepository;
43
    /**
44
     * @var PlanRepositoryInterface
45
     */
46
    private $planRepository;
47
48
    /**
49
     * @param PlanRepositoryInterface $planRepository
50
     */
51 37
    public function __construct(
52
        GeneralizerInterface $generalizer,
53
        ?SaleRepositoryInterface $saleRepository,
54
        ?PlanRepositoryInterface $planRepository
55
    ) {
56 37
        $this->generalizer    = $generalizer;
57 37
        $this->saleRepository = $saleRepository;
58 37
        $this->planRepository = $planRepository;
59 37
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 2
    public function calculateOrder(OrderInterface $order): array
65
    {
66 2
        $plans = $this->findPlans($order);
67 2
        $charges = [];
68 2
        foreach ($order->getActions() as $actionKey => $action) {
69 2
            if ($plans[$actionKey] === null) {
70
                continue;
71
            }
72
73 2
            $charges = array_merge($charges, $this->calculatePlan($plans[$actionKey], $action));
74
        }
75
76 2
        return $charges;
77
    }
78
79 4
    public function calculatePlan(PlanInterface $plan, ActionInterface $action): array
80
    {
81 4
        $result = [];
82 4
        foreach ($plan->getPrices() as $price) {
83 4
            $charges = $this->calculatePrice($price, $action);
84 4
            if (!empty($charges)) {
85 4
                $result = array_merge($result, $charges);
86
            }
87
        }
88
89 4
        return $result;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 4
    public function calculatePrice(PriceInterface $price, ActionInterface $action): array
96
    {
97 4
        $charge = $this->calculateCharge($price, $action);
98 4
        if ($charge === null) {
99 4
            return [];
100
        }
101
102 4
        if ($price instanceof ChargeModifier) {
103 4
            $charges = $price->modifyCharge($charge, $action);
104
        } else {
105
            $charges = [$charge];
106
        }
107
108 4
        if ($action->isFinished()) {
109
            foreach ($charges as $charge) {
110
                $charge->setFinished();
111
            }
112
        }
113
114 4
        return $charges;
115
    }
116
117
    /**
118
     * Calculates charge for given action and price.
119
     * Returns `null`, if $price is not applicable to $action.
120
     *
121
     * @return ChargeInterface|Charge|null
122
     */
123 25
    public function calculateCharge(PriceInterface $price, ActionInterface $action): ?ChargeInterface
124
    {
125 25
        if (!$action->isApplicable($price)) {
126 8
            return null;
127
        }
128
129 21
        $usage = $price->calculateUsage($action->getQuantity());
130 21
        if ($usage === null) {
131 4
            return null;
132
        }
133
134 17
        $sum = $price->calculateSum($action->getQuantity());
135 17
        if ($sum === null) {
136
            return null;
137
        }
138
139 17
        $type = $this->generalizer->specializeType($price->getType(), $action->getType());
140 17
        $target = $this->generalizer->specializeTarget($price->getTarget(), $action->getTarget());
141
142
        /* sorry, debugging facility
143
         * var_dump([
144
            'unit'      => $usage->getUnit()->getName(),
145
            'quantity'  => $usage->getQuantity(),
146
            'price'     => $price->calculatePrice($usage)->getAmount(),
147
            'sum'       => $sum->getAmount(),
148
        ]);*/
149
150 17
        return new Charge(null, $type, $target, $action, $price, $usage, $sum);
151
    }
152
153
    /**
154
     * @throws \Exception
155
     * @return PlanInterface[]|Plan
156
     */
157 2
    public function findPlans(OrderInterface $order)
158
    {
159 2
        $sales = $this->findSales($order);
160 2
        $plans = [];
161 2
        $lookPlanIds = [];
162 2
        foreach ($order->getActions() as $actionKey => $action) {
163
            /** @var Action $action */
164 2
            if ($sales[$actionKey] === false) {
165
                /// it is ok when no sale found for upper resellers
166
                $plans[$actionKey] = null;
167
            } else {
168 2
                $sale = $sales[$actionKey];
169
                /** @var Plan|PlanInterface[] $plan */
170 2
                $plan = $sale->getPlan();
171
172 2
                if ($action instanceof TemporaryActionInterface && $plan->getId() && !$action->hasSale()) {
173
                    $action->setSale($sale);
174
                }
175
176 2
                if ($plan->hasPrices()) {
177 2
                    $plans[$actionKey] = $plan;
178
                } elseif ($plan->getId() !== null) {
179
                    $lookPlanIds[$actionKey] = $plan->getId();
180
                } else {
181 2
                    $plans[$actionKey] = null;
182
                }
183
            }
184
        }
185
186 2
        if ($lookPlanIds) {
187
            $foundPlans = $this->planRepository->findByIds($lookPlanIds);
188
            foreach ($foundPlans as $actionKey => $plan) {
189
                $foundPlans[$plan->getId()] = $plan;
190
            }
191
            foreach ($lookPlanIds as $actionKey => $planId) {
192
                if (empty($foundPlans[$planId])) {
193
                    throw new \Exception('not found plan');
194
                }
195
                $plans[$actionKey] = $foundPlans[$planId];
196
            }
197
        }
198
199 2
        return $plans;
200
    }
201
202
    /**
203
     * @return SaleInterface[]|Sale
204
     */
205 2
    public function findSales(OrderInterface $order)
206
    {
207 2
        $sales = [];
208 2
        $lookActions = [];
209 2
        foreach ($order->getActions() as $actionKey => $action) {
210 2
            $sale = $action->getSale();
211 2
            if ($sale) {
212
                $sales[$actionKey] = $sale;
213
            } else {
214 2
                $lookActions[$actionKey] = $action;
215
            }
216
        }
217
218 2
        if ($lookActions) {
219 2
            $lookOrder = new Order(null, $order->getCustomer(), $lookActions);
220 2
            $foundSales = $this->saleRepository->findByOrder($lookOrder);
221 2
            foreach ($foundSales as $actionKey => $sale) {
222 2
                $sales[$actionKey] = $sale;
223
            }
224
        }
225
226 2
        return $sales;
227
    }
228
}
229