Completed
Push — master ( d99711...5ac685 )
by Dmitry
03:31
created

Calculator::calculatePrice()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.5069

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 5
nop 2
dl 0
loc 20
ccs 8
cts 11
cp 0.7272
crap 5.5069
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\action\ActionInterface;
14
use hiqdev\php\billing\action\TemporaryAction;
15
use hiqdev\php\billing\charge\Charge;
16
use hiqdev\php\billing\charge\ChargeInterface;
17
use hiqdev\php\billing\charge\ChargeModifier;
18
use hiqdev\php\billing\charge\GeneralizerInterface;
19
use hiqdev\php\billing\plan\Plan;
20
use hiqdev\php\billing\plan\PlanInterface;
21
use hiqdev\php\billing\plan\PlanRepositoryInterface;
22
use hiqdev\php\billing\price\PriceInterface;
23
use hiqdev\php\billing\sale\Sale;
24
use hiqdev\php\billing\sale\SaleInterface;
25
use hiqdev\php\billing\sale\SaleRepositoryInterface;
26
27
/**
28
 * Calculator calculates charges for given order or action.
29
 *
30
 * @author Andrii Vasyliev <[email protected]>
31
 */
32
class Calculator implements CalculatorInterface
33
{
34
    /**
35
     * @var GeneralizerInterface
36
     */
37
    protected $generalizer;
38
    /**
39
     * @var SaleRepositoryInterface
40
     */
41
    private $saleRepository;
42
    /**
43
     * @var PlanRepositoryInterface
44
     */
45
    private $planRepository;
46
47
    /**
48
     * @param GeneralizerInterface $generalizer
49
     * @param SaleRepositoryInterface|null $saleRepository
50
     * @param PlanRepositoryInterface $planRepository
51
     */
52 33
    public function __construct(
53
        GeneralizerInterface $generalizer,
54
        ?SaleRepositoryInterface $saleRepository,
55
        ?PlanRepositoryInterface $planRepository
56
    ) {
57 33
        $this->generalizer    = $generalizer;
58 33
        $this->saleRepository = $saleRepository;
59 33
        $this->planRepository = $planRepository;
60 33
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 2
    public function calculateOrder(OrderInterface $order): array
66
    {
67 2
        $plans = $this->findPlans($order);
68 2
        $charges = [];
69 2
        foreach ($order->getActions() as $actionKey => $action) {
70 2
            if ($plans[$actionKey] === null) {
71
                continue;
72
            }
73
74 2
            $charges[$actionKey] = $this->calculatePlan($plans[$actionKey], $action);
75
        }
76
77 2
        return $charges;
78
    }
79
80 4
    public function calculatePlan(PlanInterface $plan, ActionInterface $action): array
81
    {
82 4
        $result = [];
83 4
        foreach ($plan->getPrices() as $price) {
0 ignored issues
show
Bug introduced by
The method getPrices() does not exist on hiqdev\php\billing\plan\PlanInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to hiqdev\php\billing\plan\PlanInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
        foreach ($plan->/** @scrutinizer ignore-call */ getPrices() as $price) {
Loading history...
84 4
            $charges = $this->calculatePrice($price, $action);
85 4
            if (!empty($charges)) {
86 4
                $result = array_merge($result, $charges);
87
            }
88
        }
89
90 4
        return $result;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 4
    public function calculatePrice(PriceInterface $price, ActionInterface $action): array
97
    {
98 4
        $charge = $this->calculateCharge($price, $action);
99 4
        if ($charge === null) {
100 4
            return [];
101
        }
102
103 4
        if ($price instanceof ChargeModifier) {
104 4
            $charges = $price->modifyCharge($charge, $action);
105
        } else {
106
            $charges = [$charge];
107
        }
108
109 4
        if ($action->isFinished()) {
110
            foreach ($charges as $charge) {
111
                $charge->setFinished();
112
            }
113
        }
114
115 4
        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
     * @param PriceInterface $price
123
     * @param ActionInterface $action
124
     * @return ChargeInterface|Charge|null
125
     */
126 25
    public function calculateCharge(PriceInterface $price, ActionInterface $action): ?ChargeInterface
127
    {
128 25
        if (!$action->isApplicable($price)) {
129 8
            return null;
130
        }
131
132 21
        $usage = $price->calculateUsage($action->getQuantity());
133 21
        if ($usage === null) {
134 4
            return null;
135
        }
136
137 17
        $sum = $price->calculateSum($action->getQuantity());
138 17
        if ($sum === null) {
139
            return null;
140
        }
141
142 17
        $type = $this->generalizer->specializeType($price->getType(), $action->getType());
143 17
        $target = $this->generalizer->specializeTarget($price->getTarget(), $action->getTarget());
144
145
        /* sorry, debugging facility
146
         * var_dump([
147
            'unit'      => $usage->getUnit()->getName(),
148
            'quantity'  => $usage->getQuantity(),
149
            'price'     => $price->calculatePrice($usage)->getAmount(),
150
            'sum'       => $sum->getAmount(),
151
        ]);*/
152
153 17
        return new Charge(null, $type, $target, $action, $price, $usage, $sum);
154
    }
155
156
    /**
157
     * @param OrderInterface $order
158
     * @return PlanInterface[]|Plan
159
     * @throws \Exception
160
     */
161 2
    public function findPlans(OrderInterface $order)
162
    {
163 2
        $sales = $this->findSales($order);
164 2
        $plans = [];
165 2
        $lookPlanIds = [];
166 2
        foreach ($order->getActions() as $actionKey => $action) {
167 2
            if ($sales[$actionKey] === false) {
168
                /// it is ok when no sale found for upper resellers
169
                $plans[$actionKey] = null;
170
            } else {
171 2
                $sale = $sales[$actionKey];
172
                /** @var Plan|PlanInterface[] $plan */
173 2
                $plan = $sale->getPlan();
174
175 2
                if ($action instanceof TemporaryAction && $plan->getId() && !$action->hasSale()) {
176
                    $action->setSale($sale);
177
                }
178
179 2
                if ($plan->hasPrices()) {
180 2
                    $plans[$actionKey] = $plan;
181
                } elseif ($plan->getId() !== null) {
182
                    $lookPlanIds[$actionKey] = $plan->getId();
183
                } else {
184 2
                    $plans[$actionKey] = null;
185
                }
186
            }
187
        }
188
189 2
        if ($lookPlanIds) {
190
            $foundPlans = $this->planRepository->findByIds($lookPlanIds);
191
            foreach ($foundPlans as $actionKey => $plan) {
192
                $foundPlans[$plan->getId()] = $plan;
193
            }
194
            foreach ($lookPlanIds as $actionKey => $planId) {
195
                if (empty($foundPlans[$planId])) {
196
                    throw new \Exception('not found plan');
197
                }
198
                $plans[$actionKey] = $foundPlans[$planId];
199
            }
200
        }
201
202 2
        return $plans;
203
    }
204
205
    /**
206
     * @param OrderInterface $order
207
     * @return SaleInterface[]|Sale
208
     */
209 2
    public function findSales(OrderInterface $order)
210
    {
211 2
        $sales = [];
212 2
        $lookActions = [];
213 2
        foreach ($order->getActions() as $actionKey => $action) {
214 2
            $sale = $action->getSale();
215 2
            if ($sale) {
216
                $sales[$actionKey] = $sale;
217
            } else {
218 2
                $lookActions[$actionKey] = $action;
219
            }
220
        }
221
222 2
        if ($lookActions) {
223 2
            $lookOrder = new Order(null, $order->getCustomer(), $lookActions);
224 2
            $foundSales = $this->saleRepository->findByOrder($lookOrder);
225 2
            foreach ($foundSales as $actionKey => $sale) {
226 2
                $sales[$actionKey] = $sale;
227
            }
228
        }
229
230 2
        return $sales;
231
    }
232
}
233