GeneralResponseValidator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 25 7
A __construct() 0 7 1
1
<?php
2
/**
3
 * Copyright © Wirecard Brasil. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
namespace Moip\Magento2\Gateway\Validator;
10
11
use Magento\Payment\Gateway\Validator\AbstractValidator;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\...dator\AbstractValidator 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 Magento\Payment\Gateway\Validator\ResultInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\Validator\ResultInterface 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...
13
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...
14
use Moip\Magento2\Gateway\SubjectReader;
15
16
/**
17
 * Class GeneralResponseValidator - Handles return from gateway.
18
 */
19
class GeneralResponseValidator extends AbstractValidator
20
{
21
    /**
22
     * The result code.
23
     */
24
    const RESULT_CODE_SUCCESS = '1';
25
26
    /**
27
     * @var SubjectReader
28
     */
29
    private $subjectReader;
30
31
    /**
32
     * @var ResultInterfaceFactory
33
     */
34
    private $resultFactory;
35
36
    /**
37
     * @param ResultInterfaceFactory $resultFactory
38
     * @param SubjectReader          $subjectReader
39
     */
40
    public function __construct(
41
        ResultInterfaceFactory $resultFactory,
42
        SubjectReader $subjectReader
43
    ) {
44
        parent::__construct($resultFactory);
45
        $this->resultFactory = $resultFactory;
46
        $this->subjectReader = $subjectReader;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function validate(array $validationSubject): ResultInterface
53
    {
54
        $response = $this->subjectReader->readResponse($validationSubject);
55
        $isValid = $response['RESULT_CODE'];
56
        $errorCodes = [];
57
        $errorMessages = [];
58
59
        if (!$isValid) {
60
            if (isset($response['messages']['message']['code'])) {
61
                $errorCodes[] = $response['messages']['message']['code'];
62
                $errorMessages[] = $response['messages']['message']['text'];
63
            } elseif (isset($response['messages']['message']['errors'])) {
64
                foreach ($response['messages']['message']['errors'] as $message) {
65
                    $errorCodes[] = $message['code'];
66
                    $errorMessages[] = $message['description'];
67
                }
68
            } elseif (isset($response['errors'])) {
69
                foreach ($response['errors'] as $message) {
70
                    $errorCodes[] = $message['code'];
71
                    $errorMessages[] = $message['description'];
72
                }
73
            }
74
        }
75
76
        return $this->createResult($isValid, $errorMessages, $errorCodes);
77
    }
78
}
79