Skip to content

Commit 53f3e3b

Browse files
committed
Proof of Concept for storable resources
1 parent 868e99c commit 53f3e3b

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types=1);
2+
use ApiClients\Client\Travis\AsyncClient;
3+
use ApiClients\Client\Travis\Resource\RepositoryInterface;
4+
use React\EventLoop\Factory;
5+
use function ApiClients\Foundation\resource_pretty_print;
6+
7+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
8+
9+
$loop = Factory::create();
10+
$client = AsyncClient::create($loop);
11+
12+
$repos = [
13+
'WyriHaximus/php-travis-client',
14+
];
15+
16+
if (count($argv) > 1) {
17+
unset($argv[0]);
18+
foreach ($argv as $repo) {
19+
$repos[] = $repo;
20+
}
21+
}
22+
23+
foreach ($repos as $repo) {
24+
$client->repository($repo)->then(function (RepositoryInterface $repo) use ($client) {
25+
resource_pretty_print($repo);
26+
return $client->extract($repo);
27+
})->then(function (array $json) use ($client) {
28+
var_export($json);
29+
return $client->hydrate($json);
30+
})->then(function (RepositoryInterface $repo) {
31+
resource_pretty_print($repo);
32+
return $repo->refresh();
33+
})->done(function (RepositoryInterface $repo) {
34+
resource_pretty_print($repo);
35+
}, function ($e) {
36+
echo (string)$e;
37+
});
38+
}
39+
40+
$loop->run();

src/AsyncClient.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
use ApiClients\Client\Travis\Resource\HookInterface;
77
use ApiClients\Foundation\ClientInterface;
88
use ApiClients\Foundation\Factory;
9+
use ApiClients\Foundation\Hydrator\CommandBus\Command\ExtractFQCNCommand;
10+
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateFQCNCommand;
11+
use ApiClients\Foundation\Resource\ResourceInterface;
912
use React\EventLoop\LoopInterface;
1013
use React\Promise\CancellablePromiseInterface;
1114
use React\Promise\PromiseInterface;
1215
use Rx\ObservableInterface;
1316
use Rx\React\Promise;
1417
use Rx\Scheduler;
1518
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
19+
use function React\Promise\resolve;
1620

1721
final class AsyncClient implements AsyncClientInterface
1822
{
@@ -55,6 +59,26 @@ public static function create(
5559
return new self($client);
5660
}
5761

62+
public function hydrate(array $resource)
63+
{
64+
$class = $resource['class'];
65+
$json = $resource['json'];
66+
return $this->client->handle(new HydrateFQCNCommand($class, $json));
67+
}
68+
69+
public function extract(ResourceInterface $resource)
70+
{
71+
$class = get_class($resource);
72+
return $this->client->handle(
73+
new ExtractFQCNCommand($class, $resource)
74+
)->then(function ($json) use ($class) {
75+
return resolve([
76+
'class' => $class,
77+
'json' => $json,
78+
]);
79+
});
80+
}
81+
5882
/**
5983
* Create an AsyncClient from a ApiClients\Foundation\ClientInterface.
6084
* Be sure to pass in a client with the options from ApiSettings and the Async namespace suffix.

0 commit comments

Comments
 (0)