Passed
Push — feature/eco-2295/eco-2344-crea... ( b58615...425ba1 )
by Aleksey
01:12
created

createResponseTransferWithError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

78
            ->mapResponseDataToResponseTransfer(/** @scrutinizer ignore-type */ $responseData, $responseTransfer);
Loading history...
79
    }
80
81
    /**
82
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
83
     * @param array|null $responseData
84
     *
85
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
86
     */
87
    protected function updateResponseTransferWithError(
88
        CrefoPayApiResponseTransfer $responseTransfer,
89
        ?array $responseData
90
    ): CrefoPayApiResponseTransfer {
91
        $errorTransfer = (new CrefoPayApiErrorResponseTransfer())
92
            ->setMessage(static::EXTERNAL_ERROR_MESSAGE)
93
            ->setErrorType($this->config->getApiErrorTypeExternal());
94
95
        if ($responseData !== null) {
0 ignored issues
show
introduced by
The condition $responseData !== null is always true.
Loading history...
96
            $errorTransfer->fromArray($responseData, true);
97
        }
98
99
        return $responseTransfer
100
            ->setIsSuccess(false)
101
            ->setError($errorTransfer);
102
    }
103
104
    /**
105
     * @param bool $isSuccess
106
     *
107
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
108
     */
109
    protected function createResponseTransfer(bool $isSuccess): CrefoPayApiResponseTransfer
110
    {
111
        return (new CrefoPayApiResponseTransfer())
112
            ->setIsSuccess($isSuccess);
113
    }
114
}
115