GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AbstractRequest::getServer()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 6
eloc 18
nc 6
nop 0
1
<?php
2
3
namespace Lexik\Bundle\PayboxBundle\Paybox;
4
5
use Lexik\Bundle\PayboxBundle\Paybox\System\Tools;
6
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
8
9
/**
10
 * Class AbstractRequest
11
 *
12
 * @package Lexik\Bundle\PayboxBundle\Paybox
13
 *
14
 * @author Lexik <[email protected]>
15
 * @author Olivier Maisonneuve <[email protected]>
16
 */
17
abstract class AbstractRequest implements RequestInterface
18
{
19
    /**
20
     * Array of the transaction's parameters.
21
     *
22
     * @var array
23
     */
24
    protected $parameters;
25
26
    /**
27
     * Array of globals parameters.
28
     *
29
     * @var array
30
     */
31
    protected $globals;
32
33
    /**
34
     * Array of servers informations.
35
     *
36
     * @var array
37
     */
38
    protected $servers;
39
40
    /**
41
     * Constructor.
42
     *
43
     * @param  array $parameters
44
     * @param  array $servers
45
     */
46
    public function __construct(array $parameters, array $servers)
47
    {
48
        $this->parameters = array();
49
        $this->globals    = array();
50
        $this->servers    = $servers;
51
52
        $this->initGlobals($parameters);
53
        $this->initParameters();
54
    }
55
56
    /**
57
     * Initialize the object with the defaults values.
58
     *
59
     * @param array $parameters
60
     */
61
    abstract protected function initGlobals(array $parameters);
62
63
    /**
64
     * Initialize defaults parameters with globals.
65
     */
66
    abstract protected function initParameters();
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function setParameter($name, $value)
72
    {
73
        $this->parameters[$name] = $value;
74
75
        return $this;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function setParameters(array $parameters)
82
    {
83
        foreach ($parameters as $name => $value) {
84
            $this->setParameter($name, $value);
85
        }
86
87
        return $this;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getParameter($name)
94
    {
95
        return (isset($this->parameters[strtoupper($name)])) ? $this->parameters[strtoupper($name)] : null;
96
    }
97
98
    /**
99
     * Returns all parameters as a querystring.
100
     *
101
     * @return string
102
     */
103
    protected function stringifyParameters()
104
    {
105
        if (isset($this->parameters['PBX_HMAC'])) {
106
            unset($this->parameters['PBX_HMAC']);
107
        }
108
109
        ksort($this->parameters);
110
111
        return Tools::stringify($this->parameters);
112
    }
113
114
    /**
115
     * Computes the hmac hash.
116
     *
117
     * @return string
118
     */
119
    protected function computeHmac()
120
    {
121
        $binKey = pack('H*', $this->globals['hmac_key']);
122
123
        return hash_hmac($this->globals['hmac_algorithm'], $this->stringifyParameters(), $binKey);
124
    }
125
126
    /**
127
     * Returns the url of an available server.
128
     *
129
     * @return array
130
     *
131
     * @throws InvalidArgumentException If the specified environment is not valid (dev/prod).
132
     * @throws RuntimeException         If no server is available.
133
     */
134
    protected function getServer()
135
    {
136
        $servers = array();
137
138
        if (isset($this->globals['production']) && (true === $this->globals['production'])) {
139
            $servers[] = $this->servers['primary'];
140
            $servers[] = $this->servers['secondary'];
141
        } else {
142
            $servers[] = $this->servers['preprod'];
143
        }
144
145
        foreach ($servers as $server) {
146
            $doc = new \DOMDocument();
147
            $doc->loadHTML($this->getWebPage(sprintf(
148
                '%s://%s%s',
149
                $server['protocol'],
150
                $server['host'],
151
                $server['test_path']
152
            )));
153
            $element = $doc->getElementById('server_status');
154
155
            if ($element && 'OK' == $element->textContent) {
156
                return $server;
157
            }
158
        }
159
160
        throw new RuntimeException('No server available.');
161
    }
162
163
    /**
164
     * Returns the content of a web resource.
165
     *
166
     * @param  string $url
167
     *
168
     * @return string
169
     */
170
    protected function getWebPage($url)
171
    {
172
        $curl = curl_init();
173
174
        curl_setopt($curl, CURLOPT_URL,            $url);
175
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
176
        curl_setopt($curl, CURLOPT_HEADER,         false);
177
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
178
        $output = curl_exec($curl);
179
        curl_close($curl);
180
181
        return (string) $output;
182
    }
183
}
184