Skip to content

Commit 0e9d4fe

Browse files
committed
Storable resources
1 parent a396fce commit 0e9d4fe

5 files changed

Lines changed: 85 additions & 21 deletions

File tree

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.

src/AsyncClient.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use ApiClients\Foundation\ClientInterface;
77
use ApiClients\Foundation\Factory;
88
use ApiClients\Foundation\Options;
9+
use ApiClients\Foundation\Resource\ResourceInterface;
910
use React\EventLoop\LoopInterface;
11+
use React\Promise\CancellablePromiseInterface;
1012
use React\Promise\PromiseInterface;
1113
use Rx\Observable;
1214
use Rx\Scheduler;
@@ -71,6 +73,16 @@ public static function createFromClient(ClientInterface $client, RateLimitState
7173
return new self($client, $rateLimitState);
7274
}
7375

76+
public function hydrate(string $resource): CancellablePromiseInterface
77+
{
78+
return $this->client->hydrate($resource);
79+
}
80+
81+
public function extract(ResourceInterface $resource): CancellablePromiseInterface
82+
{
83+
return $this->client->extract($resource);
84+
}
85+
7486
public function meta(): PromiseInterface
7587
{
7688
return $this->client->handle(new Command\MetaCommand());

src/AsyncClientInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,29 @@
22

33
namespace ApiClients\Client\Github;
44

5+
use ApiClients\Foundation\Resource\ResourceInterface;
6+
use React\Promise\CancellablePromiseInterface;
57
use React\Promise\PromiseInterface;
68
use Rx\Observable;
79

810
interface AsyncClientInterface
911
{
12+
/**
13+
* Take a string create by the extract method and hydrate it back to a resource.
14+
*
15+
* @param string $resource
16+
* @return CancellablePromiseInterface
17+
*/
18+
public function hydrate(string $resource): CancellablePromiseInterface;
19+
20+
/**
21+
* Extract a resource into a string for storage.
22+
*
23+
* @param ResourceInterface $resource
24+
* @return CancellablePromiseInterface
25+
*/
26+
public function extract(ResourceInterface $resource): CancellablePromiseInterface;
27+
1028
public function meta(): PromiseInterface;
1129

1230
public function user(string $user): PromiseInterface;

src/Client.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ApiClients\Client\Github\Resource\UserInterface;
66
use ApiClients\Foundation\Factory as FoundationClientFactory;
77
use ApiClients\Foundation\Options;
8+
use ApiClients\Foundation\Resource\ResourceInterface;
89
use React\EventLoop\Factory;
910
use React\EventLoop\LoopInterface;
1011
use Rx\Scheduler;
@@ -20,7 +21,7 @@ final class Client implements ClientInterface
2021
/**
2122
* @var AsyncClient
2223
*/
23-
private $client;
24+
private $asyncClient;
2425

2526
/**
2627
* @param LoopInterface $loop
@@ -29,7 +30,7 @@ final class Client implements ClientInterface
2930
private function __construct(LoopInterface $loop, AsyncClient $client)
3031
{
3132
$this->loop = $loop;
32-
$this->client = $client;
33+
$this->asyncClient = $client;
3334
}
3435

3536
/**
@@ -59,14 +60,30 @@ public static function create(
5960
return new self($loop, $asyncClient);
6061
}
6162

63+
public function hydrate(string $resource): ResourceInterface
64+
{
65+
return await(
66+
$this->asyncClient->hydrate($resource),
67+
$this->loop
68+
);
69+
}
70+
71+
public function extract(ResourceInterface $resource): string
72+
{
73+
return await(
74+
$this->asyncClient->extract($resource),
75+
$this->loop
76+
);
77+
}
78+
6279
/**
6380
* @param string $user
6481
* @return UserInterface
6582
*/
6683
public function user(string $user): UserInterface
6784
{
6885
return await(
69-
$this->client->user($user),
86+
$this->asyncClient->user($user),
7087
$this->loop
7188
);
7289
}
@@ -76,6 +93,6 @@ public function user(string $user): UserInterface
7693
*/
7794
public function getRateLimitState(): RateLimitState
7895
{
79-
return $this->client->getRateLimitState();
96+
return $this->asyncClient->getRateLimitState();
8097
}
8198
}

src/ClientInterface.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,26 @@
33
namespace ApiClients\Client\Github;
44

55
use ApiClients\Client\Github\Resource\UserInterface;
6+
use ApiClients\Foundation\Resource\ResourceInterface;
67

78
interface ClientInterface
89
{
10+
/**
11+
* Take a string create by the extract method and hydrate it back to a resource.
12+
*
13+
* @param string $resource
14+
* @return ResourceInterface
15+
*/
16+
public function hydrate(string $resource): ResourceInterface;
17+
18+
/**
19+
* Extract a resource into a string for storage.
20+
*
21+
* @param ResourceInterface $resource
22+
* @return string
23+
*/
24+
public function extract(ResourceInterface $resource): string;
25+
926
public function user(string $user): UserInterface;
1027

1128
public function getRateLimitState(): RateLimitState;

0 commit comments

Comments
 (0)