Skip to content

Commit 590a0f8

Browse files
committed
Updated to extracting and hydrating functionality from foundation
1 parent bdb4a83 commit 590a0f8

File tree

7 files changed

+86
-36
lines changed

7 files changed

+86
-36
lines changed

composer.lock

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/repository-extra-hydrate-refresh-async.php renamed to examples/repository-extract-hydrate-refresh-async.php

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

src/AsyncClient.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
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;
119
use ApiClients\Foundation\Resource\ResourceInterface;
1210
use React\EventLoop\LoopInterface;
1311
use React\Promise\CancellablePromiseInterface;
@@ -16,7 +14,6 @@
1614
use Rx\React\Promise;
1715
use Rx\Scheduler;
1816
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
19-
use function React\Promise\resolve;
2017

2118
final class AsyncClient implements AsyncClientInterface
2219
{
@@ -59,24 +56,14 @@ public static function create(
5956
return new self($client);
6057
}
6158

62-
public function hydrate(array $resource)
59+
public function hydrate(string $resource): CancellablePromiseInterface
6360
{
64-
$class = $resource['class'];
65-
$json = $resource['json'];
66-
return $this->client->handle(new HydrateFQCNCommand($class, $json));
61+
return $this->client->hydrate($resource);
6762
}
6863

69-
public function extract(ResourceInterface $resource)
64+
public function extract(ResourceInterface $resource): CancellablePromiseInterface
7065
{
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-
});
66+
return $this->client->extract($resource);
8067
}
8168

8269
/**

src/AsyncClientInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ApiClients\Client\Travis;
44

5+
use ApiClients\Foundation\Resource\ResourceInterface;
56
use React\Promise\CancellablePromiseInterface;
67
use React\Promise\PromiseInterface;
78
use Rx\Observable;
@@ -10,6 +11,10 @@
1011

1112
interface AsyncClientInterface
1213
{
14+
public function hydrate(string $resource): CancellablePromiseInterface;
15+
16+
public function extract(ResourceInterface $resource): CancellablePromiseInterface;
17+
1318
/**
1419
* Fetch the given repository.
1520
* This will resolve a Resource\Async\Repository object.

src/Client.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ApiClients\Client\Travis\Resource\SSHKeyInterface;
88
use ApiClients\Client\Travis\Resource\UserInterface;
99
use ApiClients\Foundation\Factory;
10+
use ApiClients\Foundation\Resource\ResourceInterface;
1011
use React\EventLoop\Factory as LoopFactory;
1112
use React\EventLoop\LoopInterface;
1213
use Rx\React\Promise;
@@ -67,6 +68,22 @@ public static function createFromClient(LoopInterface $loop, AsyncClientInterfac
6768
return new self($loop, $asyncClient);
6869
}
6970

71+
public function hydrate(string $resource): ResourceInterface
72+
{
73+
return await(
74+
$this->asyncClient->hydrate($resource),
75+
$this->loop
76+
);
77+
}
78+
79+
public function extract(ResourceInterface $resource): string
80+
{
81+
return await(
82+
$this->asyncClient->extract($resource),
83+
$this->loop
84+
);
85+
}
86+
7087
/**
7188
* {@inheritdoc}
7289
*/

src/ClientInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
use ApiClients\Client\Travis\Resource\RepositoryInterface;
77
use ApiClients\Client\Travis\Resource\SSHKeyInterface;
88
use ApiClients\Client\Travis\Resource\UserInterface;
9+
use ApiClients\Foundation\Resource\ResourceInterface;
910

1011
interface ClientInterface
1112
{
13+
public function hydrate(string $resource): ResourceInterface;
14+
15+
public function extract(ResourceInterface $resource): string;
16+
1217
/**
1318
* Fetch the given repository.
1419
* This will return a Resource\Async\Repository object.

0 commit comments

Comments
 (0)