Skip to content

Commit cec7db0

Browse files
authored
Merge pull request #17 from php-api-clients/storable-resources
Storable resources
2 parents f04f4a0 + 65babe2 commit cec7db0

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/Client.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
namespace ApiClients\Foundation;
44

5+
use ApiClients\Foundation\Hydrator\CommandBus\Command\ExtractFQCNCommand;
6+
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateFQCNCommand;
7+
use ApiClients\Foundation\Resource\ResourceInterface;
58
use ApiClients\Tools\CommandBus\CommandBusInterface;
9+
use InvalidArgumentException;
610
use Psr\Container\ContainerInterface;
711
use React\Promise\CancellablePromiseInterface;
12+
use function React\Promise\resolve;
813

914
final class Client implements ClientInterface
1015
{
@@ -51,4 +56,33 @@ public function handle($command): CancellablePromiseInterface
5156
{
5257
return $this->commandBus->handle($command);
5358
}
59+
60+
public function hydrate(string $resource): CancellablePromiseInterface
61+
{
62+
$resource = json_decode($resource, true);
63+
if (!isset($resource['class'], $resource['properties'])) {
64+
throw new InvalidArgumentException();
65+
}
66+
67+
if (!class_exists($resource['class'])) {
68+
throw new InvalidArgumentException();
69+
}
70+
71+
$class = $resource['class'];
72+
$json = $resource['properties'];
73+
return $this->handle(new HydrateFQCNCommand($class, $json));
74+
}
75+
76+
public function extract(ResourceInterface $resource): CancellablePromiseInterface
77+
{
78+
$class = get_class($resource);
79+
return $this->handle(
80+
new ExtractFQCNCommand($class, $resource)
81+
)->then(function ($json) use ($class) {
82+
return resolve(json_encode([
83+
'class' => $class,
84+
'properties' => $json,
85+
]));
86+
});
87+
}
5488
}

tests/ClientTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace ApiClients\Tests\Foundation;
44

55
use ApiClients\Foundation\Client;
6+
use ApiClients\Foundation\Hydrator\CommandBus\Command\ExtractFQCNCommand;
7+
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateFQCNCommand;
8+
use ApiClients\Foundation\Resource\ResourceInterface;
69
use ApiClients\Tools\CommandBus\CommandBus;
710
use ApiClients\Tools\CommandBus\CommandBusInterface;
811
use ApiClients\Tools\TestUtilities\TestCase;
@@ -57,4 +60,44 @@ public function testCommandBusMissing()
5760
{
5861
new Client(ContainerBuilder::buildDevContainer());
5962
}
63+
64+
public function testHydrate()
65+
{
66+
$resource = $this->prophesize(ResourceInterface::class)->reveal();
67+
68+
$commandBus = $this->prophesize(CommandBusInterface::class);
69+
$commandBus->handle(new HydrateFQCNCommand('stdClass', []))->shouldBeCalled()->willReturn(resolve($resource));
70+
71+
$container = ContainerBuilder::buildDevContainer();
72+
$container->set(CommandBusInterface::class, $commandBus->reveal());
73+
$client = new Client($container);
74+
75+
$json = json_encode([
76+
'class' => 'stdClass',
77+
'properties' => [],
78+
]);
79+
80+
self::assertSame($resource, await($client->hydrate($json), Factory::create()));
81+
}
82+
83+
public function testExtract()
84+
{
85+
$resource = $this->prophesize(ResourceInterface::class)->reveal();
86+
87+
$json = json_encode([
88+
'class' => get_class($resource),
89+
'properties' => [],
90+
]);
91+
92+
$commandBus = $this->prophesize(CommandBusInterface::class);
93+
$commandBus->handle(
94+
new ExtractFQCNCommand(get_class($resource), $resource)
95+
)->shouldBeCalled()->willReturn(resolve([]));
96+
97+
$container = ContainerBuilder::buildDevContainer();
98+
$container->set(CommandBusInterface::class, $commandBus->reveal());
99+
$client = new Client($container);
100+
101+
self::assertSame($json, await($client->extract($resource), Factory::create()));
102+
}
60103
}

0 commit comments

Comments
 (0)