Passed
Pull Request — feature/eco-2295/dev (#1)
by Aleksey
02:03 queued 01:01
created

AbstractRequestBuilder::removeRedundantParams()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
rs 9.6111
c 0
b 0
f 0
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\CrefoPayApi\Business\Builder\Request;
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\Shared\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
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
30
     *
31
     * @return array
32
     */
33
    abstract protected function convertRequestTransferToArray(CrefoPayApiRequestTransfer $requestTransfer): array;
34
35
    /**
36
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface $encodingService
37
     * @param \SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface $service
38
     */
39
    public function __construct(
40
        CrefoPayApiToUtilEncodingServiceInterface $encodingService,
41
        CrefoPayApiServiceInterface $service
42
    ) {
43
        $this->encodingService = $encodingService;
44
        $this->service = $service;
45
    }
46
47
    /**
48
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
49
     *
50
     * @return array
51
     */
52
    public function buildRequestPayload(CrefoPayApiRequestTransfer $requestTransfer): array
53
    {
54
        $requestPayload = $this->convertRequestTransferToArray($requestTransfer);
55
        $requestPayload = $this->removeRedundantParams($requestPayload);
56
        $requestPayload = $this->convertNestedArrayToJson($requestPayload);
57
        $requestPayload[CrefoPayApiConfig::API_FIELD_MAC] = $this->service->calculateMac($requestPayload);
58
59
        return $requestPayload;
60
    }
61
62
    /**
63
     * @param array $data
64
     *
65
     * @return array
66
     */
67
    protected function removeRedundantParams(array $data): array
68
    {
69
        $data = array_filter($data, function ($item) {
70
            if ($item instanceof ArrayObject) {
71
                return $item->count() !== 0;
72
            }
73
            return $item !== null;
74
        });
75
76
        foreach ($data as $key => $value) {
77
            if (is_array($value) || $value instanceof ArrayObject) {
78
                $data[$key] = $this->removeRedundantParams($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type ArrayObject; however, parameter $data of SprykerEco\Zed\CrefoPayA...removeRedundantParams() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
                $data[$key] = $this->removeRedundantParams(/** @scrutinizer ignore-type */ $value);
Loading history...
79
            }
80
        }
81
82
        return $data;
83
    }
84
85
    /**
86
     * @param array $data
87
     *
88
     * @return array
89
     */
90
    protected function convertNestedArrayToJson(array $data): array
91
    {
92
        foreach ($data as $key => $value) {
93
            if (is_array($value) || $value instanceof ArrayObject) {
94
                $data[$key] = $this->encodingService->encodeJson($value);
95
            }
96
        }
97
98
        return $data;
99
    }
100
}
101