Skip to content

Commit 1b18fc0

Browse files
authored
Merge pull request #13 from php-api-clients/storable-resources
Storable resources
2 parents a3f15b6 + 5eecfd1 commit 1b18fc0

File tree

7 files changed

+154
-17
lines changed

7 files changed

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

src/AsyncClient.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ApiClients\Client\Travis\Resource\HookInterface;
77
use ApiClients\Foundation\ClientInterface;
88
use ApiClients\Foundation\Factory;
9+
use ApiClients\Foundation\Resource\ResourceInterface;
910
use React\EventLoop\LoopInterface;
1011
use React\Promise\CancellablePromiseInterface;
1112
use React\Promise\PromiseInterface;
@@ -55,6 +56,16 @@ public static function create(
5556
return new self($client);
5657
}
5758

59+
public function hydrate(string $resource): CancellablePromiseInterface
60+
{
61+
return $this->client->hydrate($resource);
62+
}
63+
64+
public function extract(ResourceInterface $resource): CancellablePromiseInterface
65+
{
66+
return $this->client->extract($resource);
67+
}
68+
5869
/**
5970
* Create an AsyncClient from a ApiClients\Foundation\ClientInterface.
6071
* Be sure to pass in a client with the options from ApiSettings and the Async namespace suffix.

src/AsyncClientInterface.php

Lines changed: 17 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,22 @@
1011

1112
interface AsyncClientInterface
1213
{
14+
/**
15+
* Take a string create by the extract method and hydrate it back to a resource.
16+
*
17+
* @param string $resource
18+
* @return CancellablePromiseInterface
19+
*/
20+
public function hydrate(string $resource): CancellablePromiseInterface;
21+
22+
/**
23+
* Extract a resource into a string for storage.
24+
*
25+
* @param ResourceInterface $resource
26+
* @return CancellablePromiseInterface
27+
*/
28+
public function extract(ResourceInterface $resource): CancellablePromiseInterface;
29+
1330
/**
1431
* Fetch the given repository.
1532
* 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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,26 @@
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+
/**
14+
* Take a string create by the extract method and hydrate it back to a resource.
15+
*
16+
* @param string $resource
17+
* @return ResourceInterface
18+
*/
19+
public function hydrate(string $resource): ResourceInterface;
20+
21+
/**
22+
* Extract a resource into a string for storage.
23+
*
24+
* @param ResourceInterface $resource
25+
* @return string
26+
*/
27+
public function extract(ResourceInterface $resource): string;
28+
1229
/**
1330
* Fetch the given repository.
1431
* This will return a Resource\Async\Repository object.

0 commit comments

Comments
 (0)