-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClient.php
More file actions
68 lines (57 loc) · 1.88 KB
/
Client.php
File metadata and controls
68 lines (57 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php declare(strict_types=1);
namespace ApiClients\Foundation;
use ApiClients\Foundation\Hydrator\CommandBus\Command\ExtractFQCNCommand;
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateFQCNCommand;
use ApiClients\Foundation\Resource\ResourceInterface;
use ApiClients\Tools\CommandBus\CommandBusInterface;
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use React\Promise\CancellablePromiseInterface;
use function React\Promise\resolve;
final class Client implements ClientInterface
{
/**
* @var CommandBusInterface
*/
private $commandBus;
/**
* @param CommandBusInterface $commandBus
*/
public function __construct(CommandBusInterface $commandBus)
{
$this->commandBus = $commandBus;
}
/**
* @param $command
* @return CancellablePromiseInterface
*/
public function handle($command): CancellablePromiseInterface
{
return $this->commandBus->handle($command);
}
public function hydrate(string $resource): CancellablePromiseInterface
{
$resource = json_decode($resource, true);
if (!isset($resource['class'], $resource['properties'])) {
throw new InvalidArgumentException();
}
if (!class_exists($resource['class'])) {
throw new InvalidArgumentException();
}
$class = $resource['class'];
$json = $resource['properties'];
return $this->handle(new HydrateFQCNCommand($class, $json));
}
public function extract(ResourceInterface $resource): CancellablePromiseInterface
{
$class = get_class($resource);
return $this->handle(
new ExtractFQCNCommand($class, $resource)
)->then(function ($json) use ($class) {
return resolve(json_encode([
'class' => $class,
'properties' => $json,
]));
});
}
}