CrefoPayApiRequestBuilder::buildRequestPayload()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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\CrefoPayApi\Business\Request\Builder;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\CrefoPayApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayApiRequestTransfer 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\Service\CrefoPayApi\CrefoPayApiServiceInterface;
13
use SprykerEco\Zed\CrefoPayApi\Business\Request\Converter\CrefoPayApiRequestConverterInterface;
14
use SprykerEco\Zed\CrefoPayApi\Business\Request\CrefoPayApiRequestFields;
15
use SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface;
16
17
class CrefoPayApiRequestBuilder implements CrefoPayApiRequestBuilderInterface
18
{
19
    /**
20
     * @var \SprykerEco\Zed\CrefoPayApi\Business\Request\Converter\CrefoPayApiRequestConverterInterface
21
     */
22
    protected $requestConverter;
23
24
    /**
25
     * @var \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface
26
     */
27
    protected $encodingService;
28
29
    /**
30
     * @var \SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface
31
     */
32
    protected $crefoPayApiService;
33
34
    /**
35
     * @param \SprykerEco\Zed\CrefoPayApi\Business\Request\Converter\CrefoPayApiRequestConverterInterface $requestConverter
36
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface $encodingService
37
     * @param \SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface $crefoPayApiService
38
     */
39
    public function __construct(
40
        CrefoPayApiRequestConverterInterface $requestConverter,
41
        CrefoPayApiToUtilEncodingServiceInterface $encodingService,
42
        CrefoPayApiServiceInterface $crefoPayApiService
43
    ) {
44
        $this->requestConverter = $requestConverter;
45
        $this->encodingService = $encodingService;
46
        $this->crefoPayApiService = $crefoPayApiService;
47
    }
48
49
    /**
50
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
51
     *
52
     * @return array
53
     */
54
    public function buildRequestPayload(CrefoPayApiRequestTransfer $requestTransfer): array
55
    {
56
        $requestPayload = $this->requestConverter
57
            ->convertRequestTransferToArray($requestTransfer);
58
        $requestPayload = $this->removeRedundantParams($requestPayload);
59
        $requestPayload = $this->convertNestedArrayToJson($requestPayload);
60
        $requestPayload = $this->addMacToRequestPayload($requestPayload);
61
62
        return $requestPayload;
63
    }
64
65
    /**
66
     * @param array $data
67
     *
68
     * @return array
69
     */
70
    protected function removeRedundantParams(array $data): array
71
    {
72
        $data = array_filter($data, function ($item) {
73
            if ($item instanceof ArrayObject) {
74
                return $item->count() !== 0;
75
            }
76
77
            return $item !== null;
78
        });
79
80
        foreach ($data as $key => $value) {
81
            if (is_array($value) || $value instanceof ArrayObject) {
82
                $data[$key] = $this->removeRedundantParams((array)$value);
83
            }
84
        }
85
86
        return $data;
87
    }
88
89
    /**
90
     * @param array $data
91
     *
92
     * @return array
93
     */
94
    protected function convertNestedArrayToJson(array $data): array
95
    {
96
        foreach ($data as $key => $value) {
97
            if (is_array($value) || $value instanceof ArrayObject) {
98
                $data[$key] = $this->encodingService->encodeJson($value);
99
            }
100
        }
101
102
        return $data;
103
    }
104
105
    /**
106
     * @param array $requestPayload
107
     *
108
     * @return array
109
     */
110
    protected function addMacToRequestPayload(array $requestPayload): array
111
    {
112
        $requestPayload[CrefoPayApiRequestFields::API_FIELD_MAC] = $this->crefoPayApiService->calculateMac($requestPayload);
113
114
        return $requestPayload;
115
    }
116
}
117