CrefoPayApiResponseValidator::validateResponse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 7
rs 10
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\Validator;
9
10
use SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface;
11
use SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface;
12
13
class CrefoPayApiResponseValidator implements CrefoPayApiResponseValidatorInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    protected const API_HEADER_MAC = 'X-Payco-HMAC';
19
20
    /**
21
     * @var string
22
     */
23
    protected const API_RESPONSE_FIELD_RESULT_CODE = 'resultCode';
24
25
    /**
26
     * @var int
27
     */
28
    protected const RESULT_CODE_OK = 0;
29
30
    /**
31
     * @var int
32
     */
33
    protected const RESULT_CODE_REDIRECT = 1;
34
35
    /**
36
     * @var \SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface
37
     */
38
    protected $service;
39
40
    /**
41
     * @param \SprykerEco\Service\CrefoPayApi\CrefoPayApiServiceInterface $service
42
     */
43
    public function __construct(CrefoPayApiServiceInterface $service)
44
    {
45
        $this->service = $service;
46
    }
47
48
    /**
49
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface $response
50
     * @param array|null $responseData
51
     *
52
     * @return bool
53
     */
54
    public function validateResponse(
55
        CrefoPayApiGuzzleResponseInterface $response,
56
        ?array $responseData
57
    ): bool {
58
        return $responseData !== null
59
            && $this->isResultCodeSuccess($responseData)
60
            && $this->validateMac($response);
61
    }
62
63
    /**
64
     * @param array $responseData
65
     *
66
     * @return bool
67
     */
68
    protected function isResultCodeSuccess(array $responseData): bool
69
    {
70
        $resultCode = $responseData[static::API_RESPONSE_FIELD_RESULT_CODE] ?? null;
71
        $successResultCodes = [
72
            static::RESULT_CODE_OK,
73
            static::RESULT_CODE_REDIRECT,
74
        ];
75
76
        return in_array($resultCode, $successResultCodes);
77
    }
78
79
    /**
80
     * @param \SprykerEco\Zed\CrefoPayApi\Dependency\External\Guzzle\Response\CrefoPayApiGuzzleResponseInterface $response
81
     *
82
     * @return bool
83
     */
84
    protected function validateMac(CrefoPayApiGuzzleResponseInterface $response): bool
85
    {
86
        $mac = $response->getHeader(static::API_HEADER_MAC);
87
        if ($mac === null) {
88
            return false;
89
        }
90
91
        return $this->service
92
            ->validateMac([$response->getResponseBody()], $mac);
93
    }
94
}
95