FirstDataRequestBuilder::removeRedundantParams()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 17
rs 9.6111
cc 5
nc 3
nop 1
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\FirstData\Business\Api\Request\Builder;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\FirstDataApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...tDataApiRequestTransfer 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 Generated\Shared\Transfer\FirstDataHttpRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...DataHttpRequestTransfer 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...
13
use Ramsey\Uuid\Uuid;
14
use SprykerEco\Zed\FirstData\Business\Api\Generator\HashGeneratorInterface;
15
use SprykerEco\Zed\FirstData\Dependency\Service\FirstDataToUtilEncodingServiceInterface;
16
use SprykerEco\Zed\FirstData\FirstDataConfig;
17
18
class FirstDataRequestBuilder implements FirstDataRequestBuilderInterface
19
{
20
    protected const REQUEST_KEY_REQUEST_TYPE = 'requestType';
21
22
    /**
23
     * @var \SprykerEco\Zed\FirstData\Business\Api\Request\Converter\FirstDataRequestConverterInterface[]
24
     */
25
    protected $firstDataRequestConverters;
26
27
    /**
28
     * @var \SprykerEco\Zed\FirstData\Dependency\Service\FirstDataToUtilEncodingServiceInterface
29
     */
30
    protected $utilEncodingService;
31
32
    /**
33
     * @var \SprykerEco\Zed\FirstData\FirstDataConfig
34
     */
35
    protected $firstDataConfig;
36
37
    /**
38
     * @var \SprykerEco\Zed\FirstData\Business\Api\Generator\HashGeneratorInterface
39
     */
40
    protected $hashGenerator;
41
42
    /**
43
     * @param \SprykerEco\Zed\FirstData\Business\Api\Request\Converter\FirstDataRequestConverterInterface[] $firstDataRequestConverters
44
     * @param \SprykerEco\Zed\FirstData\Dependency\Service\FirstDataToUtilEncodingServiceInterface $utilEncodingService
45
     * @param \SprykerEco\Zed\FirstData\FirstDataConfig $firstDataConfig
46
     * @param \SprykerEco\Zed\FirstData\Business\Api\Generator\HashGeneratorInterface $hashGenerator
47
     */
48
    public function __construct(
49
        array $firstDataRequestConverters,
50
        FirstDataToUtilEncodingServiceInterface $utilEncodingService,
51
        FirstDataConfig $firstDataConfig,
52
        HashGeneratorInterface $hashGenerator
53
    ) {
54
        $this->firstDataRequestConverters = $firstDataRequestConverters;
55
        $this->utilEncodingService = $utilEncodingService;
56
        $this->firstDataConfig = $firstDataConfig;
57
        $this->hashGenerator = $hashGenerator;
58
    }
59
60
    /**
61
     * @param \Generated\Shared\Transfer\FirstDataApiRequestTransfer $firstDataApiRequestTransfer
62
     *
63
     * @return \Generated\Shared\Transfer\FirstDataHttpRequestTransfer
64
     */
65
    public function buildRequest(FirstDataApiRequestTransfer $firstDataApiRequestTransfer): FirstDataHttpRequestTransfer
66
    {
67
        $requestPayload = $this->getRequestPayload($firstDataApiRequestTransfer);
68
69
        return (new FirstDataHttpRequestTransfer())
70
            ->setBody($requestPayload)
71
            ->setHeaders($this->buildRequestHeaders($requestPayload));
72
    }
73
74
    /**
75
     * @param \Generated\Shared\Transfer\FirstDataApiRequestTransfer $firstDataApiRequestTransfer
76
     *
77
     * @return string
78
     */
79
    protected function getRequestPayload(FirstDataApiRequestTransfer $firstDataApiRequestTransfer): string
80
    {
81
        $requestPayload = [];
82
        $requestPayload = $this->executeFirstDataRequestConverter($requestPayload, $firstDataApiRequestTransfer);
83
        $requestPayload[static::REQUEST_KEY_REQUEST_TYPE] = $firstDataApiRequestTransfer->getRequestType();
84
85
        return $this->utilEncodingService->encodeJson($requestPayload) ?? '';
86
    }
87
88
    /**
89
     * @param array $requestPayload
90
     * @param \Generated\Shared\Transfer\FirstDataApiRequestTransfer $firstDataApiRequestTransfer
91
     *
92
     * @return array
93
     */
94
    protected function executeFirstDataRequestConverter(array $requestPayload, FirstDataApiRequestTransfer $firstDataApiRequestTransfer): array
95
    {
96
        foreach ($this->firstDataRequestConverters as $firstDataRequestConverter) {
97
            if ($firstDataRequestConverter->isApplicable($firstDataApiRequestTransfer)) {
98
                $requestPayload = $firstDataRequestConverter->convertRequestTransferToArray($firstDataApiRequestTransfer);
99
                $requestPayload = $this->removeRedundantParams($requestPayload);
100
101
                break;
102
            }
103
        }
104
105
        return $requestPayload;
106
    }
107
108
    /**
109
     * @param string $requestPayload
110
     *
111
     * @return array
112
     */
113
    protected function buildRequestHeaders(string $requestPayload): array
114
    {
115
        $clientRequestId = Uuid::uuid4()->toString();
116
        $timestamp = time() * 1000;
117
118
        return [
119
            'Client-Request-Id' => $clientRequestId,
120
            'Api-Key' => $this->firstDataConfig->getFirstDataApiKey(),
121
            'Timestamp' => $timestamp,
122
            'Message-Signature' => $this->hashGenerator->generateHash([
123
                $this->firstDataConfig->getFirstDataApiKey(),
124
                $clientRequestId,
125
                $timestamp,
126
                $requestPayload,
127
            ]),
128
        ];
129
    }
130
131
    /**
132
     * @param array $data
133
     *
134
     * @return array
135
     */
136
    protected function removeRedundantParams(array $data): array
137
    {
138
        $data = array_filter($data, function ($item) {
139
            if ($item instanceof ArrayObject) {
140
                return $item->count() !== 0;
141
            }
142
143
            return $item !== null;
144
        });
145
146
        foreach ($data as $key => $value) {
147
            if (is_array($value) || $value instanceof ArrayObject) {
148
                $data[$key] = $this->removeRedundantParams((array)$value);
149
            }
150
        }
151
152
        return $data;
153
    }
154
}
155