1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Service\Discount\Calculator; |
||
9 | |||
10 | use Generated\Shared\Transfer\DiscountCalculationRequestTransfer; |
||
11 | use Generated\Shared\Transfer\DiscountCalculationResponseTransfer; |
||
12 | use Generated\Shared\Transfer\DiscountTransfer; |
||
13 | use Spryker\Service\Discount\Exception\DiscountCalculatorPluginNotFoundException; |
||
14 | use Spryker\Zed\Discount\Dependency\Plugin\DiscountCalculatorPluginInterface; |
||
15 | |||
16 | class Calculator implements CalculatorInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var array<\Spryker\Zed\Discount\Dependency\Plugin\DiscountCalculatorPluginInterface> |
||
20 | */ |
||
21 | protected $discountCalculatorPlugins; |
||
22 | |||
23 | /** |
||
24 | * @param array<\Spryker\Zed\Discount\Dependency\Plugin\DiscountCalculatorPluginInterface> $discountCalculatorPlugins |
||
25 | */ |
||
26 | public function __construct(array $discountCalculatorPlugins) |
||
27 | { |
||
28 | $this->discountCalculatorPlugins = $discountCalculatorPlugins; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @param \Generated\Shared\Transfer\DiscountCalculationRequestTransfer $discountCalculationRequestTransfer |
||
33 | * |
||
34 | * @return \Generated\Shared\Transfer\DiscountCalculationResponseTransfer |
||
35 | */ |
||
36 | public function calculate( |
||
37 | DiscountCalculationRequestTransfer $discountCalculationRequestTransfer |
||
38 | ): DiscountCalculationResponseTransfer { |
||
39 | $discountCalculatorPlugin = $this->getDiscountCalculatorPlugin($discountCalculationRequestTransfer->getDiscount()); |
||
40 | |||
41 | $discountAmount = $discountCalculatorPlugin->calculateDiscount( |
||
42 | $discountCalculationRequestTransfer->getDiscountableItems()->getArrayCopy(), |
||
43 | $discountCalculationRequestTransfer->getDiscount(), |
||
44 | ); |
||
45 | |||
46 | return (new DiscountCalculationResponseTransfer())->setAmount($discountAmount); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param \Generated\Shared\Transfer\DiscountTransfer $discountTransfer |
||
51 | * |
||
52 | * @throws \Spryker\Service\Discount\Exception\DiscountCalculatorPluginNotFoundException |
||
53 | * |
||
54 | * @return \Spryker\Zed\Discount\Dependency\Plugin\DiscountCalculatorPluginInterface |
||
55 | */ |
||
56 | protected function getDiscountCalculatorPlugin(DiscountTransfer $discountTransfer): DiscountCalculatorPluginInterface |
||
57 | { |
||
58 | if (!isset($this->discountCalculatorPlugins[$discountTransfer->getCalculatorPlugin()])) { |
||
59 | throw new DiscountCalculatorPluginNotFoundException( |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
60 | sprintf( |
||
61 | 'Calculator plugin with name "%s" not found. Did you forget to register it in DiscountDependencyProvider::getAvailableCalculatorPlugins()', |
||
62 | $discountTransfer->getCalculatorPlugin(), |
||
63 | ), |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | return $this->discountCalculatorPlugins[$discountTransfer->getCalculatorPlugin()]; |
||
68 | } |
||
69 | } |
||
70 |