ResponsePayloadToApiResponse   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setParameterGroupProperty() 0 4 2
A __construct() 0 3 1
A mapRequestParameterGroup() 0 10 3
A map() 0 6 2
A getTransactionPayloadArray() 0 3 1
A getRequestParameterObject() 0 9 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\Zed\Heidelpay\Business\Adapter\Mapper;
9
10
use Heidelpay\PhpPaymentApi\Response;
11
use SprykerEco\Zed\Heidelpay\Dependency\Service\HeidelpayToUtilEncodingServiceInterface;
12
13
class ResponsePayloadToApiResponse implements ResponsePayloadToApiResponseInterface
14
{
15
    /**
16
     * @var \SprykerEco\Zed\Heidelpay\Dependency\Service\HeidelpayToUtilEncodingServiceInterface
17
     */
18
    protected $utilEncoding;
19
20
    /**
21
     * @param \SprykerEco\Zed\Heidelpay\Dependency\Service\HeidelpayToUtilEncodingServiceInterface $utilEncoding
22
     */
23
    public function __construct(HeidelpayToUtilEncodingServiceInterface $utilEncoding)
24
    {
25
        $this->utilEncoding = $utilEncoding;
26
    }
27
28
    /**
29
     * @param string $transactionPayload
30
     * @param \Heidelpay\PhpPaymentApi\Response $heidelpayResponse
31
     *
32
     * @return void
33
     */
34
    public function map($transactionPayload, Response $heidelpayResponse): void
35
    {
36
        $transactionPayloadArray = $this->getTransactionPayloadArray($transactionPayload);
37
38
        foreach ($transactionPayloadArray as $parameterGroup => $values) {
39
            $this->mapRequestParameterGroup($parameterGroup, $values, $heidelpayResponse);
40
        }
41
    }
42
43
    /**
44
     * @param string $parameterGroup
45
     * @param array $values
46
     * @param \Heidelpay\PhpPaymentApi\Response $heidelpayResponse
47
     *
48
     * @return void
49
     */
50
    protected function mapRequestParameterGroup($parameterGroup, array $values, Response $heidelpayResponse): void
51
    {
52
        $parameterGroupObject = $this->getRequestParameterObject($parameterGroup, $heidelpayResponse);
53
54
        if ($parameterGroupObject === null) {
55
            return;
56
        }
57
58
        foreach ($values as $fieldName => $fieldValue) {
59
            $this->setParameterGroupProperty($parameterGroupObject, $fieldName, $fieldValue);
60
        }
61
    }
62
63
    /**
64
     * @param string $parameterGroup
65
     * @param \Heidelpay\PhpPaymentApi\Response $heidelpayResponse
66
     *
67
     * @return mixed
68
     */
69
    protected function getRequestParameterObject($parameterGroup, Response $heidelpayResponse)
70
    {
71
        $getParameterMethod = 'get' . ucfirst($parameterGroup);
72
73
        if (!method_exists($heidelpayResponse, $getParameterMethod)) {
74
            return null;
75
        }
76
77
        return $heidelpayResponse->$getParameterMethod();
78
    }
79
80
    /**
81
     * @param object $parameterGroupObject
82
     * @param string $fieldName
83
     * @param mixed $fieldValue
84
     *
85
     * @return void
86
     */
87
    protected function setParameterGroupProperty($parameterGroupObject, $fieldName, $fieldValue): void
88
    {
89
        if (property_exists($parameterGroupObject, $fieldName)) {
90
            $parameterGroupObject->$fieldName = $fieldValue;
91
        }
92
    }
93
94
    /**
95
     * @param string $transactionPayload
96
     *
97
     * @return array
98
     */
99
    protected function getTransactionPayloadArray($transactionPayload): array
100
    {
101
        return (array)$this->utilEncoding->decodeJson($transactionPayload, true);
102
    }
103
}
104