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
01:50
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 ReflectionObject;
22
23
/**
24
 * Class RequestFactory
25
 *
26
 * @package WyriHaximus\React\Guzzle\HttpClient
27
 */
28
class RequestFactory
29
{
30
    /**
31
     *
32
     * @param RequestInterface $request
33
     * @param array $options
34
     * @param $resolver Resolver
35
     * @param HttpClient $httpClient
36
     * @param LoopInterface $loop
37
     * @return \React\Promise\Promise
38
     */
39 6
    public function create(
40
        RequestInterface $request,
41
        array $options,
42
        Resolver $resolver,
43
        HttpClient $httpClient,
44
        LoopInterface $loop
45
    ) {
46 6
        $options = $this->convertOptions($options);
47
48 6
        if (isset($options['delay'])) {
49
            $promise = \WyriHaximus\React\timedPromise($loop, $options['delay']);
50
        }
51 6
        if (!isset($promise)) {
52 6
            $promise = \WyriHaximus\React\futurePromise($loop);
53 6
        }
54
        
55 6
        return $promise->then(function () use (
56 6
            $request,
57 6
            $options,
58 6
            $resolver,
59 6
            $httpClient,
60 6
            $loop
61
        ) {
62 6
            $sender = $this->createSender($options, $resolver, $httpClient, $loop);
63 6
            return (new Browser($loop, $sender))
64 6
                ->withOptions($options)
65 6
                ->send($request);
66 6
        });
67
    }
68
69
    /**
70
     * @param array $options
71
     * @param HttpClient $httpClient
72
     * @param LoopInterface $loop
73
     * @return Sender
74
     */
75 6
    protected function createSender(array $options, Resolver $resolver, HttpClient $httpClient, LoopInterface $loop)
76
    {
77 6
        $connector = $this->getProperty($httpClient, 'connector');
78
79 6
        if (isset($options['proxy'])) {
80 6
            switch (parse_url($options['proxy'], PHP_URL_SCHEME)) {
81 5
                case 'http':
82 1
                    $connector = new HttpProxyClient($options['proxy'], $connector);
83 1
                    break;
84 4
                case 'socks':
85 1
                    $connector = $this->createSocksProxy(
86 1
                        $options['proxy'],
87 1
                        $loop,
88 1
                        $connector,
89
                        $resolver
90 1
                    );
91 1
                    break;
92 3
                case 'socks4':
93 3
                case 'socks4a':
94 2
                    $connector = $this->createSocksProxy(
95 2
                        $options['proxy'],
96 2
                        $loop,
97 2
                        $connector,
98 2
                        $resolver,
99
                        4
100 2
                    );
101 2
                    break;
102 1
                case 'socks5':
103 1
                    $connector = $this->createSocksProxy(
104 1
                        $options['proxy'],
105 1
                        $loop,
106 1
                        $connector,
107 1
                        $resolver,
108
                        5
109 1
                    );
110 1
                    break;
111 5
            }
112 5
        }
113
114 6
        return Sender::createFromLoopConnectors($loop, $connector);
115
    }
116
117 4
    protected function createSocksProxy(
118
        $url,
119
        $loop,
120
        $connector,
121
        $resolver,
122
        $version = null
123
    ) {
124 4
        $proxyClient = new SocksProxyClient(
125 4
            $url,
126 4
            $loop,
127 4
            $connector,
128
            $resolver
129 4
        );
130 4
        if ($version !== null) {
131 3
            $proxyClient->setProtocolVersion($version);
132 3
        }
133 4
        return $proxyClient->createConnector();
134
    }
135
136
    /**
137
     * @param array $options
138
     * @return array
139
     */
140 6
    protected function convertOptions(array $options)
141
    {
142
        // provides backwards compatibility for Guzzle 3-5.
143 6
        if (isset($options['client'])) {
144
            $options = array_merge($options, $options['client']);
145
            unset($options['client']);
146
        }
147
148
        // provides for backwards compatibility for Guzzle 3-5
149 6
        if (isset($options['save_to'])) {
150 1
            $options['sink'] = $options['save_to'];
151
            unset($options['save_to']);
152
        }
153
154 6
        if (isset($options['allow_redirects'])) {
155
            $this->convertRedirectOption($options);
156
        }
157
158 6
        return $options;
159
    }
160
161
    protected function convertRedirectOption(&$options)
162
    {
163
        $option = $options['allow_redirects'];
164
        unset($options['allow_redirects']);
165
166
        if (is_bool($option)) {
167
            $options['followRedirects'] = $option;
168
            return;
169
        }
170
171
        if (is_array($option)) {
172
            if (isset($option['max'])) {
173
                $options['maxRedirects'] = $option['max'];
174
            }
175
            $options['followRedirects'] = true;
176
            return;
177
        }
178
    }
179
180
    /**
181
     * @param object $object
182
     * @param string $desiredProperty
183
     * @return mixed
184
     */
185 6
    protected function getProperty($object, $desiredProperty)
186
    {
187 6
        $reflection = new ReflectionObject($object);
188 6
        $property = $reflection->getProperty($desiredProperty);
189 6
        $property->setAccessible(true);
190 6
        return $property->getValue($object);
191
    }
192
}
193