EasyCreditStatusHandler   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 13
eloc 53
c 3
b 0
f 1
dl 0
loc 151
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A parseStatusExplanation() 0 3 1
A addExpense() 0 20 3
A parseStatusValue() 0 7 2
A __construct() 0 8 1
A parseExpense() 0 9 2
A parsePaymentAmount() 0 9 2
A handle() 0 25 2
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Computop\Business\Payment\Handler\PrePlace;
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 SprykerEco\Shared\Computop\ComputopConfig;
13
use SprykerEco\Zed\Computop\Business\Payment\Handler\Logger\ComputopResponseLoggerInterface;
14
use SprykerEco\Zed\Computop\Dependency\Facade\ComputopToComputopApiFacadeInterface;
15
use SprykerEco\Zed\Computop\Dependency\Facade\ComputopToMoneyFacadeInterface;
16
17
class EasyCreditStatusHandler extends AbstractHandler
18
{
19
    protected const PLAN = 'ratenplan';
20
    protected const PAY_RENT = 'zinsen';
21
    protected const ACCRUED_INTEREST = 'anfallendeZinsen';
22
    protected const TOTAL = 'gesamtsumme';
23
    protected const DECISION = 'entscheidung';
24
    protected const DECISION_RESULT = 'entscheidungsergebnis';
25
    protected const DECISION_RESULT_GREEN = 'GRUEN';
26
27
    protected const COMPUTOP_EASY_CREDIT_EXPENSE_TYPE = 'COMPUTOP_EASY_CREDIT_EXPENSE_TYPE';
28
29
    /**
30
     * @var \SprykerEco\Zed\Computop\Dependency\Facade\ComputopToMoneyFacadeInterface
31
     */
32
    protected $moneyFacade;
33
34
    /**
35
     * @var \SprykerEco\Zed\Computop\Business\Payment\Handler\Logger\ComputopResponseLoggerInterface
36
     */
37
    protected $logger;
38
39
    /**
40
     * @param \SprykerEco\Zed\Computop\Dependency\Facade\ComputopToComputopApiFacadeInterface $computopApiFacade
41
     * @param \SprykerEco\Zed\Computop\Dependency\Facade\ComputopToMoneyFacadeInterface $moneyFacade
42
     * @param \SprykerEco\Zed\Computop\Business\Payment\Handler\Logger\ComputopResponseLoggerInterface $logger
43
     */
44
    public function __construct(
45
        ComputopToComputopApiFacadeInterface $computopApiFacade,
46
        ComputopToMoneyFacadeInterface $moneyFacade,
47
        ComputopResponseLoggerInterface $logger
48
    ) {
49
        parent::__construct($computopApiFacade);
50
        $this->moneyFacade = $moneyFacade;
51
        $this->logger = $logger;
52
    }
53
54
    /**
55
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
56
     *
57
     * @return \Generated\Shared\Transfer\QuoteTransfer
58
     */
59
    public function handle(QuoteTransfer $quoteTransfer): QuoteTransfer
60
    {
61
        $computopHeaderPayment = $this->createComputopHeaderPayment($quoteTransfer);
62
63
        $responseTransfer = $this
64
            ->computopApiFacade
65
            ->performEasyCreditStatusRequest($quoteTransfer, $computopHeaderPayment);
66
67
        if ($responseTransfer->getHeader()->getIsSuccess()) {
68
            $decision = $this->parseStatusExplanation($responseTransfer->getDecision());
69
            $financing = $this->parseStatusExplanation($responseTransfer->getFinancing());
70
            $process = $this->parseStatusExplanation($responseTransfer->getProcess());
71
72
            $responseTransfer->setDecisionData($decision);
73
            $responseTransfer->setFinancingData($financing);
74
            $responseTransfer->setProcessData($process);
75
76
            $quoteTransfer = $this->addExpense($quoteTransfer, $financing);
77
        }
78
79
        $quoteTransfer->getPayment()->getComputopEasyCredit()->setEasyCreditStatusResponse($responseTransfer);
80
81
        $this->logger->log($responseTransfer->getHeader(), $responseTransfer->getHeader()->getMethod());
82
83
        return $quoteTransfer;
84
    }
85
86
    /**
87
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
88
     * @param string[] $financing
89
     *
90
     * @return \Generated\Shared\Transfer\QuoteTransfer
91
     */
92
    protected function addExpense(QuoteTransfer $quoteTransfer, array $financing): QuoteTransfer
93
    {
94
        $expenses = $quoteTransfer->getExpenses();
95
96
        foreach ($expenses as $index => $expenseTransfer) {
97
            if ($expenseTransfer->getType() === static::COMPUTOP_EASY_CREDIT_EXPENSE_TYPE) {
98
                $expenses->offsetUnset($index);
99
            }
100
        }
101
102
        $expense = (new ExpenseTransfer())
103
            ->setType(static::COMPUTOP_EASY_CREDIT_EXPENSE_TYPE)
104
            ->setName(ComputopConfig::PAYMENT_METHOD_EASY_CREDIT)
105
            ->setUnitGrossPrice($this->parseExpense($financing))
106
            ->setQuantity(1);
107
108
        $expenses->append($expense);
109
        $quoteTransfer->setExpenses($expenses);
110
111
        return $quoteTransfer;
112
    }
113
114
    /**
115
     * @param array $financing
116
     *
117
     * @return int
118
     */
119
    protected function parseExpense(array $financing): int
120
    {
121
        if (!isset($financing[static::PLAN][static::PAY_RENT][static::ACCRUED_INTEREST])) {
122
            return 0;
123
        }
124
125
        $expense = (float)$financing[static::PLAN][static::PAY_RENT][static::ACCRUED_INTEREST];
126
127
        return $this->moneyFacade->convertDecimalToInteger($expense);
128
    }
129
130
    /**
131
     * @param string $status
132
     *
133
     * @return array
134
     */
135
    protected function parseStatusExplanation($status)
136
    {
137
        return json_decode(base64_decode($status), true);
138
    }
139
140
    /**
141
     * @param array $decision
142
     *
143
     * @return bool
144
     */
145
    protected function parseStatusValue($decision): bool
146
    {
147
        if (isset($decision[static::DECISION][static::DECISION_RESULT])) {
148
            return $decision[static::DECISION][static::DECISION_RESULT] === static::DECISION_RESULT_GREEN;
149
        }
150
151
        return false;
152
    }
153
154
    /**
155
     * @param array $financing
156
     *
157
     * @return int
158
     */
159
    protected function parsePaymentAmount($financing): int
160
    {
161
        if (!isset($financing[static::PLAN][static::TOTAL])) {
162
            return 0;
163
        }
164
165
        $paymentAmount = (float)$financing[static::PLAN][static::TOTAL];
166
167
        return $this->moneyFacade->convertDecimalToInteger($paymentAmount);
168
    }
169
}
170