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

createResponseTransferWithError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\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 Psr\Http\Message\ResponseInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface 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...
13
use SprykerEco\Shared\CrefoPayApi\CrefoPayApiConfig;
14
use SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface;
15
16
abstract class AbstractConverter implements CrefoPayApiConverterInterface
17
{
18
    /**
19
     * @var \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface
20
     */
21
    protected $encodingService;
22
23
    /**
24
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
25
     * @param array $responseData
26
     *
27
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
28
     */
29
    abstract protected function updateResponseTransferWithApiCallResponse(
30
        CrefoPayApiResponseTransfer $responseTransfer,
31
        array $responseData
32
    ): CrefoPayApiResponseTransfer;
33
34
    /**
35
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface $encodingService
36
     */
37
    public function __construct(
38
        CrefoPayApiToUtilEncodingServiceInterface $encodingService
39
    ) {
40
        $this->encodingService = $encodingService;
41
    }
42
43
    /**
44
     * @param \Psr\Http\Message\ResponseInterface $response
45
     * @param bool $isSuccess
46
     *
47
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
48
     */
49
    public function convertToResponseTransfer(
50
        ResponseInterface $response,
51
        bool $isSuccess = true
52
    ): CrefoPayApiResponseTransfer {
53
        $responseData = $this->encodingService->decodeJson($response->getBody(), true);
54
55
        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

55
        if (!$isSuccess || !$this->isResultCodeSuccess(/** @scrutinizer ignore-type */ $responseData)) {
Loading history...
56
            return $this->createResponseTransferWithError($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...onseTransferWithError() 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

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

60
        $responseTransfer = $this->updateResponseTransferWithApiCallResponse($responseTransfer, /** @scrutinizer ignore-type */ $responseData);
Loading history...
61
62
        return $responseTransfer;
63
    }
64
65
    /**
66
     * @param array $responseData
67
     *
68
     * @return bool
69
     */
70
    protected function isResultCodeSuccess(array $responseData): bool
71
    {
72
        $resultCode = $responseData[CrefoPayApiConfig::API_RESPONSE_FIELD_RESULT_CODE];
73
74
        return isset($resultCode) && ($resultCode === 0 || $resultCode === 1);
75
    }
76
77
    /**
78
     * @param array $responseData
79
     *
80
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
81
     */
82
    protected function createResponseTransferWithError(array $responseData): CrefoPayApiResponseTransfer
83
    {
84
        $errorTransfer = (new CrefoPayApiErrorResponseTransfer())
85
            ->fromArray($responseData, true)
86
            ->setErrorType(CrefoPayApiConfig::API_ERROR_TYPE_EXTERNAL);
87
88
        return (new CrefoPayApiResponseTransfer())
89
            ->setIsSuccess(false)
90
            ->setError($errorTransfer);
91
    }
92
93
    /**
94
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
95
     */
96
    protected function createSuccessResponseTransfer(): CrefoPayApiResponseTransfer
97
    {
98
        return (new CrefoPayApiResponseTransfer())
99
            ->setIsSuccess(true);
100
    }
101
}
102