Passed
Push — feature/eco-2295/eco-2344-crea... ( 1e5ae0...217e63 )
by Aleksey
01:02
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\Zed\CrefoPayApi\Business\Validator\Request\CrefoPayApiRequestValidatorInterface;
13
14
abstract class AbstractRequestBuilder implements CrefoPayApiRequestBuilderInterface
15
{
16
    /**
17
     * @var \SprykerEco\Zed\CrefoPayApi\Business\Validator\Request\CrefoPayApiRequestValidatorInterface
18
     */
19
    protected $validator;
20
21
    /**
22
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
23
     *
24
     * @return array
25
     */
26
    abstract protected function convertRequestTransferToArray(CrefoPayApiRequestTransfer $requestTransfer): array;
27
28
    /**
29
     * @param \SprykerEco\Zed\CrefoPayApi\Business\Validator\Request\CrefoPayApiRequestValidatorInterface $validator
30
     */
31
    public function __construct(CrefoPayApiRequestValidatorInterface $validator)
32
    {
33
        $this->validator = $validator;
34
    }
35
36
    /**
37
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
38
     *
39
     * @return array
40
     */
41
    public function buildRequestPayload(CrefoPayApiRequestTransfer $requestTransfer): array
42
    {
43
        $this->validator->validate($requestTransfer);
44
        $requestPayload = $this->convertRequestTransferToArray($requestTransfer);
45
46
        return $this->removeRedundantParams($requestPayload);
47
    }
48
49
    /**
50
     * @param array $data
51
     *
52
     * @return array
53
     */
54
    protected function removeRedundantParams(array $data): array
55
    {
56
        $data = array_filter($data, function ($item) {
57
            if ($item instanceof ArrayObject) {
58
                return $item->count() !== 0;
59
            }
60
            return !empty($item);
61
        });
62
63
        foreach ($data as $key => $value) {
64
            if (is_array($value) || $value instanceof ArrayObject) {
65
                $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

65
                $data[$key] = $this->removeRedundantParams(/** @scrutinizer ignore-type */ $value);
Loading history...
66
            }
67
        }
68
69
        return $data;
70
    }
71
}
72