ResponseParser::parseResponse()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 21
rs 9.8333
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\Client\FactFinderNg\Parser;
9
10
use Generated\Shared\Transfer\FactFinderNgResponseErrorTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...NgResponseErrorTransfer 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\FactFinderNgResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...inderNgResponseTransfer 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;
13
use SprykerEco\Client\FactFinderNg\Dependency\Service\FactFinderNgToUtilEncodingServiceInterface;
14
15
class ResponseParser implements ResponseParserInterface
16
{
17
    public const RESPONSE_KEY_ERROR = 'error';
18
    public const RESPONSE_KEY_ERROR_DESCRIPTION = 'errorDescription';
19
    public const RESPONSE_KEY_ERROR_STACKTRACE = 'stacktrace';
20
21
    /**
22
     * @var \SprykerEco\Client\FactFinderNg\Dependency\Service\FactFinderNgToUtilEncodingServiceInterface
23
     */
24
    protected $utilEncodingService;
25
26
    /**
27
     * @param \SprykerEco\Client\FactFinderNg\Dependency\Service\FactFinderNgToUtilEncodingServiceInterface $utilEncodingService
28
     */
29
    public function __construct(FactFinderNgToUtilEncodingServiceInterface $utilEncodingService)
30
    {
31
        $this->utilEncodingService = $utilEncodingService;
32
    }
33
34
    /**
35
     * @param \Psr\Http\Message\ResponseInterface $response
36
     *
37
     * @return \Generated\Shared\Transfer\FactFinderNgResponseTransfer
38
     */
39
    public function parseResponse(ResponseInterface $response): FactFinderNgResponseTransfer
40
    {
41
        $responseTransfer = new FactFinderNgResponseTransfer();
42
        $responseBody = $this->utilEncodingService->decodeJson($response->getBody(), true);
43
44
        if ($response->getStatusCode() >= 400) {
45
            $errorTransfer = new FactFinderNgResponseErrorTransfer();
46
            $errorTransfer->setError($responseBody[static::RESPONSE_KEY_ERROR]);
47
            $errorTransfer->setErrorDescription($responseBody[static::RESPONSE_KEY_ERROR_DESCRIPTION]);
48
            $errorTransfer->setStacktrace($responseBody[static::RESPONSE_KEY_ERROR_STACKTRACE]);
49
50
            $responseTransfer->setIsSuccess(false);
51
            $responseTransfer->setError($errorTransfer);
52
53
            return $responseTransfer;
54
        }
55
56
        $responseTransfer->setIsSuccess(true);
57
        $responseTransfer->setBody($responseBody);
58
59
        return $responseTransfer;
60
    }
61
}
62