Passed
Push — feature/eco-2295/eco-2344-crea... ( 79adcf...d1237e )
by Aleksey
02:24
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 Spryker\Shared\Kernel\Transfer\Exception\RequiredTransferPropertyException;
0 ignored issues
show
Bug introduced by
The type Spryker\Shared\Kernel\Tr...ansferPropertyException 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...
14
use SprykerEco\Shared\CrefoPayApi\CrefoPayApiConfig;
15
use SprykerEco\Zed\CrefoPayApi\Business\Validator\Response\CrefoPayApiResponseValidatorInterface;
16
use SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface;
17
18
abstract class AbstractConverter implements CrefoPayApiConverterInterface
19
{
20
    /**
21
     * @var \SprykerEco\Zed\CrefoPayApi\Business\Validator\Response\CrefoPayApiResponseValidatorInterface
22
     */
23
    protected $validator;
24
25
    /**
26
     * @var \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface
27
     */
28
    protected $encodingService;
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\Business\Validator\Response\CrefoPayApiResponseValidatorInterface $validator
43
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface $encodingService
44
     */
45
    public function __construct(
46
        CrefoPayApiResponseValidatorInterface $validator,
47
        CrefoPayApiToUtilEncodingServiceInterface $encodingService
48
    ) {
49
        $this->validator = $validator;
50
        $this->encodingService = $encodingService;
51
    }
52
53
    /**
54
     * @param \Psr\Http\Message\ResponseInterface $response
55
     * @param bool $isSuccess
56
     *
57
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
58
     */
59
    public function convertToResponseTransfer(
60
        ResponseInterface $response,
61
        bool $isSuccess = true
62
    ): CrefoPayApiResponseTransfer {
63
        $responseData = $this->encodingService->decodeJson($response->getBody(), 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);
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

66
            return $this->createResponseTransferWithError(/** @scrutinizer ignore-type */ $responseData);
Loading history...
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
        try {
73
            $this->validator->validate($responseTransfer);
74
        } catch (RequiredTransferPropertyException $requiredTransferPropertyException) {
75
            $responseTransfer = $this->updateResponseTransferWithValidationError(
76
                $responseTransfer,
77
                $requiredTransferPropertyException
78
            );
79
        }
80
81
        return $responseTransfer;
82
    }
83
84
    /**
85
     * @param array $responseData
86
     *
87
     * @return bool
88
     */
89
    protected function isResultCodeSuccess(array $responseData): bool
90
    {
91
        $resultCode = $responseData[CrefoPayApiConfig::API_RESPONSE_FIELD_RESULT_CODE];
92
93
        return isset($resultCode) && ($resultCode === 0 || $resultCode === 1);
94
    }
95
96
    /**
97
     * @param array $responseData
98
     *
99
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
100
     */
101
    protected function createResponseTransferWithError(array $responseData): CrefoPayApiResponseTransfer
102
    {
103
        $errorTransfer = (new CrefoPayApiErrorResponseTransfer())
104
            ->fromArray($responseData, true)
105
            ->setErrorType(CrefoPayApiConfig::API_ERROR_TYPE_EXTERNAL);
106
107
        return (new CrefoPayApiResponseTransfer())
108
            ->setIsSuccess(false)
109
            ->setError($errorTransfer);
110
    }
111
112
    /**
113
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
114
     */
115
    protected function createSuccessResponseTransfer(): CrefoPayApiResponseTransfer
116
    {
117
        return (new CrefoPayApiResponseTransfer())
118
            ->setIsSuccess(true);
119
    }
120
121
    /**
122
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
123
     * @param $requiredTransferPropertyException
124
     *
125
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
126
     */
127
    protected function updateResponseTransferWithValidationError(
128
        CrefoPayApiResponseTransfer $responseTransfer,
129
        RequiredTransferPropertyException $requiredTransferPropertyException
130
    ): CrefoPayApiResponseTransfer {
131
        $error = (new CrefoPayApiErrorResponseTransfer())
132
            ->setMessage($requiredTransferPropertyException->getMessage())
133
            ->setErrorType(CrefoPayApiConfig::API_ERROR_TYPE_INTERNAL);
134
135
        return $responseTransfer
136
            ->setIsSuccess(false)
137
            ->setError($error);
138
    }
139
}
140