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.
Completed
Pull Request — master (#6)
by Cees-Jan
03:09
created

RequestFactory::createSocksProxy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 5
crap 2
1
<?php
2
3
/**
4
 * This file is part of ReactGuzzleRing.
5
 *
6
 ** (c) 2014 Cees-Jan Kiewiet
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace WyriHaximus\React\Guzzle\HttpClient;
12
13
use Clue\React\Buzz\Browser;
14
use Clue\React\Buzz\Io\Sender;
15
use Clue\React\HttpProxy\ProxyConnector as HttpProxyClient;
16
use Clue\React\Socks\Client as SocksProxyClient;
17
use Psr\Http\Message\RequestInterface;
18
use React\Dns\Resolver\Resolver;
19
use React\EventLoop\LoopInterface;
20
use React\HttpClient\Client as HttpClient;
21
use React\SocketClient\TimeoutConnector;
22
use ReflectionObject;
23
24
/**
25
 * Class RequestFactory
26
 *
27
 * @package WyriHaximus\React\Guzzle\HttpClient
28
 */
29
class RequestFactory
30
{
31
    /**
32
     *
33
     * @param RequestInterface $request
34
     * @param array $options
35
     * @param $resolver Resolver
36
     * @param HttpClient $httpClient
37
     * @param LoopInterface $loop
38
     * @return \React\Promise\Promise
39
     */
40 6
    public function create(
41
        RequestInterface $request,
42
        array $options,
43
        Resolver $resolver,
44
        HttpClient $httpClient,
45
        LoopInterface $loop
46
    ) {
47 6
        $options = $this->convertOptions($options);
48
49 6
        if (isset($options['delay'])) {
50
            $promise = \WyriHaximus\React\timedPromise($loop, $options['delay']);
51
        }
52 6
        if (!isset($promise)) {
53 6
            $promise = \WyriHaximus\React\futurePromise($loop);
54 6
        }
55
        
56 6
        return $promise->then(function () use (
57 6
            $request,
58 6
            $options,
59 6
            $resolver,
60 6
            $httpClient,
61 6
            $loop
62
        ) {
63 6
            $sender = $this->createSender($options, $resolver, $httpClient, $loop);
64 6
            return (new Browser($loop, $sender))
65 6
                ->withOptions($options)
66 6
                ->send($request);
67 6
        });
68
    }
69
70
    /**
71
     * @param array $options
72
     * @param HttpClient $httpClient
73
     * @param LoopInterface $loop
74
     * @return Sender
75
     */
76 6
    protected function createSender(array $options, Resolver $resolver, HttpClient $httpClient, LoopInterface $loop)
77
    {
78 6
        $connector = $this->getProperty($httpClient, 'connector');
79
80 6
        if (isset($options['proxy'])) {
81 5
            switch (parse_url($options['proxy'], PHP_URL_SCHEME)) {
82 5
                case 'http':
83 1
                    $connector = new HttpProxyClient($options['proxy'], $connector);
84 1
                    break;
85 4
                case 'socks':
86 1
                    $connector = $this->createSocksProxy(
87 1
                        $options['proxy'],
88 1
                        $loop,
89 1
                        $connector,
90
                        $resolver
91 1
                    );
92 1
                    break;
93 3
                case 'socks4':
94 3
                case 'socks4a':
95 2
                    $connector = $this->createSocksProxy(
96 2
                        $options['proxy'],
97 2
                        $loop,
98 2
                        $connector,
99 2
                        $resolver,
100
                        4
101 2
                    );
102 2
                    break;
103 1
                case 'socks5':
104 1
                    $connector = $this->createSocksProxy(
105 1
                        $options['proxy'],
106 1
                        $loop,
107 1
                        $connector,
108 1
                        $resolver,
109
                        5
110 1
                    );
111 1
                    break;
112 5
            }
113 5
        }
114
115 6
        if (isset($options['connect_timeout'])) {
116
            $connector = new TimeoutConnector($connector, $options['connect_timeout'], $loop);
117
        }
118
119 6
        return Sender::createFromLoopConnectors($loop, $connector);
120
    }
121
122 4
    protected function createSocksProxy(
123
        $url,
124
        $loop,
125
        $connector,
126
        $resolver,
127
        $version = null
128
    ) {
129 4
        $proxyClient = new SocksProxyClient(
130 4
            $url,
131 4
            $loop,
132 4
            $connector,
133
            $resolver
134 4
        );
135 4
        if ($version !== null) {
136 3
            $proxyClient->setProtocolVersion($version);
137 3
        }
138 4
        return $proxyClient->createConnector();
139
    }
140
141
    /**
142
     * @param array $options
143
     * @return array
144
     */
145 6
    protected function convertOptions(array $options)
146
    {
147
        // provides backwards compatibility for Guzzle 3-5.
148 6
        if (isset($options['client'])) {
149
            $options = array_merge($options, $options['client']);
150 1
            unset($options['client']);
151
        }
152
153
        // provides for backwards compatibility for Guzzle 3-5
154 6
        if (isset($options['save_to'])) {
155
            $options['sink'] = $options['save_to'];
156
            unset($options['save_to']);
157
        }
158
159 6
        if (isset($options['allow_redirects'])) {
160
            $this->convertRedirectOption($options);
161
        }
162
163 6
        return $options;
164
    }
165
166
    protected function convertRedirectOption(&$options)
167
    {
168
        $option = $options['allow_redirects'];
169
        unset($options['allow_redirects']);
170
171
        if (is_bool($option)) {
172
            $options['followRedirects'] = $option;
173
            return;
174
        }
175
176
        if (is_array($option)) {
177
            if (isset($option['max'])) {
178
                $options['maxRedirects'] = $option['max'];
179
            }
180
            $options['followRedirects'] = true;
181
            return;
182
        }
183
    }
184
185
    /**
186
     * @param object $object
187
     * @param string $desiredProperty
188
     * @return mixed
189
     */
190 6
    protected function getProperty($object, $desiredProperty)
191
    {
192 6
        $reflection = new ReflectionObject($object);
193 6
        $property = $reflection->getProperty($desiredProperty);
194 6
        $property->setAccessible(true);
195 6
        return $property->getValue($object);
196
    }
197
}
198