Passed
Push — feature/eco-2295/eco-2344-crea... ( 0c1e08...b58615 )
by Aleksey
01:17
created

convertNestedArrayToJson()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 3
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\CrefoPayApiConfig;
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 $service;
33
34
    /**
35
     * @var \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig
36
     */
37
    protected $config;
38
39
    /**
40
     * @param \SprykerEco\Zed\CrefoPayApi\Business\Request\Converter\CrefoPayApiRequestConverterInterface $requestConverter
41
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface $encodingService
42
     * @param \SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface $service
43
     * @param \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig $config
44
     */
45
    public function __construct(
46
        CrefoPayApiRequestConverterInterface $requestConverter,
47
        CrefoPayApiToUtilEncodingServiceInterface $encodingService,
48
        CrefoPayApiServiceInterface $service,
49
        CrefoPayApiConfig $config
50
    ) {
51
        $this->requestConverter = $requestConverter;
52
        $this->encodingService = $encodingService;
53
        $this->service = $service;
54
        $this->config = $config;
55
    }
56
57
    /**
58
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
59
     *
60
     * @return array
61
     */
62
    public function buildRequestPayload(CrefoPayApiRequestTransfer $requestTransfer): array
63
    {
64
        $requestPayload = $this->requestConverter
65
            ->convertRequestTransferToArray($requestTransfer);
66
        $requestPayload = $this->removeRedundantParams($requestPayload);
67
        $requestPayload = $this->convertNestedArrayToJson($requestPayload);
68
        $requestPayload[$this->config->getApiFieldMac()] = $this->service->calculateMac($requestPayload);
69
70
        return $requestPayload;
71
    }
72
73
    /**
74
     * @param array $data
75
     *
76
     * @return array
77
     */
78
    protected function removeRedundantParams(array $data): array
79
    {
80
        $data = array_filter($data, function ($item) {
81
            if ($item instanceof ArrayObject) {
82
                return $item->count() !== 0;
83
            }
84
            return $item !== null;
85
        });
86
87
        foreach ($data as $key => $value) {
88
            if (is_array($value) || $value instanceof ArrayObject) {
89
                $data[$key] = $this->removeRedundantParams((array)$value);
90
            }
91
        }
92
93
        return $data;
94
    }
95
96
    /**
97
     * @param array $data
98
     *
99
     * @return array
100
     */
101
    protected function convertNestedArrayToJson(array $data): array
102
    {
103
        foreach ($data as $key => $value) {
104
            if (is_array($value) || $value instanceof ArrayObject) {
105
                $data[$key] = $this->encodingService->encodeJson($value);
106
            }
107
        }
108
109
        return $data;
110
    }
111
}
112