Skip to content

Commit

Permalink
Merge pull request #1 from php-opencloud/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
Jamie Hannaford committed Apr 11, 2016
2 parents 4a4aec0 + 1bf701b commit e1cfdfe
Show file tree
Hide file tree
Showing 58 changed files with 947 additions and 561 deletions.
2 changes: 1 addition & 1 deletion src/Common/Api/AbstractApi.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare (strict_types = 1);

namespace OpenCloud\Common\Api;

Expand Down
4 changes: 2 additions & 2 deletions src/Common/Api/AbstractParams.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Api;

Expand Down Expand Up @@ -97,4 +97,4 @@ public function sortKey(): array
'description' => "Sorts by one or more sets of attribute and sort direction combinations.",
];
}
}
}
2 changes: 1 addition & 1 deletion src/Common/Api/ApiInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Api;

Expand Down
4 changes: 2 additions & 2 deletions src/Common/Api/Operation.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Api;

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

return true;
}
}
}
16 changes: 13 additions & 3 deletions src/Common/Api/OperatorInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Api;

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

/**
* @param string $name The name of the model class.
* Retrieves a populated Operation according to the definition and values provided. A
* HTTP client is also injected into the object to allow it to communicate with the remote API.
*
* @param array $definition The data that dictates how the operation works
*
* @return Operation
*/
public function getOperation(array $definition): Operation;

/**
* @param string $class The name of the model class.
* @param mixed $data Either a {@see ResponseInterface} or data array that will populate the newly
* created model class.
*
* @return \OpenCloud\Common\Resource\ResourceInterface
*/
public function model(string $name, $data = null): ResourceInterface;
public function model(string $class, $data = null): ResourceInterface;
}
115 changes: 46 additions & 69 deletions src/Common/Api/Operator.php → src/Common/Api/OperatorTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Api;

Expand All @@ -11,10 +11,7 @@
use OpenCloud\Common\Transport\RequestSerializer;
use Psr\Http\Message\ResponseInterface;

/**
* {@inheritDoc}
*/
abstract class Operator implements OperatorInterface
trait OperatorTrait
{
/** @var ClientInterface */
protected $client;
Expand Down Expand Up @@ -55,18 +52,58 @@ public function __debugInfo()
}

/**
* Retrieves a populated Operation according to the definition and values provided. A
* HTTP client is also injected into the object to allow it to communicate with the remote API.
* Magic method which intercepts async calls, finds the sequential version, and wraps it in a
* {@see Promise} object. In order for this to happen, the called methods need to be in the
* following format: `createAsync`, where `create` is the sequential method being wrapped.
*
* @param $methodName The name of the method being invoked.
* @param $args The arguments to be passed to the sequential method.
*
* @param array $definition The data that dictates how the operation works
* @throws \RuntimeException If method does not exist
*
* @return Operation
* @return Promise
*/
public function __call($methodName, $args)
{
$e = function ($name) {
return new \RuntimeException(sprintf('%s::%s is not defined', get_class($this), $name));
};

if (substr($methodName, -5) === 'Async') {
$realMethod = substr($methodName, 0, -5);
if (!method_exists($this, $realMethod)) {
throw $e($realMethod);
}

$promise = new Promise(
function () use (&$promise, $realMethod, $args) {
$value = call_user_func_array([$this, $realMethod], $args);
$promise->resolve($value);
}
);

return $promise;
}

throw $e($methodName);
}

/**
* {@inheritdoc}
*/
public function getOperation(array $definition): Operation
{
return new Operation($definition);
}

/**
* @param Operation $operation
* @param array $userValues
* @param bool $async
*
* @return mixed
* @throws \Exception
*/
protected function sendRequest(Operation $operation, array $userValues = [], bool $async = false)
{
$operation->validate($userValues);
Expand Down Expand Up @@ -100,76 +137,16 @@ public function executeAsync(array $definition, array $userValues = []): Promise
public function model(string $class, $data = null): ResourceInterface
{
$model = new $class($this->client, $this->api);

// @codeCoverageIgnoreStart
if (!$model instanceof ResourceInterface) {
throw new \RuntimeException(sprintf('%s does not implement %s', $class, ResourceInterface::class));
}
// @codeCoverageIgnoreEnd

if ($data instanceof ResponseInterface) {
$model->populateFromResponse($data);
} elseif (is_array($data)) {
$model->populateFromArray($data);
}

return $model;
}

/**
* Will create a new instance of this class with the current HTTP client and API injected in. This
* is useful when enumerating over a collection since multiple copies of the same resource class
* are needed.
*
* @return static
*/
public function newInstance(): self
{
return new static($this->client, $this->api);
}

/**
* @return \GuzzleHttp\Psr7\Uri:null
*/
protected function getHttpBaseUrl()
{
return $this->client->getConfig('base_uri');
}

/**
* Magic method which intercepts async calls, finds the sequential version, and wraps it in a
* {@see Promise} object. In order for this to happen, the called methods need to be in the
* following format: `createAsync`, where `create` is the sequential method being wrapped.
*
* @param $methodName The name of the method being invoked.
* @param $args The arguments to be passed to the sequential method.
*
* @throws \RuntimeException If method does not exist
*
* @return Promise
*/
public function __call($methodName, $args)
{
$e = function ($name) {
return new \RuntimeException(sprintf('%s::%s is not defined', get_class($this), $name));
};

if (substr($methodName, -5) === 'Async') {
$realMethod = substr($methodName, 0, -5);
if (!method_exists($this, $realMethod)) {
throw $e($realMethod);
}

$promise = new Promise(
function () use (&$promise, $realMethod, $args) {
$value = call_user_func_array([$this, $realMethod], $args);
$promise->resolve($value);
}
);

return $promise;
}

throw $e($methodName);
}
}
4 changes: 2 additions & 2 deletions src/Common/Api/Parameter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Api;

Expand Down Expand Up @@ -240,7 +240,7 @@ private function validateObject($userValues)
/**
* Internal method which retrieves a nested property for object parameters.
*
* @param $key The name of the child parameter
* @param string $key The name of the child parameter
*
* @returns Parameter
* @throws \Exception
Expand Down
2 changes: 1 addition & 1 deletion src/Common/ArrayAccessTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common;

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Auth/AuthHandler.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Auth;

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Auth/Catalog.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Auth;

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Auth/IdentityService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Auth;

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Auth/Token.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Auth;

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Error/BadResponseError.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Error;

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Error/BaseError.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Error;

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Error/Builder.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Error;

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Error/NotImplementedError.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Error;

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Error/UserInputError.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\Error;

Expand Down
8 changes: 4 additions & 4 deletions src/Common/HydratorStrategyTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common;

Expand All @@ -15,17 +15,17 @@ trait HydratorStrategyTrait
* @param array $data The data to set
* @param array $aliases Any aliases
*/
private function hydrate(array $data, array $aliases = [])
public function hydrate(array $data, array $aliases = [])
{
foreach ($data as $key => $val) {
$key = isset($aliases[$key]) ? $aliases[$key] : $key;
if (property_exists($this, $key)) {
$this->$key = $val;
$this->{$key} = $val;
}
}
}

private function set(string $key, $property, array $data, callable $fn = null)
public function set(string $key, $property, array $data, callable $fn = null)
{
if (isset($data[$key]) && property_exists($this, $property)) {
$value = $fn ? call_user_func($fn, $data[$key]) : $data[$key];
Expand Down
2 changes: 1 addition & 1 deletion src/Common/JsonPath.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types = 1);
<?php declare (strict_types = 1);

namespace OpenCloud\Common;

Expand Down
2 changes: 1 addition & 1 deletion src/Common/JsonSchema/JsonPatch.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\JsonSchema;

Expand Down
2 changes: 1 addition & 1 deletion src/Common/JsonSchema/Schema.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php declare (strict_types=1);

namespace OpenCloud\Common\JsonSchema;

Expand Down
Loading

0 comments on commit e1cfdfe

Please sign in to comment.