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

AbstractConverter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Converter;
9
10
use Generated\Shared\Transfer\CrefoPayApiErrorResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...piErrorResponseTransfer 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...
11
use Generated\Shared\Transfer\CrefoPayApiResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...oPayApiResponseTransfer 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\CrefoPayApiConfig;
13
use SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface;
14
use SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface;
15
16
abstract class AbstractConverter implements CrefoPayApiConverterInterface
17
{
18
    protected const EXTERNAL_ERROR_MESSAGE = 'CrefoPay service temporarily unavailable.';
19
20
    /**
21
     * @var \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface
22
     */
23
    protected $encodingService;
24
25
    /**
26
     * @var \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig
27
     */
28
    protected $config;
29
30
    /**
31
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
32
     * @param array $responseData
33
     *
34
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
35
     */
36
    abstract protected function updateResponseTransferWithApiCallResponse(
37
        CrefoPayApiResponseTransfer $responseTransfer,
38
        array $responseData
39
    ): CrefoPayApiResponseTransfer;
40
41
    /**
42
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface $encodingService
43
     * @param \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig $config
44
     */
45
    public function __construct(
46
        CrefoPayApiToUtilEncodingServiceInterface $encodingService,
47
        CrefoPayApiConfig $config
48
    ) {
49
        $this->encodingService = $encodingService;
50
        $this->config = $config;
51
    }
52
53
    /**
54
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface $response
55
     * @param bool $isSuccess
56
     *
57
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
58
     */
59
    public function convertToResponseTransfer(
60
        CrefoPayApiGuzzleResponseInterface $response,
61
        bool $isSuccess = true
62
    ): CrefoPayApiResponseTransfer {
63
        $responseData = $this->encodingService->decodeJson($response->getResponseBody(), true);
64
65
        if (!$isSuccess || !$this->isResultCodeSuccess($responseData)) {
0 ignored issues
show
Bug introduced by
It seems like $responseData can also be of type null; however, parameter $responseData of SprykerEco\Zed\CrefoPayA...::isResultCodeSuccess() 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
        if (!$isSuccess || !$this->isResultCodeSuccess(/** @scrutinizer ignore-type */ $responseData)) {
Loading history...
66
            return $this->createResponseTransferWithError($responseData);
67
        }
68
69
        $responseTransfer = $this->createSuccessResponseTransfer();
70
        $responseTransfer = $this->updateResponseTransferWithApiCallResponse($responseTransfer, $responseData);
0 ignored issues
show
Bug introduced by
It seems like $responseData can also be of type null; however, parameter $responseData of SprykerEco\Zed\CrefoPayA...erWithApiCallResponse() 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

70
        $responseTransfer = $this->updateResponseTransferWithApiCallResponse($responseTransfer, /** @scrutinizer ignore-type */ $responseData);
Loading history...
71
72
        return $responseTransfer;
73
    }
74
75
    /**
76
     * @param array $responseData
77
     *
78
     * @return bool
79
     */
80
    protected function isResultCodeSuccess(array $responseData): bool
81
    {
82
        if ($responseData === null) {
0 ignored issues
show
introduced by
The condition $responseData === null is always false.
Loading history...
83
            return false;
84
        }
85
86
        $resultCode = $responseData[$this->config->getApiResponseFieldResultCode()];
87
88
        return isset($resultCode) && ($resultCode === 0 || $resultCode === 1);
89
    }
90
91
    /**
92
     * @param array|null $responseData
93
     *
94
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
95
     */
96
    protected function createResponseTransferWithError(?array $responseData): CrefoPayApiResponseTransfer
97
    {
98
        $errorTransfer = (new CrefoPayApiErrorResponseTransfer())
99
            ->setMessage(static::EXTERNAL_ERROR_MESSAGE)
100
            ->setErrorType($this->config->getApiErrorTypeExternal());
101
102
        if ($responseData !== null) {
0 ignored issues
show
introduced by
The condition $responseData !== null is always true.
Loading history...
103
            $errorTransfer->fromArray($responseData, true);
104
        }
105
106
        return (new CrefoPayApiResponseTransfer())
107
            ->setIsSuccess(false)
108
            ->setError($errorTransfer);
109
    }
110
111
    /**
112
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
113
     */
114
    protected function createSuccessResponseTransfer(): CrefoPayApiResponseTransfer
115
    {
116
        return (new CrefoPayApiResponseTransfer())
117
            ->setIsSuccess(true);
118
    }
119
}
120