DeviceDataRequest::build()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 18
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 31
rs 9.6666
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\Request;
10
11
use Getnet\PaymentMagento\Gateway\SubjectReader;
12
use InvalidArgumentException;
13
use Magento\Framework\HTTP\Header as HeaderClient;
14
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
15
use Magento\Framework\Session\SessionManager;
16
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
17
use Magento\Payment\Gateway\Request\BuilderInterface;
18
19
/**
20
 * Class Device Data Request - User Device Data Structure.
21
 */
22
class DeviceDataRequest implements BuilderInterface
23
{
24
    /**
25
     * Device data customer.
26
     */
27
    public const DEVICE_DATA = 'device';
28
29
    /**
30
     * Remote IP data.
31
     */
32
    public const REMOTE_IP = 'ip_address';
33
34
    /**
35
     * Remote User Agent data.
36
     */
37
    public const REMOTE_USER_AGENT = 'userAgent';
38
39
    /**
40
     * Device Id data.
41
     */
42
    public const DEVICE_ID = 'device_id';
43
44
    /**
45
     * @var SubjectReader
46
     */
47
    protected $subjectReader;
48
49
    /**
50
     * @var remoteAddress
51
     */
52
    protected $remoteAddress;
53
54
    /**
55
     * @var headerClient
56
     */
57
    protected $headerClient;
58
59
    /**
60
     * @var SessionManager
61
     */
62
    protected $session;
63
64
    /**
65
     * @param RemoteAddress  $remoteAddress
66
     * @param HeaderClient   $headerClient
67
     * @param SubjectReader  $subjectReader
68
     * @param SessionManager $session
69
     */
70
    public function __construct(
71
        RemoteAddress $remoteAddress,
72
        HeaderClient $headerClient,
73
        SubjectReader $subjectReader,
74
        SessionManager $session
75
    ) {
76
        $this->remoteAddress = $remoteAddress;
77
        $this->headerClient = $headerClient;
78
        $this->subjectReader = $subjectReader;
79
        $this->session = $session;
80
    }
81
82
    /**
83
     * Build.
84
     *
85
     * @param array $buildSubject
86
     */
87
    public function build(array $buildSubject)
88
    {
89
        if (!isset($buildSubject['payment'])
90
        || !$buildSubject['payment'] instanceof PaymentDataObjectInterface
91
        ) {
92
            throw new InvalidArgumentException('Payment data object should be provided');
93
        }
94
95
        $paymentDO = $this->subjectReader->readPayment($buildSubject);
96
97
        $result = [];
98
        $ipCustomer = $this->remoteAddress->getRemoteAddress();
99
        if (empty($ipCustomer)) {
100
            $payment = $paymentDO->getPayment();
101
            $order = $payment->getOrder();
102
            $ipCustomer = $order->getXForwardedFor();
103
        }
104
        $result[self::DEVICE_DATA] = [
105
            self::REMOTE_IP         => $ipCustomer,
106
            // self::REMOTE_USER_AGENT => $this->headerClient->getHttpUserAgent(),
107
            self::DEVICE_ID         => $this->session->getSessionId(),
108
        ];
109
110
        $paymentInfo = $paymentDO->getPayment();
111
112
        $paymentInfo->setAdditionalInformation(
113
            self::DEVICE_DATA,
114
            $result[self::DEVICE_DATA]
115
        );
116
117
        return $result;
118
    }
119
}
120