ResponseCodeValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isSuccessfulTransaction() 0 3 1
A validate() 0 18 4
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 InvalidArgumentException;
12
use Magento\Payment\Gateway\Validator\AbstractValidator;
13
use Magento\Payment\Gateway\Validator\ResultInterface;
14
15
/**
16
 * Class ResponseCodeValidator - Handles return from gateway.
17
 */
18
class ResponseCodeValidator extends AbstractValidator
19
{
20
    /**
21
     * @var string
22
     */
23
    public const RESULT_CODE = 'RESULT_CODE';
24
25
    /**
26
     * Validation.
27
     *
28
     * @param array $validationSubject
29
     *
30
     * @return ResultInterface
31
     */
32
    public function validate(array $validationSubject)
33
    {
34
        if (!isset($validationSubject['response']) || !is_array($validationSubject['response'])) {
35
            throw new InvalidArgumentException('Response does not exist');
36
        }
37
38
        $response = $validationSubject['response'];
39
40
        if ($this->isSuccessfulTransaction($response)) {
41
            return $this->createResult(
42
                true,
43
                []
44
            );
45
        }
46
47
        return $this->createResult(
48
            false,
49
            [__('Gateway rejected the transaction.')]
50
        );
51
    }
52
53
    /**
54
     * Is Successful Transaction.
55
     *
56
     * @param array $response
57
     *
58
     * @return bool
59
     */
60
    private function isSuccessfulTransaction(array $response)
61
    {
62
        return $response[self::RESULT_CODE];
63
    }
64
}
65