Passed
Push — feature/eco-2295/eco-2344-crea... ( 3d8328...0c1e08 )
by Aleksey
01:40
created

CrefoPayApiResponseConverter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
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\Response\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\Business\Response\Mapper\CrefoPayApiResponseMapperInterface;
13
use SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig;
14
use SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface;
15
use SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface;
16
17
class CrefoPayApiResponseConverter implements CrefoPayApiResponseConverterInterface
18
{
19
    protected const EXTERNAL_ERROR_MESSAGE = 'CrefoPay service temporarily unavailable.';
20
21
    /**
22
     * @var \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface
23
     */
24
    protected $encodingService;
25
26
    /**
27
     * @var \SprykerEco\Zed\CrefoPayApi\Business\Response\Mapper\CrefoPayApiResponseMapperInterface
28
     */
29
    protected $responseMapper;
30
31
    /**
32
     * @var \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig
33
     */
34
    protected $config;
35
36
    /**
37
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface $encodingService
38
     * @param \SprykerEco\Zed\CrefoPayApi\Business\Response\Mapper\CrefoPayApiResponseMapperInterface $responseMapper
39
     * @param \SprykerEco\Zed\CrefoPayApi\CrefoPayApiConfig $config
40
     */
41
    public function __construct(
42
        CrefoPayApiToUtilEncodingServiceInterface $encodingService,
43
        CrefoPayApiResponseMapperInterface $responseMapper,
44
        CrefoPayApiConfig $config
45
    ) {
46
        $this->encodingService = $encodingService;
47
        $this->responseMapper = $responseMapper;
48
        $this->config = $config;
49
    }
50
51
    /**
52
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface $response
53
     * @param bool $isSuccess
54
     *
55
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
56
     */
57
    public function convertToResponseTransfer(
58
        CrefoPayApiGuzzleResponseInterface $response,
59
        bool $isSuccess = true
60
    ): CrefoPayApiResponseTransfer {
61
        $responseData = $this->encodingService->decodeJson($response->getResponseBody(), true);
62
63
        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

63
        if (!$isSuccess || !$this->isResultCodeSuccess(/** @scrutinizer ignore-type */ $responseData)) {
Loading history...
64
            return $this->createResponseTransferWithError($responseData);
65
        }
66
67
        $responseTransfer = $this->createSuccessResponseTransfer();
68
        $responseTransfer = $this->responseMapper
69
            ->mapResponseDataToResponseTransfer($responseData, $responseTransfer);
0 ignored issues
show
Bug introduced by
It seems like $responseData can also be of type null; however, parameter $responseData of SprykerEco\Zed\CrefoPayA...ataToResponseTransfer() 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

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