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

Add more info on session resource & getOne domain method #63

Merged
merged 7 commits into from
Oct 16, 2018
Merged
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
13 changes: 6 additions & 7 deletions src/Api/Catalog/InventoryDomain.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<?php
namespace ShoppingFeed\Sdk\Api\Catalog;

use ShoppingFeed\Sdk\Api\Catalog as ApiCatalog;
use ShoppingFeed\Sdk\Catalog;
use ShoppingFeed\Sdk\Resource\AbstractDomainResource;

/**
* @method ApiCatalog\InventoryResource[] getIterator()
* @method ApiCatalog\InventoryResource[] getAll($page = 1, $limit = 100)
* @method InventoryResource[] getIterator()
* @method InventoryResource[] getAll($page = 1, $limit = 100)
* @method InventoryResource getOne($identity)
*/
class InventoryDomain extends AbstractDomainResource
{
/**
* @var string
*/
protected $resourceClass = ApiCatalog\InventoryResource::class;
protected $resourceClass = InventoryResource::class;

/**
* @param string $reference the resource reference
Expand All @@ -24,8 +23,8 @@ class InventoryDomain extends AbstractDomainResource
public function getByReference($reference)
{
$resource = $this->link->get([], ['query' => ['reference' => $reference]]);
if ($resource->getProperty('count') > 0) {
return new ApiCatalog\InventoryResource(
if ($resource && $resource->getProperty('count') > 0) {
return new InventoryResource(
$resource->getFirstResource('inventory'),
false
);
Expand Down
12 changes: 6 additions & 6 deletions src/Api/Order/OrderDomain.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php
namespace ShoppingFeed\Sdk\Api\Order;

use ShoppingFeed\Sdk\Api\Order as ApiOrder;
use ShoppingFeed\Sdk\Resource\AbstractDomainResource;

/**
* @method ApiOrder\OrderResource[] getIterator()
* @method ApiOrder\OrderResource[] getAll($criteria = [])
* @method ApiOrder\OrderResource[] getPage(array $criteria = [])
* @method ApiOrder\OrderResource[] getPages(array $criteria = [])
* @method OrderResource[] getIterator()
* @method OrderResource[] getAll($criteria = [])
* @method OrderResource[] getPage(array $criteria = [])
* @method OrderResource[] getPages(array $criteria = [])
* @method OrderResource getOne($identity)
*/
class OrderDomain extends AbstractDomainResource
{
/**
* @var string
*/
protected $resourceClass = ApiOrder\OrderResource::class;
protected $resourceClass = OrderResource::class;

/**
* @param OrderOperation $operation
Expand Down
43 changes: 39 additions & 4 deletions src/Api/Session/SessionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,52 @@

class SessionResource extends AbstractResource
{
/**
* @return int|null NULL when account id is not found, id integer otherwise
*/
public function getId()
{
if ($account = $this->resource->getFirstResource('account')) {
return $account->getProperty('id');
}

return null;
}

/**
* @return array A list of named roles
*/
public function getRoles()
{
return $this->getProperty('roles') ?: [];
}

/**
* @return string
*/
public function getLogin()
{
return $this->resource->getProperty('login');
return (string) $this->resource->getProperty('login');
}

/**
* @return string
*/
public function getEmail()
{
return $this->resource->getProperty('email');
return (string) $this->resource->getProperty('email');
}

/**
* @return string
*/
public function getToken()
{
return $this->resource->getProperty('token');
return (string) $this->resource->getProperty('token');
}

/**
* @return Store\StoreCollection
* @return Store\StoreCollection|Store\StoreResource[]
*/
public function getStores()
{
Expand All @@ -40,6 +60,21 @@ public function getStores()
);
}

/**
* Return the language tag, as following:
*
* - en_US
* - fr_FR
*
* ...etc
*
* @return string
*/
public function getLanguageTag()
{
return (string) $this->resource->getProperty('language');
}

/**
* @param int|string $idOrName
*
Expand Down
1 change: 1 addition & 0 deletions src/Api/Store/StoreChannelDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @method StoreChannelResource[] getAll($criteria = [])
* @method StoreChannelResource[] getPage(array $criteria = [])
* @method StoreChannelResource[] getPages(array $criteria = [])
* @method StoreChannelResource getOne($identity)
*/
class StoreChannelDomain extends Resource\AbstractDomainResource
{
Expand Down
4 changes: 3 additions & 1 deletion src/Api/Store/StoreChannelResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public function isInstalled()
public function getChannel()
{
if (null === $this->channel) {
$this->channel = new Channel\ChannelResource($this->resource->getFirstResource('channel'));
$this->channel = new Channel\ChannelResource(
$this->resource->getFirstResource('channel')
);
}

return $this->channel;
Expand Down
12 changes: 12 additions & 0 deletions src/Api/Store/StoreResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ public function isActive()
return $this->getProperty('status') === 'active';
}

/**
* @return string One of the following status:
* - active
* - demo
* - deleted
* - suspended
*/
public function getStatus()
{
return $this->getProperty('status');
}

/**
* @return string
*/
Expand Down
15 changes: 13 additions & 2 deletions src/Hal/HalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,23 @@ public function getTitle()
return $this->title;
}

/**
* @param $path
*
* @return HalLink
*/
public function withAddedHref($path)
{
$instance = clone $this;
$instance->href = rtrim($instance->href, '/') . '/' . ltrim($path, '/');

return $instance;
}

/**
* @param array $variables
*
* @return null|string|string[]
*
* @throws \QL\UriTemplate\Exception
*/
public function getUri(array $variables)
{
Expand Down
15 changes: 15 additions & 0 deletions src/Resource/AbstractDomainResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ public function __construct(Hal\HalLink $link)
$this->link = $link;
}

/**
* Get the resource by it's identity
*
* @param mixed $identity a scalar value that identity the resource
*
* @return AbstractResource
*/
public function getOne($identity)
{
$link = $this->link->withAddedHref($identity);
$class = $this->resourceClass;

return new $class($link->get());
}

/**
* @param array $criteria
*
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Api/AbstractResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function initHalResourceProperties(array $props = [])

$resource = $this->initHalResource();
$resource
->expects($this->exactly(count($props)))
->expects($this->any())
->method('getProperty')
->with($this->logicalOr(...array_keys($props)))
->will($this->returnCallback(function($prop) use($props) {
Expand Down
36 changes: 32 additions & 4 deletions tests/unit/Api/Session/SessionResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ class SessionResourceTest extends Sdk\Test\Api\AbstractResourceTest

public function setUp()
{
$this->props = [
'login' => 'username',
'email' => 'user@mail.com',
'token' => 'fd9cf7c178a1efd30bb1aad0e302abde',
$this->props = [
'login' => 'username',
'email' => 'user@mail.com',
'token' => 'fd9cf7c178a1efd30bb1aad0e302abde',
'language' => 'en_US',
'roles' => ['user'],
];

$this->resources = [
$this->createMock(Sdk\Hal\HalResource::class),
$this->createMock(Sdk\Hal\HalResource::class),
Expand All @@ -33,6 +36,31 @@ public function testPropertiesGetters()
$this->assertEquals($this->props['email'], $instance->getEmail());
$this->assertEquals($this->props['login'], $instance->getLogin());
$this->assertEquals($this->props['token'], $instance->getToken());
$this->assertEquals($this->props['roles'], $instance->getRoles());
$this->assertEquals($this->props['language'], $instance->getLanguageTag());
}

public function testGetAccountIdWithMatchedOrNullAccount()
{
$halResource = $this->createMock(Sdk\Hal\HalResource::class);
$halResource
->expects($this->exactly(2))
->method('getFirstResource')
->with('account')
->willReturnOnConsecutiveCalls(
$halResource,
null
);

$halResource
->expects($this->once())
->method('getProperty')
->with('id')
->willReturn(12);

$instance = new Sdk\Api\Session\SessionResource($halResource);
$this->assertSame(12, $instance->getId(), 'First call resource will return mock');
$this->assertNull($instance->getId(), 'Second resource call return NULL');
}

public function testGetStores()
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/Api/Store/StoreResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public function testPropertiesGetters()

$instance = new Sdk\Api\Store\StoreResource($this->halResource);

$this->assertEquals($this->props['id'], $instance->getId());
$this->assertEquals($this->props['name'], $instance->getName());
$this->assertEquals($this->props['country'], $instance->getCountryCode());
$this->assertSame($this->props['id'], $instance->getId());
$this->assertSame($this->props['name'], $instance->getName());
$this->assertSame($this->props['country'], $instance->getCountryCode());
$this->assertSame('active', $instance->getStatus());
$this->assertTrue($instance->isActive());
}

Expand Down
Loading