Passed
Pull Request — feature/eco-2295/dev (#1)
by Aleksey
05:36 queued 03:07
created

CrefoPayApiResponseConverter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A updateResponseTransferWithError() 0 15 2
A createResponseTransfer() 0 4 1
A __construct() 0 8 1
A convertToResponseTransfer() 0 13 3
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
    protected const API_ERROR_TYPE_EXTERNAL = 'EXTERNAL';
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
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\Service\CrefoPayApiToUtilEncodingServiceInterface $encodingService
39
     * @param \SprykerEco\Zed\CrefoPayApi\Business\Response\Mapper\CrefoPayApiResponseMapperInterface $responseMapper
40
     * @param \SprykerEco\Zed\CrefoPayApi\Business\Response\Validator\CrefoPayApiResponseValidatorInterface $responseValidator
41
     */
42
    public function __construct(
43
        CrefoPayApiToUtilEncodingServiceInterface $encodingService,
44
        CrefoPayApiResponseMapperInterface $responseMapper,
45
        CrefoPayApiResponseValidatorInterface $responseValidator
46
    ) {
47
        $this->encodingService = $encodingService;
48
        $this->responseMapper = $responseMapper;
49
        $this->responseValidator = $responseValidator;
50
    }
51
52
    /**
53
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface $response
54
     * @param bool $isSuccess
55
     *
56
     * @return \Generated\Shared\Transfer\CrefoPayApiResponseTransfer
57
     */
58
    public function convertToResponseTransfer(
59
        CrefoPayApiGuzzleResponseInterface $response,
60
        bool $isSuccess = true
61
    ): CrefoPayApiResponseTransfer {
62
        $responseData = $this->encodingService->decodeJson($response->getResponseBody(), true);
63
        $responseTransfer = $this->createResponseTransfer($isSuccess);
64
65
        if (!$isSuccess || !$this->responseValidator->validateResponse($response, $responseData)) {
66
            return $this->updateResponseTransferWithError($responseTransfer, $responseData);
67
        }
68
69
        return $this->responseMapper
70
            ->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

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