ResponseCodeValidator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isSuccessfulTransaction() 0 4 2
A validate() 0 17 4
1
<?php
2
/**
3
 * Copyright © 2016 Magento. All rights reserved.
4
 * See COPYING.txt for license details.
5
 */
6
namespace Pagantis\Pagantis\Gateway\Validator;
7
8
use Magento\Payment\Gateway\Validator\AbstractValidator;
9
use Magento\Payment\Gateway\Validator\ResultInterface;
10
use Pagantis\Pagantis\Gateway\Http\Client\ClientMock;
11
12
class ResponseCodeValidator extends AbstractValidator
13
{
14
    const RESULT_CODE = 'RESULT_CODE';
15
16
    /**
17
     * Performs validation of result code
18
     *
19
     * @param array $validationSubject
20
     * @return ResultInterface
21
     */
22
    public function validate(array $validationSubject)
23
    {
24
        if (!isset($validationSubject['response']) || !is_array($validationSubject['response'])) {
25
            throw new \InvalidArgumentException('Response does not exist');
26
        }
27
28
        $response = $validationSubject['response'];
29
30
        if ($this->isSuccessfulTransaction($response)) {
31
            return $this->createResult(
32
                true,
33
                []
34
            );
35
        } else {
36
            return $this->createResult(
37
                false,
38
                [__('Gateway rejected the transaction.')]
39
            );
40
        }
41
    }
42
43
    /**
44
     * @param array $response
45
     * @return bool
46
     */
47
    private function isSuccessfulTransaction(array $response)
48
    {
49
        return isset($response[self::RESULT_CODE])
50
        && $response[self::RESULT_CODE] !== ClientMock::FAILURE;
51
    }
52
}
53