Passed
Push — feature/eco-2295/eco-2344-crea... ( cd4fd8...13938b )
by Aleksey
01:14
created

AbstractRequestBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 25
dl 0
loc 92
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A removeRedundantParams() 0 16 5
A buildRequestPayload() 0 9 1
A convertNestedArrayToJson() 0 9 4
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\Zed\CrefoPayApi\Business\Validator\Request\CrefoPayApiRequestValidatorInterface;
14
use SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface;
15
16
abstract class AbstractRequestBuilder implements CrefoPayApiRequestBuilderInterface
17
{
18
    /**
19
     * @var \SprykerEco\Zed\CrefoPayApi\Business\Validator\Request\CrefoPayApiRequestValidatorInterface
20
     */
21
    protected $validator;
22
23
    /**
24
     * @var \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface
25
     */
26
    protected $encodingService;
27
28
    /**
29
     * @var \SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface
30
     */
31
    protected $service;
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\Business\Validator\Request\CrefoPayApiRequestValidatorInterface $validator
42
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface $encodingService
43
     * @param \SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface $service
44
     */
45
    public function __construct(
46
        CrefoPayApiRequestValidatorInterface $validator,
47
        CrefoPayApiToUtilEncodingServiceInterface $encodingService,
48
        CrefoPayApiServiceInterface $service
49
    ) {
50
        $this->validator = $validator;
51
        $this->encodingService = $encodingService;
52
        $this->service = $service;
53
    }
54
55
    /**
56
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
57
     *
58
     * @return array
59
     */
60
    public function buildRequestPayload(CrefoPayApiRequestTransfer $requestTransfer): array
61
    {
62
        $this->validator->validate($requestTransfer);
63
        $requestPayload = $this->convertRequestTransferToArray($requestTransfer);
64
        $requestPayload = $this->removeRedundantParams($requestPayload);
65
        $requestPayload = $this->convertNestedArrayToJson($requestPayload);
66
        $requestPayload['mac'] = $this->service->calculateMac($requestPayload);
67
68
        return $requestPayload;
69
    }
70
71
    /**
72
     * @param array $data
73
     *
74
     * @return array
75
     */
76
    protected function removeRedundantParams(array $data): array
77
    {
78
        $data = array_filter($data, function ($item) {
79
            if ($item instanceof ArrayObject) {
80
                return $item->count() !== 0;
81
            }
82
            return $item !== null;
83
        });
84
85
        foreach ($data as $key => $value) {
86
            if (is_array($value) || $value instanceof ArrayObject) {
87
                $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

87
                $data[$key] = $this->removeRedundantParams(/** @scrutinizer ignore-type */ $value);
Loading history...
88
            }
89
        }
90
91
        return $data;
92
    }
93
94
    /**
95
     * @param array $data
96
     *
97
     * @return array
98
     */
99
    protected function convertNestedArrayToJson(array $data): array
100
    {
101
        foreach ($data as $key => $value) {
102
            if (is_array($value) || $value instanceof ArrayObject) {
103
                $data[$key] = $this->encodingService->encodeJson($value);
104
            }
105
        }
106
107
        return $data;
108
    }
109
}
110