Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Previewsdk #12

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/ClientBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ abstract class ClientBase {
protected $accessToken;
protected $endpointBase;
protected $cacher;
protected $assoc = true;

function __construct($accessToken, $spaceId = null, CacherInterface $cacher = null) {
function __construct($accessToken, $spaceId = null, CacherInterface $cacher = null, $assoc = true) {
$this->accessToken = $accessToken;
$this->spaceId = $spaceId;
$this->setClient(new GuzzleHttp\Client());
$this->cacher = $cacher;
$this->assoc = $assoc;
}

/**
Expand Down Expand Up @@ -60,15 +62,18 @@ function getEndpoint() {
* @param array $headers
* @return mixed
*/

function get($resource, $query = array(), $headers = array()) {

$url = $this->build_url($resource, $query);
$key = $this->buildCacheKey('get', $resource, $url, $headers, $query);
if (($this->cacher) && ($this->cacher->has($key))) return $this->cacher->get($key);
//echo $url;exit;
$result = $this->client->get($url, [
'headers' => array_merge([
'Authorization' => $this->getBearer()
], $headers)
])->json();
])->json(['object' => !$this->assoc]);
if ($this->cacher) $this->cacher->put($key, $result);
return $result;
}
Expand Down
8 changes: 4 additions & 4 deletions src/DeliverySDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DeliverySDK extends SDKBase {
*/
function assets()
{
return new Assets($this->accessToken, $this->spaceId, $this->cacher);
return new Assets($this->client);
}

/**
Expand All @@ -31,7 +31,7 @@ function assets()
*/
function contentTypes()
{
return new ContentTypes($this->accessToken, $this->spaceId, $this->cacher);
return new ContentTypes($this->client);
}

/**
Expand All @@ -40,7 +40,7 @@ function contentTypes()
*/
function entries()
{
return new Entries($this->accessToken, $this->spaceId, $this->cacher);
return new Entries($this->client);
}

/**
Expand All @@ -49,7 +49,7 @@ function entries()
*/
function spaces()
{
return new Spaces($this->accessToken, $this->spaceId, $this->cacher);
return new Spaces($this->client);
}

}
2 changes: 1 addition & 1 deletion src/ManagementClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function get($resource, $query = array(), $headers = array()) {
'headers' => array_merge([
'Authorization' => $this->getBearer()
], $headers)
])->json();
])->json(['object' => !$this->assoc]);
return $result;
}

Expand Down
11 changes: 11 additions & 0 deletions src/ManagementResources/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ function limitByType($contentType)
return $this;
}

/**
* Target locale
* @param $locale
* @return $this
*/
function limitByLocale($locale)
{
$this->requestDecorator->addParameter('locale', '=', $locale);
return $this;
}

/**
* Target content type.
* @param $contentType
Expand Down
1 change: 0 additions & 1 deletion src/ManagementResources/ResourceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


abstract class ResourceBase extends \Incraigulous\ContentfulSDK\Resources\ResourceBase {
protected $clientClassName = 'Incraigulous\ContentfulSDK\ManagementClient';

/**
* Make a post request.
Expand Down
1 change: 0 additions & 1 deletion src/ManagementResources/Spaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class Spaces extends ResourceBase {
* Init and store a new client and decorator.
*/
function refresh() {
$this->client = new $this->clientClassName($this->accessToken, null, $this->cacher);
$this->requestDecorator = new RequestDecorator();
$this->requestDecorator->setResource($this->resourceName);
}
Expand Down
11 changes: 6 additions & 5 deletions src/ManagementSDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
*/

class ManagementSDK extends SDKBase {
protected $clientClassName = 'Incraigulous\ContentfulSDK\ManagementClient';

/**
* Use the assets resource.
* @return \Incraigulous\ContentfulSDK\ManagementResources\Assets
*/
function assets()
{
return new Assets($this->accessToken, $this->spaceId, $this->cacher);
return new Assets($this->client);
}

/**
Expand All @@ -38,7 +39,7 @@ function assets()
*/
function contentTypes()
{
return new ContentTypes($this->accessToken, $this->spaceId, $this->cacher);
return new ContentTypes($this->client);
}

/**
Expand All @@ -47,7 +48,7 @@ function contentTypes()
*/
function entries()
{
return new Entries($this->accessToken, $this->spaceId, $this->cacher);
return new Entries($this->client);
}

/**
Expand All @@ -56,7 +57,7 @@ function entries()
*/
function spaces()
{
return new Spaces($this->accessToken, $this->spaceId, $this->cacher);
return new Spaces($this->client);
}

/**
Expand All @@ -65,6 +66,6 @@ function spaces()
*/
function webhooks()
{
return new Webhooks($this->accessToken, $this->spaceId, $this->cacher);
return new Webhooks($this->client);
}
}
9 changes: 9 additions & 0 deletions src/PreviewClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Incraigulous\ContentfulSDK;

use GuzzleHttp;

class PreviewClient extends DeliveryClient {
protected $endpointBase = 'https://preview.contentful.com/spaces';

}
15 changes: 15 additions & 0 deletions src/PreviewSDK.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Incraigulous\ContentfulSDK;

/**
* Factory for returning resources.
*
* Class PreviewSDK
* @package Incraigulous\ContentfulSDK
*/

class PreviewSDK extends DeliverySDK {
protected $clientClassName = 'Incraigulous\ContentfulSDK\PreviewClient';

}
11 changes: 11 additions & 0 deletions src/Resources/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ function limitByType($contentType)
return $this;
}

/**
* Target locale
* @param $locale
* @return $this
*/
function limitByLocale($locale)
{
$this->requestDecorator->addParameter('locale', '=', $locale);
return $this;
}

/**
* Target content type.
* @param $contentType
Expand Down
13 changes: 3 additions & 10 deletions src/Resources/ResourceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,24 @@

namespace Incraigulous\ContentfulSDK\Resources;

use Incraigulous\ContentfulSDK\ClientBase;
use Incraigulous\ContentfulSDK\RequestDecorator;
use Incraigulous\ContentfulSDK\CacherInterface;

abstract class ResourceBase {
protected $clientClassName = 'Incraigulous\ContentfulSDK\DeliveryClient';
protected $resourceName;
protected $spaceId;
protected $accessToken;
protected $client;
protected $requestDecorator;
protected $cacher;

function __construct($accessToken, $spaceId = null, CacherInterface $cacher = null)
function __construct(ClientBase $client)
{
$this->spaceId = $spaceId;
$this->accessToken = $accessToken;
$this->cacher = $cacher;
$this->client = $client;
$this->refresh();
}

/**
* Init and store a new client and decorator.
*/
function refresh() {
$this->client = new $this->clientClassName($this->accessToken, $this->spaceId, $this->cacher);
$this->requestDecorator = new RequestDecorator();
$this->requestDecorator->setResource($this->resourceName);
}
Expand Down
6 changes: 5 additions & 1 deletion src/SDKBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
namespace Incraigulous\ContentfulSDK;

abstract class SDKBase {
protected $clientClassName = 'Incraigulous\ContentfulSDK\DeliveryClient';

protected $accessToken;
protected $spaceId;
protected $cacher;
protected $client;

function __construct($accessToken, $spaceId = null, CacherInterface $cacher = null)
function __construct($accessToken, $spaceId = null, CacherInterface $cacher = null, $assoc = true)
{
$this->accessToken = $accessToken;
$this->spaceId = $spaceId;
$this->cacher = $cacher;
$this->client = new $this->clientClassName($this->accessToken, $this->spaceId, $this->cacher, $assoc);
}
}
27 changes: 15 additions & 12 deletions tests/ClientTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Incraigulous\ContentfulSDK\DeliveryClient;
use Incraigulous\ContentfulSDK\ManagementClient;
use Incraigulous\ContentfulSDK\ManagementResources\Assets as ManagementAssets;
use Incraigulous\ContentfulSDK\PreviewClient;
use Incraigulous\ContentfulSDK\Resources\Assets;
use Incraigulous\ContentfulSDK\ManagementResources\Entries as ManagementEntries;
use Incraigulous\ContentfulSDK\Resources\Entries;
Expand All @@ -22,15 +23,17 @@
class ClientTests extends PHPUnit_Framework_TestCase {
protected function getAssets() {
return [
new Assets('asdfasdfklj', 'asdfasdf'),
new ManagementAssets('asdfasdfklj', 'asdfasdf')
new Assets(new DeliveryClient('asdfasdfklj', 'asdfasdf')),
new Assets(new PreviewClient('asdfasdfklj', 'asdfasdf')),
new ManagementAssets(new ManagementClient('asdfasdfklj', 'asdfasdf'))
];
}

protected function getEntries() {
return [
new Entries('asdfasdfklj', 'asdfasdf'),
new ManagementEntries('asdfasdfklj', 'asdfasdf')
new Entries(new DeliveryClient('asdfasdfklj', 'asdfasdf')),
new Entries(new PreviewClient('asdfasdfklj', 'asdfasdf')),
new ManagementEntries(new ManagementClient('asdfasdfklj', 'asdfasdf'))
];
}

Expand All @@ -56,49 +59,49 @@ public function testGet() {
}

public function testPost() {
$resource = $this->attachSuccessful(new ManagementEntries('asdfasdfklj', 'asdfasdf'));
$resource = $this->attachSuccessful(new ManagementEntries(new ManagementClient('asdfasdfklj', 'asdfasdf')));
$response = $resource->post(array('fields' => array('cat' => 'dog')));
$this->assertEquals('200', $response->getStatusCode());
}

public function testPut() {
$resource = $this->attachSuccessful(new ManagementEntries('asdfasdfklj', 'asdfasdf'));
$resource = $this->attachSuccessful(new ManagementEntries(new ManagementClient('asdfasdfklj', 'asdfasdf')));
$response = $resource->put('asdfasdf', array('fields' => array('cat' => 'dog')));
$this->assertEquals('200', $response->getStatusCode());
}

public function testDelete() {
$resource = $this->attachSuccessful(new ManagementEntries('asdfasdfklj', 'asdfasdf'));
$resource = $this->attachSuccessful(new ManagementEntries(new ManagementClient('asdfasdfklj', 'asdfasdf')));
$response = $resource->delete('asdfasdf');
$this->assertEquals('200', $response->getStatusCode());
}

public function testProcess() {
$resource = $this->attachSuccessful(new ManagementAssets('asdfasdfklj', 'asdfasdf'));
$resource = $this->attachSuccessful(new ManagementAssets(new ManagementClient('asdfasdfklj', 'asdfasdf')));
$response = $resource->process('asdfasdf');
$this->assertEquals('200', $response->getStatusCode());
}

public function testArchive() {
$resource = $this->attachSuccessful(new ManagementAssets('asdfasdfklj', 'asdfasdf'));
$resource = $this->attachSuccessful(new ManagementAssets(new ManagementClient('asdfasdfklj', 'asdfasdf')));
$response = $resource->archive('asdfasdf');
$this->assertEquals('200', $response->getStatusCode());
}

public function testUnarchive() {
$resource = $this->attachSuccessful(new ManagementAssets('asdfasdfklj', 'asdfasdf'));
$resource = $this->attachSuccessful(new ManagementAssets(new ManagementClient('asdfasdfklj', 'asdfasdf')));
$response = $resource->unarchive('asdfasdf');
$this->assertEquals('200', $response->getStatusCode());
}

public function testPublish() {
$resource = $this->attachSuccessful(new ManagementAssets('asdfasdfklj', 'asdfasdf'));
$resource = $this->attachSuccessful(new ManagementAssets(new ManagementClient('asdfasdfklj', 'asdfasdf')));
$response = $resource->publish('asdfasdf', 234);
$this->assertEquals('200', $response->getStatusCode());
}

public function testUnpublish() {
$resource = $this->attachSuccessful(new ManagementAssets('asdfasdfklj', 'asdfasdf'));
$resource = $this->attachSuccessful(new ManagementAssets(new ManagementClient('asdfasdfklj', 'asdfasdf')));
$response = $resource->unpublish('asdfasdf', 123);
$this->assertEquals('200', $response->getStatusCode());
}
Expand Down
13 changes: 9 additions & 4 deletions tests/ResourceTests.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
namespace Incraigulous\ContentfulSDK\Tests;

use Incraigulous\ContentfulSDK\DeliveryClient;
use Incraigulous\ContentfulSDK\ManagementClient;
use Incraigulous\ContentfulSDK\ManagementResources\Assets as ManagementAssets;
use Incraigulous\ContentfulSDK\PreviewClient;
use Incraigulous\ContentfulSDK\Resources\Assets;
use Incraigulous\ContentfulSDK\ManagementResources\Entries as ManagementEntries;
use Incraigulous\ContentfulSDK\Resources\Entries;
Expand All @@ -11,15 +14,17 @@ class ResourceTests extends PHPUnit_Framework_TestCase
{
protected function getAssets() {
return [
new Assets('asdfasdfklj', 'asdfasdf'),
new ManagementAssets('asdfasdfklj', 'asdfasdf')
new Assets(new DeliveryClient('asdfasdfklj', 'asdfasdf')),
new Assets(new PreviewClient('asdfasdfklj', 'asdfasdf')),
new ManagementAssets(new ManagementClient('asdfasdfklj', 'asdfasdf'))
];
}

protected function getEntries() {
return [
new Entries('asdfasdfklj', 'asdfasdf'),
new ManagementEntries('asdfasdfklj', 'asdfasdf')
new Entries(new DeliveryClient('asdfasdfklj', 'asdfasdf')),
new Entries(new PreviewClient('asdfasdfklj', 'asdfasdf')),
new ManagementEntries(new ManagementClient('asdfasdfklj', 'asdfasdf'))
];
}

Expand Down