Passed
Branch master (acde85)
by Alexander
01:40
created

CallbackValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace B2Binpay\Payment\Gateway\Validator;
4
5
use Magento\Sales\Model\OrderFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\OrderFactory 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...
6
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...
7
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...
8
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...
9
use B2Binpay\Payment\Model\Adapter\B2BinpayAdapterFactory;
10
11
class CallbackValidator extends AbstractValidator
12
{
13
    const UNAUTHORIZED = 'Unauthorized';
14
    const BAD_REQUEST = 'Bad Request';
15
    const UNAUTHORIZED_STATUS = '401';
16
    const BAD_REQUEST_STATUS = '400';
17
18
    /**
19
     * @var OrderFactory
20
     */
21
    protected $orderFactory;
22
23
    /**
24
     * @var B2BinpayAdapterFactory
25
     */
26
    protected $adapterFactory;
27
28
    /**
29
     * CallbackValidator constructor.
30
     * @param ResultInterfaceFactory $resultFactory
31
     * @param OrderFactory $orderFactory
32
     * @param B2BinpayAdapterFactory $adapterFactory
33
     */
34
    public function __construct(
35
        ResultInterfaceFactory $resultFactory,
36
        OrderFactory $orderFactory,
37
        B2BinpayAdapterFactory $adapterFactory
38
    ) {
39
        $this->orderFactory = $orderFactory;
40
        $this->adapterFactory = $adapterFactory;
41
        parent::__construct($resultFactory);
42
    }
43
44
    /**
45
     * Performs validation of result code
46
     *
47
     * @param array $validationSubject
48
     * @return ResultInterface
49
     */
50
    public function validate(array $validationSubject)
51
    {
52
        $auth = $validationSubject['request']->getHeader('Authorization');
53
54
        if (empty($auth)) {
55
            return $this->createResult(
56
                false,
57
                [
58
                    'status' => self::UNAUTHORIZED_STATUS,
59
                    'message' => self::UNAUTHORIZED
60
                ]
61
            );
62
        }
63
64
        $trackingId = $validationSubject['request']->getParam('tracking_id');
65
66
        if (empty($trackingId)) {
67
            return $this->createResult(
68
                false,
69
                [
70
                    'status' => self::BAD_REQUEST_STATUS,
71
                    'message' => self::BAD_REQUEST
72
                ]
73
            );
74
        }
75
76
        $order = $this->orderFactory->create()->loadByIncrementId($trackingId);
77
78
        if (empty($order->getEntityId())) {
79
            return $this->createResult(
80
                false,
81
                [
82
                    'status' => self::BAD_REQUEST_STATUS,
83
                    'message' => self::BAD_REQUEST
84
                ]
85
            );
86
        }
87
88
        $b2binpay = $this->adapterFactory->create($order->getStoreId());
89
90
        $checkAuth = $b2binpay->getAuthorization();
91
92
        if ($auth !== $checkAuth) {
93
            return $this->createResult(
94
                false,
95
                [
96
                    'status' => self::UNAUTHORIZED_STATUS,
97
                    'message' => self::UNAUTHORIZED
98
                ]
99
            );
100
        }
101
102
        return $this->createResult(
103
            true,
104
            [
105
                'order' => $order
106
            ]
107
        );
108
    }
109
}
110