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.
Passed
Push — master ( 307cce...27531d )
by Jamie
04:17 queued 15s
created

Operator::sendRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
nop 3
crap 2
1
<?php
2
3
namespace OpenStack\Common\Api;
4
5
use function GuzzleHttp\uri_template;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\Promise\Promise;
8
use OpenStack\Common\Resource\ResourceInterface;
9
use OpenStack\Common\Transport\RequestSerializer;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * {@inheritDoc}
14
 */
15
abstract class Operator implements OperatorInterface
16
{
17
    /** @var ClientInterface */
18
    private $client;
19
20
    /** @var ApiInterface */
21
    protected $api;
22
23
    /**
24
     * {@inheritDoc}
25
     */
26 232
    public function __construct(ClientInterface $client, ApiInterface $api)
27
    {
28 232
        $this->client = $client;
29 232
        $this->api = $api;
30 232
    }
31
32
    /**
33
     * Magic method for dictating how objects are rendered when var_dump is called.
34
     * For the benefit of users, extremely verbose and heavy properties (such as HTTP clients) are
35
     * removed to provide easier access to normal state, such as resource attributes.
36
     *
37
     * @codeCoverageIgnore
38
     * @return array
39
     */
40
    public function __debugInfo()
41
    {
42
        $excludedVars = ['client', 'errorBuilder', 'api'];
43
44
        $output = [];
45
46
        foreach (get_object_vars($this) as $key => $val) {
47
            if (!in_array($key, $excludedVars)) {
48
                $output[$key] = $val;
49
            }
50
        }
51
52
        return $output;
53
    }
54
55
    /**
56
     * Retrieves a populated Operation according to the definition and values provided. A
57
     * HTTP client is also injected into the object to allow it to communicate with the remote API.
58
     *
59
     * @param array $definition  The data that dictates how the operation works
60
     *
61
     * @return Operation
62
     */
63 177
    public function getOperation(array $definition)
64
    {
65 177
        return new Operation($definition);
66
    }
67
68 176
    protected function sendRequest(Operation $operation, array $userValues = [], $async = false)
69
    {
70 176
        $uri     = uri_template($operation->getPath(), $userValues);
71 176
        $options = (new RequestSerializer)->serializeOptions($operation, $userValues);
72 176
        $method  = $async ? 'requestAsync' : 'request';
73
74 176
        return $this->client->$method($operation->getMethod(), $uri, $options);
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80 148
    public function execute(array $definition, array $userValues = [], $async = false)
81
    {
82 148
        return $this->sendRequest($this->getOperation($definition), $userValues, $async);
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88 84
    public function model($class, $data = null)
89
    {
90 84
        $model = new $class($this->client, $this->api);
91
92
        // @codeCoverageIgnoreStart
93
        if (!$model instanceof ResourceInterface) {
94
            throw new \RuntimeException(sprintf('%s does not implement %s', $class, ResourceInterface::class));
95
        }
96
        // @codeCoverageIgnoreEnd
97
98 84
        if ($data instanceof ResponseInterface) {
99 3
            $model->populateFromResponse($data);
100 84
        } elseif (is_array($data)) {
101 26
            $model->populateFromArray($data);
102 26
        }
103
104 84
        return $model;
105
    }
106
107
    /**
108
     * Will create a new instance of this class with the current HTTP client and API injected in. This
109
     * is useful when enumerating over a collection since multiple copies of the same resource class
110
     * are needed.
111
     *
112
     * @return static
113
     */
114 32
    public function newInstance()
115
    {
116 32
        return new static($this->client, $this->api);
117
    }
118
119
    /**
120
     * @return \GuzzleHttp\Psr7\Uri
121
     */
122 2
    protected function getHttpBaseUrl()
123
    {
124 2
        return $this->client->getConfig('base_uri');
125
    }
126
127
    /**
128
     * Magic method which intercepts async calls, finds the sequential version, and wraps it in a
129
     * {@see Promise} object. In order for this to happen, the called methods need to be in the
130
     * following format: `createAsync`, where `create` is the sequential method being wrapped.
131
     *
132
     * @param $methodName The name of the method being invoked.
133
     * @param $args       The arguments to be passed to the sequential method.
134
     *
135
     * @throws \RuntimeException If method does not exist
136
     *
137
     * @return Promise
138
     */
139 3
    public function __call($methodName, $args)
140
    {
141
        $e = function ($name) {
142 2
            return new \RuntimeException(sprintf('%s::%s is not defined', $name, get_class($this)));
143 3
        };
144
145 3
        if (substr($methodName, -5) === 'Async') {
146 2
            $realMethod = substr($methodName, 0, -5);
147 2
            if (!method_exists($this, $realMethod)) {
148 1
                throw $e($realMethod);
149
            }
150
151 1
            $promise = new Promise(
152 1
                function () use (&$promise, $realMethod, $args) {
153 1
                    $value = call_user_func_array([$this, $realMethod], $args);
154 1
                    $promise->resolve($value);
155 1
                }
156 1
            );
157
158 1
            return $promise;
159
        }
160
161 1
        throw $e($methodName);
162
    }
163
}