Passed
Pull Request — dev (#4)
by Mykyta
11:52 queued 07:39
created

mapRequestParameterGroup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 5
nc 3
nop 3
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
namespace SprykerEco\Zed\Heidelpay\Business\Adapter\Mapper;
8
9
use Heidelpay\PhpApi\Response;
10
use SprykerEco\Zed\Heidelpay\Dependency\Service\HeidelpayToUtilEncodingInterface;
11
12
class ResponsePayloadToApiResponse implements ResponsePayloadToApiResponseInterface
13
{
14
    /**
15
     * @var \SprykerEco\Zed\Heidelpay\Dependency\Service\HeidelpayToUtilEncodingInterface
16
     */
17
    protected $utilEncoding;
18
19
    /**
20
     * @param \SprykerEco\Zed\Heidelpay\Dependency\Service\HeidelpayToUtilEncodingInterface $utilEncoding
21
     */
22
    public function __construct(HeidelpayToUtilEncodingInterface $utilEncoding)
23
    {
24
        $this->utilEncoding = $utilEncoding;
25
    }
26
27
    /**
28
     * @param string $transactionPayload
29
     * @param \Heidelpay\PhpApi\Response $heidelpayResponse
30
     *
31
     * @return void
32
     */
33
    public function map($transactionPayload, Response $heidelpayResponse)
34
    {
35
        $transactionPayloadArray = $this->getTransactionPayloadArray($transactionPayload);
36
37
        foreach ($transactionPayloadArray as $parameterGroup => $values) {
38
            $this->mapRequestParameterGroup($parameterGroup, $values, $heidelpayResponse);
39
        }
40
    }
41
42
    /**
43
     * @param string $parameterGroup
44
     * @param array $values
45
     * @param \Heidelpay\PhpApi\Response $heidelpayResponse
46
     *
47
     * @return void
48
     */
49
    protected function mapRequestParameterGroup($parameterGroup, array $values, Response $heidelpayResponse)
50
    {
51
        $parameterGroupObject = $this->getRequestParameterObject($parameterGroup, $heidelpayResponse);
52
53
        if ($parameterGroupObject === null) {
54
            return;
55
        }
56
57
        foreach ($values as $fieldName => $fieldValue) {
58
            $this->setParameterGroupProperty($parameterGroupObject, $fieldName, $fieldValue);
59
        }
60
    }
61
62
    /**
63
     * @param string $parameterGroup
64
     * @param \Heidelpay\PhpApi\Response $heidelpayResponse
65
     *
66
     * @return object|null
67
     */
68
    protected function getRequestParameterObject($parameterGroup, Response $heidelpayResponse)
69
    {
70
        $getParameterMethod = 'get' . ucfirst($parameterGroup);
71
72
        if (!method_exists($heidelpayResponse, $getParameterMethod)) {
73
            return null;
74
        }
75
76
        return $heidelpayResponse->$getParameterMethod();
77
    }
78
79
    /**
80
     * @param object $parameterGroupObject
81
     * @param string $fieldName
82
     * @param mixed $fieldValue
83
     *
84
     * @return void
85
     */
86
    protected function setParameterGroupProperty($parameterGroupObject, $fieldName, $fieldValue)
87
    {
88
        if (property_exists($parameterGroupObject, $fieldName)) {
89
            $parameterGroupObject->$fieldName = $fieldValue;
90
        }
91
    }
92
93
    /**
94
     * @param string $transactionPayload
95
     *
96
     * @return array
97
     */
98
    protected function getTransactionPayloadArray($transactionPayload)
99
    {
100
        return (array)$this->utilEncoding->decodeJson($transactionPayload, true);
101
    }
102
}
103