GeneralResponseValidator::validate()   B
last analyzed

Complexity

Conditions 8
Paths 3

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 18
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 27
rs 8.4444
1
<?php
2
/**
3
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
9
namespace Getnet\PaymentMagento\Gateway\Validator;
10
11
use Getnet\PaymentMagento\Gateway\SubjectReader;
12
use Magento\Payment\Gateway\Validator\AbstractValidator;
13
use Magento\Payment\Gateway\Validator\ResultInterface;
14
use Magento\Payment\Gateway\Validator\ResultInterfaceFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\...\ResultInterfaceFactory 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...
15
16
/**
17
 * Class GeneralResponseValidator - Handles return from gateway.
18
 */
19
class GeneralResponseValidator extends AbstractValidator
20
{
21
    /**
22
     * The result code.
23
     */
24
    public const RESULT_CODE_SUCCESS = '1';
25
26
    /**
27
     * @var SubjectReader
28
     */
29
    private $subjectReader;
30
31
    /**
32
     * @param ResultInterfaceFactory $resultFactory
33
     * @param SubjectReader          $subjectReader
34
     */
35
    public function __construct(
36
        ResultInterfaceFactory $resultFactory,
37
        SubjectReader $subjectReader
38
    ) {
39
        parent::__construct($resultFactory);
40
        $this->subjectReader = $subjectReader;
41
    }
42
43
    /**
44
     * Validate.
45
     *
46
     * @param array $validationSubject
47
     *
48
     * @return ResultInterface
49
     */
50
    public function validate(array $validationSubject): ResultInterface
51
    {
52
        $response = $this->subjectReader->readResponse($validationSubject);
53
        $isValid = $response['RESULT_CODE'];
54
        $errorCodes = [];
55
        $errorMessages = [];
56
        if (!$isValid) {
57
            if (isset($response['details'])) {
58
                foreach ($response['details'] as $message) {
59
                    if (isset($message['antifraud'])) {
60
                        $errorCodes[] = $message['antifraud']['status_code'];
61
                        $errorMessages[] = $message['antifraud']['description'];
62
                    }
63
                    if (isset($message['error_code'])) {
64
                        $errorCodes[] = $message['error_code'];
65
                        $errorMessages[] = isset($message['description_detail']) ?: $message['description'];
66
                        if ($message['error_code'] === 'PAYMENTS-402') {
67
                            $detailCode = preg_replace('/[^0-9]/', '', $message['description_detail']);
68
                            $errorCodes[] = $message['error_code'].'-'.$detailCode;
69
                            $errorMessages[] = $message['description_detail'];
70
                        }
71
                    }
72
                }
73
            }
74
        }
75
76
        return $this->createResult($isValid, $errorMessages, $errorCodes);
77
    }
78
}
79