Issues (3877)

Spryker/Service/Discount/Calculator/Calculator.php (1 issue)

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
The class Spryker\Service\Discount...PluginNotFoundException has been deprecated: Exists for BC reasons. Will be removed in the next major release. ( Ignorable by Annotation )

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

59
            throw /** @scrutinizer ignore-deprecated */ new DiscountCalculatorPluginNotFoundException(
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