AbstractPaypalRequest::initialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Omnipay\PaypalRest\Message;
4
5
/**
6
 * @author    Ivan Kerin <[email protected]>
7
 * @copyright 2014, Clippings Ltd.
8
 * @license   http://spdx.org/licenses/BSD-3-Clause
9
 */
10
abstract class AbstractPaypalRequest extends AbstractRequest
11
{
12
    /**
13
     * @return string
14
     */
15
    abstract public function getEndpoint();
16
17
    /**
18
     * From Official Paypal SDK
19
     *
20
     * @return string
21
     */
22
    private function generateRequestId()
23
    {
24
        static $pid = -1;
25
        static $addr = -1;
26
27
        if ($pid == -1) {
28
            $pid = getmypid();
29
        }
30
31
        if ($addr == -1) {
32
            if (array_key_exists('SERVER_ADDR', $_SERVER)) {
33
                $addr = ip2long($_SERVER['SERVER_ADDR']);
34
            } else {
35
                $addr  = php_uname('n');
36
            }
37
        }
38
39
        return $addr . $pid . $_SERVER['REQUEST_TIME'] . mt_rand(0, 0xffff);
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getHttpMethod()
46
    {
47
        return 'POST';
48
    }
49
50
    /**
51
     * @param  array $parameters
52
     * @return self
53
     */
54
    public function initialize(array $parameters = array())
55
    {
56
        parent::initialize($parameters);
57
58
        if (null === $this->getRequestId()) {
59
            $this->setRequestId($this->generateRequestId());
60
        }
61
62
        return $this;
63
    }
64
65
    /**
66
     * @param string $value
67
     */
68
    public function setRequestId($value)
69
    {
70
        return $this->setParameter('requestId', $value);
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getRequestId()
77
    {
78
        return $this->getParameter('requestId');
79
    }
80
81
    /**
82
     * @param string $value
83
     */
84
    public function setToken($value)
85
    {
86
        return $this->setParameter('token', $value);
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getToken()
93
    {
94
        return $this->getParameter('token');
95
    }
96
97
    /**
98
     * @param  array                                 $data
99
     * @return \Guzzle\Http\Message\RequestInterface
100
     */
101
    public function getHttpRequest(array $data)
102
    {
103
        return $this->httpClient->createRequest(
104
            $this->getHttpMethod(),
105
            $this->getServer().$this->getEndpoint(),
106
            array(
107
                'Accept'        => 'application/json',
108
                'Authorization' => 'Bearer '.$this->getToken(),
109
                'Content-type'  => 'application/json',
110
            ),
111
            empty($data) ? '{}' : json_encode($data)
112
        );
113
    }
114
115
    /**
116
     * @param  array                         $data
117
     * @return \Guzzle\Http\Message\Response
118
     */
119
    public function sendHttpRequest(array $data)
120
    {
121
        return $this->getHttpRequest($data)->send();
122
    }
123
}
124