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

AbstractConverter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
rs 10
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\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\Shared\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
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
27
     * @param array $responseData
28
     *
29
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
30
     */
31
    abstract protected function updateResponseTransferWithApiCallResponse(
32
        CrefoPayApiResponseTransfer $responseTransfer,
33
        array $responseData
34
    ): CrefoPayApiResponseTransfer;
35
36
    /**
37
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface $encodingService
38
     */
39
    public function __construct(
40
        CrefoPayApiToUtilEncodingServiceInterface $encodingService
41
    ) {
42
        $this->encodingService = $encodingService;
43
    }
44
45
    /**
46
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface $response
47
     * @param bool $isSuccess
48
     *
49
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
50
     */
51
    public function convertToResponseTransfer(
52
        CrefoPayApiGuzzleResponseInterface $response,
53
        bool $isSuccess = true
54
    ): CrefoPayApiResponseTransfer {
55
        $responseData = $this->encodingService->decodeJson($response->getResponseBody(), true);
56
57
        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

57
        if (!$isSuccess || !$this->isResultCodeSuccess(/** @scrutinizer ignore-type */ $responseData)) {
Loading history...
58
            return $this->createResponseTransferWithError($responseData);
59
        }
60
61
        $responseTransfer = $this->createSuccessResponseTransfer();
62
        $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

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