Skip to content

Commit e1cfdfe

Browse files
author
Jamie Hannaford
committed
Merge pull request #1 from php-opencloud/refactor
Refactor
2 parents 4a4aec0 + 1bf701b commit e1cfdfe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+947
-561
lines changed

src/Common/Api/AbstractApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types = 1);
1+
<?php declare (strict_types = 1);
22

33
namespace OpenCloud\Common\Api;
44

src/Common/Api/AbstractParams.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types=1);
1+
<?php declare (strict_types=1);
22

33
namespace OpenCloud\Common\Api;
44

@@ -97,4 +97,4 @@ public function sortKey(): array
9797
'description' => "Sorts by one or more sets of attribute and sort direction combinations.",
9898
];
9999
}
100-
}
100+
}

src/Common/Api/ApiInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types=1);
1+
<?php declare (strict_types=1);
22

33
namespace OpenCloud\Common\Api;
44

src/Common/Api/Operation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types=1);
1+
<?php declare (strict_types=1);
22

33
namespace OpenCloud\Common\Api;
44

@@ -131,4 +131,4 @@ public function validate(array $userValues): bool
131131

132132
return true;
133133
}
134-
}
134+
}

src/Common/Api/OperatorInterface.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types=1);
1+
<?php declare (strict_types=1);
22

33
namespace OpenCloud\Common\Api;
44

@@ -44,11 +44,21 @@ public function execute(array $definition, array $userValues = []): ResponseInte
4444
public function executeAsync(array $definition, array $userValues = []): PromiseInterface;
4545

4646
/**
47-
* @param string $name The name of the model class.
47+
* Retrieves a populated Operation according to the definition and values provided. A
48+
* HTTP client is also injected into the object to allow it to communicate with the remote API.
49+
*
50+
* @param array $definition The data that dictates how the operation works
51+
*
52+
* @return Operation
53+
*/
54+
public function getOperation(array $definition): Operation;
55+
56+
/**
57+
* @param string $class The name of the model class.
4858
* @param mixed $data Either a {@see ResponseInterface} or data array that will populate the newly
4959
* created model class.
5060
*
5161
* @return \OpenCloud\Common\Resource\ResourceInterface
5262
*/
53-
public function model(string $name, $data = null): ResourceInterface;
63+
public function model(string $class, $data = null): ResourceInterface;
5464
}
Lines changed: 46 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types=1);
1+
<?php declare (strict_types=1);
22

33
namespace OpenCloud\Common\Api;
44

@@ -11,10 +11,7 @@
1111
use OpenCloud\Common\Transport\RequestSerializer;
1212
use Psr\Http\Message\ResponseInterface;
1313

14-
/**
15-
* {@inheritDoc}
16-
*/
17-
abstract class Operator implements OperatorInterface
14+
trait OperatorTrait
1815
{
1916
/** @var ClientInterface */
2017
protected $client;
@@ -55,18 +52,58 @@ public function __debugInfo()
5552
}
5653

5754
/**
58-
* Retrieves a populated Operation according to the definition and values provided. A
59-
* HTTP client is also injected into the object to allow it to communicate with the remote API.
55+
* Magic method which intercepts async calls, finds the sequential version, and wraps it in a
56+
* {@see Promise} object. In order for this to happen, the called methods need to be in the
57+
* following format: `createAsync`, where `create` is the sequential method being wrapped.
58+
*
59+
* @param $methodName The name of the method being invoked.
60+
* @param $args The arguments to be passed to the sequential method.
6061
*
61-
* @param array $definition The data that dictates how the operation works
62+
* @throws \RuntimeException If method does not exist
6263
*
63-
* @return Operation
64+
* @return Promise
65+
*/
66+
public function __call($methodName, $args)
67+
{
68+
$e = function ($name) {
69+
return new \RuntimeException(sprintf('%s::%s is not defined', get_class($this), $name));
70+
};
71+
72+
if (substr($methodName, -5) === 'Async') {
73+
$realMethod = substr($methodName, 0, -5);
74+
if (!method_exists($this, $realMethod)) {
75+
throw $e($realMethod);
76+
}
77+
78+
$promise = new Promise(
79+
function () use (&$promise, $realMethod, $args) {
80+
$value = call_user_func_array([$this, $realMethod], $args);
81+
$promise->resolve($value);
82+
}
83+
);
84+
85+
return $promise;
86+
}
87+
88+
throw $e($methodName);
89+
}
90+
91+
/**
92+
* {@inheritdoc}
6493
*/
6594
public function getOperation(array $definition): Operation
6695
{
6796
return new Operation($definition);
6897
}
6998

99+
/**
100+
* @param Operation $operation
101+
* @param array $userValues
102+
* @param bool $async
103+
*
104+
* @return mixed
105+
* @throws \Exception
106+
*/
70107
protected function sendRequest(Operation $operation, array $userValues = [], bool $async = false)
71108
{
72109
$operation->validate($userValues);
@@ -100,76 +137,16 @@ public function executeAsync(array $definition, array $userValues = []): Promise
100137
public function model(string $class, $data = null): ResourceInterface
101138
{
102139
$model = new $class($this->client, $this->api);
103-
104140
// @codeCoverageIgnoreStart
105141
if (!$model instanceof ResourceInterface) {
106142
throw new \RuntimeException(sprintf('%s does not implement %s', $class, ResourceInterface::class));
107143
}
108144
// @codeCoverageIgnoreEnd
109-
110145
if ($data instanceof ResponseInterface) {
111146
$model->populateFromResponse($data);
112147
} elseif (is_array($data)) {
113148
$model->populateFromArray($data);
114149
}
115-
116150
return $model;
117151
}
118-
119-
/**
120-
* Will create a new instance of this class with the current HTTP client and API injected in. This
121-
* is useful when enumerating over a collection since multiple copies of the same resource class
122-
* are needed.
123-
*
124-
* @return static
125-
*/
126-
public function newInstance(): self
127-
{
128-
return new static($this->client, $this->api);
129-
}
130-
131-
/**
132-
* @return \GuzzleHttp\Psr7\Uri:null
133-
*/
134-
protected function getHttpBaseUrl()
135-
{
136-
return $this->client->getConfig('base_uri');
137-
}
138-
139-
/**
140-
* Magic method which intercepts async calls, finds the sequential version, and wraps it in a
141-
* {@see Promise} object. In order for this to happen, the called methods need to be in the
142-
* following format: `createAsync`, where `create` is the sequential method being wrapped.
143-
*
144-
* @param $methodName The name of the method being invoked.
145-
* @param $args The arguments to be passed to the sequential method.
146-
*
147-
* @throws \RuntimeException If method does not exist
148-
*
149-
* @return Promise
150-
*/
151-
public function __call($methodName, $args)
152-
{
153-
$e = function ($name) {
154-
return new \RuntimeException(sprintf('%s::%s is not defined', get_class($this), $name));
155-
};
156-
157-
if (substr($methodName, -5) === 'Async') {
158-
$realMethod = substr($methodName, 0, -5);
159-
if (!method_exists($this, $realMethod)) {
160-
throw $e($realMethod);
161-
}
162-
163-
$promise = new Promise(
164-
function () use (&$promise, $realMethod, $args) {
165-
$value = call_user_func_array([$this, $realMethod], $args);
166-
$promise->resolve($value);
167-
}
168-
);
169-
170-
return $promise;
171-
}
172-
173-
throw $e($methodName);
174-
}
175152
}

src/Common/Api/Parameter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types=1);
1+
<?php declare (strict_types=1);
22

33
namespace OpenCloud\Common\Api;
44

@@ -240,7 +240,7 @@ private function validateObject($userValues)
240240
/**
241241
* Internal method which retrieves a nested property for object parameters.
242242
*
243-
* @param $key The name of the child parameter
243+
* @param string $key The name of the child parameter
244244
*
245245
* @returns Parameter
246246
* @throws \Exception

src/Common/ArrayAccessTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types=1);
1+
<?php declare (strict_types=1);
22

33
namespace OpenCloud\Common;
44

src/Common/Auth/AuthHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types=1);
1+
<?php declare (strict_types=1);
22

33
namespace OpenCloud\Common\Auth;
44

src/Common/Auth/Catalog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php declare(strict_types=1);
1+
<?php declare (strict_types=1);
22

33
namespace OpenCloud\Common\Auth;
44

0 commit comments

Comments
 (0)