Skip to content

Commit

Permalink
Update Hal client
Browse files Browse the repository at this point in the history
- Update Hal Client to add PUT, PATCH, POST, DELETE methods
- Fix MetroAreaClient
- Add missing namespaces in resources
  • Loading branch information
maxab committed May 1, 2015
1 parent 2a5d0b6 commit b1c0524
Show file tree
Hide file tree
Showing 25 changed files with 76 additions and 59 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Composer
vendor
composer.lock
composer.lock

# Tests
test.php
62 changes: 28 additions & 34 deletions src/ViagogoClient.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
<?php

namespace Viagogo;

use Viagogo\Clients as Clients;
use Viagogo\Common as Common;
use Viagogo\Hal\HalClient;

/**
*
*/
class ViagogoClient
{
*
*/
class ViagogoClient {
private $oauthClient;
private $tokenStore;
private $halClient;
Expand All @@ -21,9 +21,9 @@ class ViagogoClient
private $currencyClient;
private $countryClient;
private $languageClient;
private $metroAreaClient;

function __construct($clientId, $clientSecret)
{
function __construct($clientId, $clientSecret) {
$this->oauthClient = new Common\OAuthClient($clientId, $clientSecret);
$this->tokenStore = new Common\OAuthTokenStore();
$this->halClient = new HalClient($this->tokenStore);
Expand All @@ -35,62 +35,56 @@ function __construct($clientId, $clientSecret)
$this->currencyClient = new Clients\CurrencyClient($this->halClient);
$this->countryClient = new Clients\CountryClient($this->halClient);
$this->languageClient = new Clients\LanguageClient($this->halClient);
$this->metroAreaClient = new Clients\MetroAreaClient($this->halClient);
}

public function setToken(Common\OAuthToken $token)
{
public function setToken(Common\OAuthToken $token) {
$this->tokenStore->setToken($token);

return $this;
}

public function getOAuthClient()
{
public function getOAuthClient() {
return $this->oauthClient;
}

public function getHalClient()
{
public function getHalClient() {
return $this->halClient;
}

public function getCategoryClient()
{

public function getCategoryClient() {
return $this->categoryClient;
}

public function getEventClient()
{

public function getEventClient() {
return $this->eventClient;
}

public function getVenueClient()
{

public function getVenueClient() {
return $this->venueClient;
}

public function getListingClient()
{

public function getListingClient() {
return $this->listingClient;
}

public function getSearchClient()
{
public function getSearchClient() {
return $this->searchClient;
}

public function getCountryClient()
{
public function getCountryClient() {
return $this->countryClient;
}

public function getCurrencyClient()
{
public function getCurrencyClient() {
return $this->currencyClient;
}

public function getLanguageClient()
{
public function getLanguageClient() {
return $this->languageClient;
}

public function getMetroAreaClient() {
return $this->metroAreaClient;
}
}
4 changes: 2 additions & 2 deletions src/clients/MetroAreaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public function getMetroArea($metroAreaId, ViagogoRequestParams $params = null)

public function getMetroAreas(ViagogoRequestParams $params = null) {
$root = $this->client->getRoot();
$link = $root->getCurrenciesLink()->getHref();
$link = $root->getMetroAreasLink()->getHref();

return new PagedResource($this->client->getResource($link, $params), 'Viagogo\Resources\MetroArea');
}

public function getAllMetroAreas(ViagogoRequestParams $params = null) {
$root = $this->client->getRoot();
$link = $root->getCurrenciesLink()->getHref();
$link = $root->getMetroAreasLink()->getHref();

return $this->client->getAllResources($link, $params, 'Viagogo\Resources\MetroArea');
}
Expand Down
37 changes: 34 additions & 3 deletions src/hal/HalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public function getResource($url, ViagogoRequestParams $params = null, $type = n

$this->httpClient->setRequestHeader('Authorization', 'Bearer ' . $this->tokenStore->getToken()->getAccessToken());

return $this->httpClient->send($url, "GET", $params->toArray(), null);
$result = $this->httpClient->send($url, "GET", $params->toArray(), null);

return $this->createResource($result, $type);
}

public function getAllResources($url, ViagogoRequestParams $params = null, $type) {
Expand All @@ -66,8 +68,7 @@ public function getAllResources($url, ViagogoRequestParams $params = null, $type

if (isset($page->_embedded->items)) {
foreach ($page->_embedded->items as $item) {
$rc = new \ReflectionClass($type);
$result[] = $rc->newInstanceArgs(array($item));
$result[] = $this->createResource($item, $type);
}
} else {
break;
Expand All @@ -82,4 +83,34 @@ public function getAllResources($url, ViagogoRequestParams $params = null, $type

return $result;
}

public function patch($url, array $requestBody = array(), $type = null) {
$this->httpClient->setRequestHeader('Authorization', 'Bearer ' . $this->tokenStore->getToken()->getAccessToken());
$result = $this->httpClient->send($this->url, "PATCH", array(), $requestBody);

return $this->createResource($result, $type);
}

public function post($url, array $requestBody = array(), $type = null) {
$this->httpClient->setRequestHeader('Authorization', 'Bearer ' . $this->tokenStore->getToken()->getAccessToken());
$result = $this->httpClient->send($this->url, "POST", array(), $requestBody);

return $this->createResource($result, $type);
}

public function put($url, array $requestBody = array(), $type = null) {
$this->httpClient->setRequestHeader('Authorization', 'Bearer ' . $this->tokenStore->getToken()->getAccessToken());
$result = $this->httpClient->send($this->url, "PUT", array(), $requestBody);

return $this->createResource($result, $type);
}

private function createResource($item, $type) {
if ($type === null) {
return $item;
}

$rc = new \ReflectionClass($type);
return $rc->newInstanceArgs(array($item));
}
}
2 changes: 2 additions & 0 deletions src/resources/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Viagogo\Resources;

use Viagogo\Hal\Resource;

/**
*
*/
Expand Down
2 changes: 2 additions & 0 deletions src/resources/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Viagogo\Resources;

use Viagogo\Hal\Resource;

/**
*
*/
Expand Down
2 changes: 2 additions & 0 deletions src/resources/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Viagogo\Resources;

use Viagogo\Hal\Resource;

/**
*
*/
Expand Down
2 changes: 2 additions & 0 deletions src/resources/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Viagogo\Resources;

use Viagogo\Hal\Resource;

/**
*
*/
Expand Down
19 changes: 0 additions & 19 deletions test.php

This file was deleted.

Empty file.
Empty file added tests/hal/LinkTests.php
Empty file.
Empty file.
Empty file added tests/hal/ResourceTests.php
Empty file.
Empty file.
Empty file.
Empty file.
Empty file added tests/resources/EventTests.php
Empty file.
Empty file.
Empty file.
Empty file.
Empty file added tests/resources/MoneyTests.php
Empty file.
Empty file added tests/resources/RootTests.php
Empty file.
Empty file added tests/resources/SearchTests.php
Empty file.
Empty file.
Empty file added tests/resources/VenueTests.php
Empty file.

0 comments on commit b1c0524

Please sign in to comment.