SuccessResponseProcessor   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 36
c 2
b 0
f 0
dl 0
loc 112
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addEasycreditExpense() 0 11 1
A isEasycreditExpenseAdded() 0 11 3
A addEasycreditSummaryInfo() 0 11 1
A __construct() 0 10 1
A process() 0 13 2
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Yves\Easycredit\Processor;
9
10
use Generated\Shared\Transfer\ExpenseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ExpenseTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface;
13
use SprykerEco\Client\Easycredit\EasycreditClientInterface;
14
use SprykerEco\Shared\Easycredit\EasycreditConstants;
15
use SprykerEco\Yves\Easycredit\Dependency\Client\EasycreditToCalculationClientInterface;
16
use SprykerEco\Yves\Easycredit\Dependency\Client\EasycreditToQuoteClientInterface;
17
18
class SuccessResponseProcessor implements SuccessResponseProcessorInterface
19
{
20
    public const EXPENSE_TYPE_EASYCREDIT = 'Easycredit';
21
22
    /**
23
     * @var \SprykerEco\Yves\Easycredit\Dependency\Client\EasycreditToQuoteClientInterface
24
     */
25
    protected $quoteClient;
26
27
    /**
28
     * @var \SprykerEco\Yves\Easycredit\Dependency\Client\EasycreditToCalculationClientInterface
29
     */
30
    protected $calculationClient;
31
32
    /**
33
     * @var \SprykerEco\Client\Easycredit\EasycreditClientInterface
34
     */
35
    protected $easycreditClient;
36
37
    /**
38
     * @var \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface
39
     */
40
    protected $moneyPlugin;
41
42
    /**
43
     * @param \SprykerEco\Yves\Easycredit\Dependency\Client\EasycreditToQuoteClientInterface $quoteClient
44
     * @param \SprykerEco\Yves\Easycredit\Dependency\Client\EasycreditToCalculationClientInterface $calculationClient
45
     * @param \SprykerEco\Client\Easycredit\EasycreditClientInterface $easycreditClient
46
     * @param \Spryker\Shared\Money\Dependency\Plugin\MoneyPluginInterface $moneyPlugin
47
     */
48
    public function __construct(
49
        EasycreditToQuoteClientInterface $quoteClient,
50
        EasycreditToCalculationClientInterface $calculationClient,
51
        EasycreditClientInterface $easycreditClient,
52
        MoneyPluginInterface $moneyPlugin
53
    ) {
54
        $this->quoteClient = $quoteClient;
55
        $this->calculationClient = $calculationClient;
56
        $this->easycreditClient = $easycreditClient;
57
        $this->moneyPlugin = $moneyPlugin;
58
    }
59
60
    /**
61
     * @return \Generated\Shared\Transfer\QuoteTransfer
62
     */
63
    public function process(): QuoteTransfer
64
    {
65
        $quoteTransfer = $this->quoteClient->getQuote();
66
67
        if (!$this->isEasycreditExpenseAdded($quoteTransfer)) {
68
            $quoteTransfer = $this->addEasycreditSummaryInfo($quoteTransfer);
69
            $quoteTransfer = $this->addEasycreditExpense($quoteTransfer);
70
            $quoteTransfer = $this->calculationClient->recalculate($quoteTransfer);
71
72
            $this->quoteClient->setQuote($quoteTransfer);
73
        }
74
75
        return $quoteTransfer;
76
    }
77
78
    /**
79
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
80
     *
81
     * @return \Generated\Shared\Transfer\QuoteTransfer
82
     */
83
    protected function addEasycreditExpense(QuoteTransfer $quoteTransfer): QuoteTransfer
84
    {
85
        $expenseTransfer = new ExpenseTransfer();
86
        $expenseTransfer->setType(EasycreditConstants::EASYCREDIT_EXPENSE_TYPE);
87
        $expenseTransfer->setUnitNetPrice(0);
88
        $expenseTransfer->setUnitGrossPrice($this->moneyPlugin->convertDecimalToInteger($quoteTransfer->getPayment()->getEasycredit()->getAnfallendeZinsen()));
89
        $expenseTransfer->setQuantity(1);
90
91
        $quoteTransfer->addExpense($expenseTransfer);
92
93
        return $quoteTransfer;
94
    }
95
96
    /**
97
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
98
     *
99
     * @return \Generated\Shared\Transfer\QuoteTransfer
100
     */
101
    protected function addEasycreditSummaryInfo(QuoteTransfer $quoteTransfer): QuoteTransfer
102
    {
103
        $easycreditContractualInformationAndRedemptionPlanResponseTransfer = $this->easycreditClient->sendPreContractualInformationAndRedemptionPlanRequest($quoteTransfer);
104
        $easycreditInterestAndAdjustTotalSumResponseTransfer = $this->easycreditClient->sendInterestAndTotalSumRequest($quoteTransfer);
105
106
        $quoteTransfer->getPayment()->getEasycredit()
107
            ->setUrlVorvertraglicheInformationen($easycreditContractualInformationAndRedemptionPlanResponseTransfer->getUrlVorvertraglicheInformationen())
108
            ->setTilgungsplanText($easycreditInterestAndAdjustTotalSumResponseTransfer->getTilgungsplanText())
109
            ->setAnfallendeZinsen($easycreditInterestAndAdjustTotalSumResponseTransfer->getAnfallendeZinsen());
110
111
        return $quoteTransfer;
112
    }
113
114
    /**
115
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
116
     *
117
     * @return bool
118
     */
119
    protected function isEasycreditExpenseAdded(QuoteTransfer $quoteTransfer): bool
120
    {
121
        $expenses = $quoteTransfer->getExpenses();
122
123
        foreach ($expenses as $expense) {
124
            if ($expense->getType() === EasycreditConstants::EASYCREDIT_EXPENSE_TYPE) {
125
                return true;
126
            }
127
        }
128
129
        return false;
130
    }
131
}
132