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 ( b7663d...658651 )
by Jamie
08:03
created

Operator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 95.24%

Importance

Changes 13
Bugs 2 Features 4
Metric Value
wmc 18
c 13
b 2
f 4
lcom 1
cbo 5
dl 0
loc 157
ccs 40
cts 42
cp 0.9524
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __debugInfo() 0 14 3
A getOperation() 0 4 1
A sendRequest() 0 8 2
A execute() 0 4 1
A executeAsync() 0 4 1
A model() 0 18 4
A newInstance() 0 4 1
A getHttpBaseUrl() 0 4 1
B __call() 0 24 3
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 = [])
81
    {
82 148
        return $this->sendRequest($this->getOperation($definition), $userValues);
83
    }
84
    
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function executeAsync(array $definition, array $userValues = [])
89
    {
90
        return $this->sendRequest($this->getOperation($definition), $userValues, true);
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96 84
    public function model($class, $data = null)
97
    {
98 84
        $model = new $class($this->client, $this->api);
99
100
        // @codeCoverageIgnoreStart
101
        if (!$model instanceof ResourceInterface) {
102
            throw new \RuntimeException(sprintf('%s does not implement %s', $class, ResourceInterface::class));
103
        }
104
        // @codeCoverageIgnoreEnd
105
106 84
        if ($data instanceof ResponseInterface) {
107 3
            $model->populateFromResponse($data);
108 84
        } elseif (is_array($data)) {
109 26
            $model->populateFromArray($data);
110 26
        }
111
112 84
        return $model;
113
    }
114
115
    /**
116
     * Will create a new instance of this class with the current HTTP client and API injected in. This
117
     * is useful when enumerating over a collection since multiple copies of the same resource class
118
     * are needed.
119
     *
120
     * @return static
121
     */
122 32
    public function newInstance()
123
    {
124 32
        return new static($this->client, $this->api);
125
    }
126
127
    /**
128
     * @return \GuzzleHttp\Psr7\Uri
129
     */
130 2
    protected function getHttpBaseUrl()
131
    {
132 2
        return $this->client->getConfig('base_uri');
133
    }
134
135
    /**
136
     * Magic method which intercepts async calls, finds the sequential version, and wraps it in a
137
     * {@see Promise} object. In order for this to happen, the called methods need to be in the
138
     * following format: `createAsync`, where `create` is the sequential method being wrapped.
139
     *
140
     * @param $methodName The name of the method being invoked.
141
     * @param $args       The arguments to be passed to the sequential method.
142
     *
143
     * @throws \RuntimeException If method does not exist
144
     *
145
     * @return Promise
146
     */
147 3
    public function __call($methodName, $args)
148
    {
149
        $e = function ($name) {
150 2
            return new \RuntimeException(sprintf('%s::%s is not defined', $name, get_class($this)));
151 3
        };
152
153 3
        if (substr($methodName, -5) === 'Async') {
154 2
            $realMethod = substr($methodName, 0, -5);
155 2
            if (!method_exists($this, $realMethod)) {
156 1
                throw $e($realMethod);
157
            }
158
159 1
            $promise = new Promise(
160 1
                function () use (&$promise, $realMethod, $args) {
161 1
                    $value = call_user_func_array([$this, $realMethod], $args);
162 1
                    $promise->resolve($value);
163 1
                }
164 1
            );
165
166 1
            return $promise;
167
        }
168
169 1
        throw $e($methodName);
170
    }
171
}