Skip to content

Commit

Permalink
chore: better type hinting
Browse files Browse the repository at this point in the history
  • Loading branch information
batopa committed Jun 3, 2020
1 parent 7f235e7 commit 710ad05
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
33 changes: 25 additions & 8 deletions src/Controller/ApiProxyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Cake\Http\Exception\MethodNotAllowedException;
use Cake\Routing\Router;
use Cake\Utility\Hash;
use Cake\View\ViewVarsTrait;

/**
* Use this Trait in a controller to directly proxy requests to BE4 API.
Expand All @@ -36,6 +37,22 @@
*/
trait ApiProxyTrait
{
use ViewVarsTrait;

/**
* An instance of a \Cake\Http\ServerRequest object that contains information about the current request.
*
* @var \Cake\Http\ServerRequest
*/
protected $request;

/**
* An instance of a Response object that contains information about the impending response.
*
* @var \Cake\Http\Response
*/
protected $response;

/**
* BEdita4 API client
*
Expand Down Expand Up @@ -88,7 +105,7 @@ protected function setBaseUrl($path): void
public function get($path = '')
{
$this->setBaseUrl($path);
$this->request([
$this->apiRequest([
'method' => 'get',
'path' => $path,
'query' => $this->request->getQueryParams(),
Expand All @@ -108,7 +125,7 @@ public function get($path = '')
* @param array $options The request options
* @return void
*/
protected function request(array $options): void
protected function apiRequest(array $options): void
{
$options += [
'method' => '',
Expand Down Expand Up @@ -193,13 +210,13 @@ protected function maskResponseLinks(array $response): array
$response = $this->maskMultiLinks($response, 'meta.resources', 'href');
}

$data = Hash::get($response, 'data');
$data = (array)Hash::get($response, 'data');
if (empty($data)) {
return $response;
}

if (Hash::numeric(array_keys($data))) {
foreach ($data as $key => &$item) {
foreach ($data as &$item) {
$item = $this->maskLinks($item, 'links');
$item = $this->maskMultiLinks($item);
}
Expand All @@ -208,7 +225,7 @@ protected function maskResponseLinks(array $response): array
$response['data']['relationships'] = $this->maskMultiLinks($data);
}

return $response;
return (array)$response;
}

/**
Expand All @@ -220,7 +237,7 @@ protected function maskResponseLinks(array $response): array
* @param string $key The key on which are the links
* @return array
*/
protected function maskMultiLinks(array $data, $path = 'relationships', $key = 'links'): array
protected function maskMultiLinks(array $data, string $path = 'relationships', string $key = 'links'): array
{
$relationships = Hash::get($data, $path, []);
foreach ($relationships as &$rel) {
Expand All @@ -233,11 +250,11 @@ protected function maskMultiLinks(array $data, $path = 'relationships', $key = '
/**
* Mask links found in `$path`
*
* @param array|string $data The data with links to mask
* @param array $data The data with links to mask
* @param string $path The path to search for
* @return array
*/
protected function maskLinks($data, $path): array
protected function maskLinks(array $data, string $path): array
{
$links = Hash::get($data, $path, []);
if (empty($links)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Controller/ApiProxyTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected function getBaseUrl(): string
* @covers ::initialize()
* @covers ::get()
* @covers ::setBaseUrl()
* @covers ::request()
* @covers ::apiRequest()
* @covers ::maskResponseLinks()
* @covers ::maskMultiLinks()
* @covers ::maskLinks()
Expand Down Expand Up @@ -121,7 +121,7 @@ public function testGet(): void
* @return void
*
* @covers ::get()
* @covers ::request()
* @covers ::apiRequest()
* @covers ::handleError()
*/
public function testNotFoundError(): void
Expand Down

0 comments on commit 710ad05

Please sign in to comment.