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

AbstractRequestBuilder::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\CrefoPayApiConfig;
14
use SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface;
15
16
abstract class AbstractRequestBuilder implements CrefoPayApiRequestBuilderInterface
17
{
18
    /**
19
     * @var \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface
20
     */
21
    protected $encodingService;
22
23
    /**
24
     * @var \SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface
25
     */
26
    protected $service;
27
28
    /**
29
     * @var \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig
30
     */
31
    protected $config;
32
33
    /**
34
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
35
     *
36
     * @return array
37
     */
38
    abstract protected function convertRequestTransferToArray(CrefoPayApiRequestTransfer $requestTransfer): array;
39
40
    /**
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
        CrefoPayApiToUtilEncodingServiceInterface $encodingService,
47
        CrefoPayApiServiceInterface $service,
48
        CrefoPayApiConfig $config
49
    ) {
50
        $this->encodingService = $encodingService;
51
        $this->service = $service;
52
        $this->config = $config;
53
    }
54
55
    /**
56
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
57
     *
58
     * @return array
59
     */
60
    public function buildRequestPayload(CrefoPayApiRequestTransfer $requestTransfer): array
61
    {
62
        $requestPayload = $this->convertRequestTransferToArray($requestTransfer);
63
        $requestPayload = $this->removeRedundantParams($requestPayload);
64
        $requestPayload = $this->convertNestedArrayToJson($requestPayload);
65
        $requestPayload[$this->config->getApiFieldMac()] = $this->service->calculateMac($requestPayload);
66
67
        return $requestPayload;
68
    }
69
70
    /**
71
     * @param array $data
72
     *
73
     * @return array
74
     */
75
    protected function removeRedundantParams(array $data): array
76
    {
77
        $data = array_filter($data, function ($item) {
78
            if ($item instanceof ArrayObject) {
79
                return $item->count() !== 0;
80
            }
81
            return $item !== null;
82
        });
83
84
        foreach ($data as $key => $value) {
85
            if (is_array($value) || $value instanceof ArrayObject) {
86
                $data[$key] = $this->removeRedundantParams((array)$value);
87
            }
88
        }
89
90
        return $data;
91
    }
92
93
    /**
94
     * @param array $data
95
     *
96
     * @return array
97
     */
98
    protected function convertNestedArrayToJson(array $data): array
99
    {
100
        foreach ($data as $key => $value) {
101
            if (is_array($value) || $value instanceof ArrayObject) {
102
                $data[$key] = $this->encodingService->encodeJson($value);
103
            }
104
        }
105
106
        return $data;
107
    }
108
}
109