Calculator   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Test Coverage

Coverage 79.27%

Importance

Changes 10
Bugs 1 Features 1
Metric Value
eloc 91
dl 0
loc 197
ccs 65
cts 82
cp 0.7927
rs 9.44
c 10
b 1
f 1
wmc 37

7 Methods

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