updateResponseTransferWithError()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 15
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\Business\Response\Validator\CrefoPayApiResponseValidatorInterface;
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
    /**
20
     * @var string
21
     */
22
    protected const API_ERROR_TYPE_EXTERNAL = 'EXTERNAL';
23
24
    /**
25
     * @var string
26
     */
27
    protected const EXTERNAL_ERROR_MESSAGE = 'CrefoPay service temporarily unavailable.';
28
29
    /**
30
     * @var \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface
31
     */
32
    protected $encodingService;
33
34
    /**
35
     * @var \SprykerEco\Zed\CrefoPayApi\Business\Response\Mapper\CrefoPayApiResponseMapperInterface
36
     */
37
    protected $responseMapper;
38
39
    /**
40
     * @var \SprykerEco\Zed\CrefoPayApi\Business\Response\Validator\CrefoPayApiResponseValidatorInterface
41
     */
42
    protected $responseValidator;
43
44
    /**
45
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface $encodingService
46
     * @param \SprykerEco\Zed\CrefoPayApi\Business\Response\Mapper\CrefoPayApiResponseMapperInterface $responseMapper
47
     * @param \SprykerEco\Zed\CrefoPayApi\Business\Response\Validator\CrefoPayApiResponseValidatorInterface $responseValidator
48
     */
49
    public function __construct(
50
        CrefoPayApiToUtilEncodingServiceInterface $encodingService,
51
        CrefoPayApiResponseMapperInterface $responseMapper,
52
        CrefoPayApiResponseValidatorInterface $responseValidator
53
    ) {
54
        $this->encodingService = $encodingService;
55
        $this->responseMapper = $responseMapper;
56
        $this->responseValidator = $responseValidator;
57
    }
58
59
    /**
60
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface $response
61
     * @param bool $isSuccess
62
     *
63
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
64
     */
65
    public function convertToResponseTransfer(
66
        CrefoPayApiGuzzleResponseInterface $response,
67
        bool $isSuccess = true
68
    ): CrefoPayApiResponseTransfer {
69
        $responseData = $this->encodingService->decodeJson($response->getResponseBody(), true);
70
        $responseTransfer = $this->createResponseTransfer($isSuccess);
71
72
        if (!$isSuccess || !$this->responseValidator->validateResponse($response, $responseData)) {
73
            return $this->updateResponseTransferWithError($responseTransfer, $responseData);
74
        }
75
76
        return $this->responseMapper
77
            ->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

77
            ->mapResponseDataToResponseTransfer(/** @scrutinizer ignore-type */ $responseData, $responseTransfer);
Loading history...
78
    }
79
80
    /**
81
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
82
     * @param array|null $responseData
83
     *
84
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
85
     */
86
    protected function updateResponseTransferWithError(
87
        CrefoPayApiResponseTransfer $responseTransfer,
88
        ?array $responseData
89
    ): CrefoPayApiResponseTransfer {
90
        $errorTransfer = (new CrefoPayApiErrorResponseTransfer())
91
            ->setMessage(static::EXTERNAL_ERROR_MESSAGE)
92
            ->setErrorType(static::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 $responseTransfer
99
            ->setIsSuccess(false)
100
            ->setError($errorTransfer);
101
    }
102
103
    /**
104
     * @param bool $isSuccess
105
     *
106
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
107
     */
108
    protected function createResponseTransfer(bool $isSuccess): CrefoPayApiResponseTransfer
109
    {
110
        return (new CrefoPayApiResponseTransfer())
111
            ->setIsSuccess($isSuccess);
112
    }
113
}
114