Completed
Pull Request — master (#3)
by Aleksey
15:31 queued 12:58
created

CrefoPayApiRequestBuilder::removeRedundantParams()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 3
nop 1
dl 0
loc 16
rs 9.6111
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
            return $item !== null;
77
        });
78
79
        foreach ($data as $key => $value) {
80
            if (is_array($value) || $value instanceof ArrayObject) {
81
                $data[$key] = $this->removeRedundantParams((array)$value);
82
            }
83
        }
84
85
        return $data;
86
    }
87
88
    /**
89
     * @param array $data
90
     *
91
     * @return array
92
     */
93
    protected function convertNestedArrayToJson(array $data): array
94
    {
95
        foreach ($data as $key => $value) {
96
            if (is_array($value) || $value instanceof ArrayObject) {
97
                $data[$key] = $this->encodingService->encodeJson($value);
98
            }
99
        }
100
101
        return $data;
102
    }
103
104
    /**
105
     * @param array $requestPayload
106
     *
107
     * @return array
108
     */
109
    protected function addMacToRequestPayload(array $requestPayload): array
110
    {
111
        $requestPayload[CrefoPayApiRequestFields::API_FIELD_MAC] = $this->crefoPayApiService->calculateMac($requestPayload);
112
113
        return $requestPayload;
114
    }
115
}
116