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.
Test Failed
Push — master ( fcfb9f...a4817e )
by sunsky
01:47
created

MultiRequest::prepare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace MultiHttp;
4
5
use MultiHttp\Exception\InvalidArgumentException;
6
7
/**
8
 *
9
 * @author  [email protected] [email protected]
10
 * Date: 2017/6/9
11
 * Time: 15:09
12
 * @version $Id: $
13
 * @since 1.0
14
 * @copyright Sina Corp.
15
 */
16
class MultiRequest
17
{
18
    protected static $requestPool;
19
    protected static $multiHandler;
20
    private static $instance;
21 1
22 1
    protected function __construct()
23 1
    {
24
    }
25
26
    public static function create(): MultiRequest
27 1
    {
28 1
        if (!(self::$instance instanceof self)) {
29 1
            self::$instance = new self;
30
        }
31 1
        self::prepare();
32
        return self::$instance;
33
    }
34
35
    protected static function prepare(): void
36
    {
37
        self::$multiHandler = curl_multi_init();
38
    }
39 1
40 1 View Code Duplication
    public function add($method, $uri, $payload, array $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41 1
    {
42 1
        $options = array(
43 1
                'method' => $method,
44
                'url' => $uri,
45 1
                'data' => $payload,
46
            ) + $options;
47 1
        $this->addOptions(array($options));
48
        return $this;
49
    }
50 1
51
    /**
52 1
     * @param array $URLOptions
53 1
     * example: array(array('url'=>'http://localhost:9999/','timeout'=>1, 'method'=>'POST', 'data'=>'aa=bb&c=d'))
54 1
     * @return $this
55 1
     */
56 1
    public function addOptions(array $URLOptions)
57 1
    {
58
        foreach ($URLOptions as $options) {
59
            $request = Request::create()->addOptions($options)->applyOptions();
60 1
            if (isset($options['callback'])) {
61 1
                $request->onEnd($options['callback']);
62
            }
63 1
            $this->import($request);
64 1
        }
65 1
        return $this;
66
    }
67
68
    public function import(Request $request)
69
    {
70
        if (!is_resource($request->curlHandle)) {
71 1
            throw new InvalidArgumentException('Request curl handle is not initialized');
72 1
        }
73
        curl_multi_add_handle(self::$multiHandler, $request->curlHandle);
74
        self::$requestPool[] = $request;
75
        return $this;
76
    }
77
78
    /**
79
     * @return array(Response)
0 ignored issues
show
Documentation introduced by
The doc-type array(Response) could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
80
     */
81
    public function execute()
82
    {
83
        $sleepTime = 1000;//microsecond, prevent  CPU 100%
84
        do {
85
            curl_multi_exec(self::$multiHandler, $active);
86
            // bug in PHP 5.3.18+ where curl_multi_select can return -1
87 1
            // https://bugs.php.net/bug.php?id=63411
88
            if (curl_multi_select(self::$multiHandler) == -1) {
89
                usleep($sleepTime);
90 1
            }
91 1
            usleep($sleepTime);
92
        } while ($active);
93 1
        $return = array();
94 1
        foreach (self::$requestPool as $request) {
95 1
            $response = $request->makeResponse(true);
96 1
            $func = $response->request->endCallback();
97 1
            if (isset($func)) {
98 1
                $func($response);
99 1
            }
100 1
            $return[] = $response;
101
            curl_multi_remove_handle(self::$multiHandler, $request->curlHandle);
102 1
            curl_close($request->curlHandle);
103 1
        }
104 1
        curl_multi_close(self::$multiHandler);
105
        self::$requestPool = null;
106 1
        return $return;
107 1
    }
108
109
}
110