Skip to content

Commit 21005f6

Browse files
authored
Merge pull request #14 from davidwdan/cleanup
Change ObservableInterface return types to Observable
2 parents 3838ede + 4906fec commit 21005f6

7 files changed

+73
-65
lines changed

Diff for: examples/accounts-async.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
<?php declare(strict_types=1);
2+
23
use ApiClients\Client\Travis\AsyncClient;
34
use ApiClients\Client\Travis\Resource\AccountInterface;
45
use React\EventLoop\Factory;
56
use function ApiClients\Foundation\resource_pretty_print;
67

78
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
89

9-
$loop = Factory::create();
10+
$loop = Factory::create();
1011
$client = AsyncClient::create($loop, require 'resolve_key.php');
1112

12-
$client->accounts()->subscribeCallback(function (AccountInterface $account) {
13-
resource_pretty_print($account);
14-
$account->refresh()->then(function ($account) {
15-
resource_pretty_print($account);
16-
});
17-
}, function ($e) {
18-
echo (string)$e;
19-
});
13+
$client->accounts()
14+
->subscribe(
15+
function (AccountInterface $account) {
16+
resource_pretty_print($account);
17+
$account->refresh()->then(function ($account) {
18+
resource_pretty_print($account);
19+
});
20+
},
21+
function ($e) {
22+
echo (string)$e;
23+
}
24+
);
2025

2126
$loop->run();

Diff for: examples/repositories-async.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use ApiClients\Middleware\Pool\PoolMiddleware;
99
use React\EventLoop\Factory;
1010
use ResourcePool\Pool;
11-
use Rx\Observer\CallbackObserver;
1211
use function ApiClients\Foundation\resource_pretty_print;
1312

1413
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
@@ -35,18 +34,18 @@
3534
]
3635
);
3736

38-
$client->repositories()->subscribe(new CallbackObserver(
37+
$client->repositories()->subscribe(
3938
function (RepositoryInterface $repository) {
4039
resource_pretty_print($repository);
4140
},
4241
function ($e) {
4342
echo (string)$e,
44-
PHP_EOL;
43+
PHP_EOL;
4544
},
4645
function () {
4746
echo 'Done!',
48-
PHP_EOL;
47+
PHP_EOL;
4948
}
50-
));
49+
);
5150

5251
$loop->run();

Diff for: examples/specific-build-async.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
$loop = \React\EventLoop\Factory::create();
99
$client = AsyncClient::create($loop);
1010

11-
$client->repository($argv[1] ?? 'WyriHaximus/php-travis-client')->subscribeCallback(function (Repository $repository) use ($argv) {
11+
$client->repository($argv[1] ?? 'WyriHaximus/php-travis-client')->then(function (Repository $repository) use ($argv) {
1212
echo 'Repository: ', PHP_EOL;
1313
echo 'id: ' . $repository->id(), PHP_EOL;
1414
echo 'slug: ' . $repository->slug(), PHP_EOL;
1515
echo 'description: ' . $repository->description(), PHP_EOL;
1616
echo 'Builds:', PHP_EOL;
17-
$repository->build($argv[2] ?? 126863927)->subscribeCallback(function (BuildInterface $build) {
17+
$repository->build($argv[2] ?? 126863927)->then(function (BuildInterface $build) {
1818
echo "\t", 'Build', PHP_EOL;
1919
echo "\t\t" . 'id: ' . $build->id(), PHP_EOL;
2020
echo "\t\t" . 'commit id: ' . $build->commitId(), PHP_EOL;

Diff for: examples/specific-job-async.php

+29-24
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,44 @@
11
<?php declare(strict_types=1);
2+
23
use ApiClients\Client\Travis\AsyncClient;
34
use ApiClients\Client\Travis\Resource\Async\Build;
45
use ApiClients\Client\Travis\Resource\Async\Job;
56
use ApiClients\Client\Travis\Resource\Async\Repository;
67
use React\EventLoop\Factory;
8+
use Rx\Observable;
79

810
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
911

1012
$loop = Factory::create();
1113
$client = AsyncClient::create($loop);
1214

13-
$client->repository($argv[1] ?? 'WyriHaximus/php-travis-client')->flatMap(function (Repository $repository) {
14-
echo 'Repository: ', PHP_EOL;
15-
echo 'id: ' . $repository->id(), PHP_EOL;
16-
echo 'slug: ' . $repository->slug(), PHP_EOL;
17-
echo 'description: ' . $repository->description(), PHP_EOL;
18-
echo 'Builds:', PHP_EOL;
19-
//@todo the takeLast operator is isn't implemented yet in RxPHP, so we need to fake it for now
20-
return $repository->builds()
21-
->toArray()
22-
->map(function ($builds) {
23-
return array_pop($builds);
24-
});
25-
})->flatMap(function (Build $build) use ($argv) {
26-
echo "\t", 'Build', PHP_EOL;
27-
echo "\t\t" . 'id: ' . $build->id(), PHP_EOL;
28-
echo "\t\t" . 'commit id: ' . $build->commitId(), PHP_EOL;
29-
echo "\t\t" . 'duration: ' . $build->duration(), PHP_EOL;
15+
Observable::fromPromise($client->repository($argv[1] ?? 'WyriHaximus/php-travis-client'))
16+
->flatMap(function (Repository $repository) {
17+
echo 'Repository: ', PHP_EOL;
18+
echo 'id: ' . $repository->id(), PHP_EOL;
19+
echo 'slug: ' . $repository->slug(), PHP_EOL;
20+
echo 'description: ' . $repository->description(), PHP_EOL;
21+
echo 'Builds:', PHP_EOL;
22+
//@todo the takeLast operator is isn't implemented yet in RxPHP, so we need to fake it for now
23+
return $repository->builds()
24+
->toArray()
25+
->map(function ($builds) {
26+
return array_pop($builds);
27+
});
28+
})
29+
->flatMap(function (Build $build) use ($argv) {
30+
echo "\t", 'Build', PHP_EOL;
31+
echo "\t\t" . 'id: ' . $build->id(), PHP_EOL;
32+
echo "\t\t" . 'commit id: ' . $build->commitId(), PHP_EOL;
33+
echo "\t\t" . 'duration: ' . $build->duration(), PHP_EOL;
3034

31-
return $build->job($argv[2] ?? 128670080);
32-
})->subscribeCallback(function (Job $job) {
33-
echo "\t\t" . 'Job', PHP_EOL;
34-
echo "\t\t\t" . 'id: ' . $job->id(), PHP_EOL;
35-
echo "\t\t\t" . 'number: ' . $job->number(), PHP_EOL;
36-
echo "\t\t\t" . 'state: ' . $job->state(), PHP_EOL;
37-
});
35+
return $build->job($argv[2] ?? 128670080);
36+
})
37+
->subscribe(function (Job $job) {
38+
echo "\t\t" . 'Job', PHP_EOL;
39+
echo "\t\t\t" . 'id: ' . $job->id(), PHP_EOL;
40+
echo "\t\t\t" . 'number: ' . $job->number(), PHP_EOL;
41+
echo "\t\t\t" . 'state: ' . $job->state(), PHP_EOL;
42+
});
3843

3944
$loop->run();

Diff for: src/AsyncClient.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use React\EventLoop\LoopInterface;
1111
use React\Promise\CancellablePromiseInterface;
1212
use React\Promise\PromiseInterface;
13-
use Rx\ObservableInterface;
13+
use Rx\Observable;
1414
use Rx\React\Promise;
1515
use function ApiClients\Tools\Rx\setAsyncScheduler;
1616
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
@@ -90,7 +90,7 @@ public function repository(string $repository): CancellablePromiseInterface
9090
*
9191
* {@inheritdoc}
9292
*/
93-
public function repositories(callable $filter = null): ObservableInterface
93+
public function repositories(callable $filter = null): Observable
9494
{
9595
if ($filter === null) {
9696
$filter = function () {
@@ -124,9 +124,9 @@ public function sshKey(int $id): PromiseInterface
124124
}
125125

126126
/**
127-
* @return ObservableInterface
127+
* @return Observable
128128
*/
129-
public function hooks(): ObservableInterface
129+
public function hooks(): Observable
130130
{
131131
return unwrapObservableFromPromise($this->client->handle(
132132
new Command\HooksCommand()
@@ -136,7 +136,7 @@ public function hooks(): ObservableInterface
136136
/**
137137
* {@inheritdoc}
138138
*/
139-
public function accounts(): ObservableInterface
139+
public function accounts(): Observable
140140
{
141141
return unwrapObservableFromPromise($this->client->handle(
142142
new Command\AccountsCommand()
@@ -146,7 +146,7 @@ public function accounts(): ObservableInterface
146146
/**
147147
* {@inheritdoc}
148148
*/
149-
public function broadcasts(): ObservableInterface
149+
public function broadcasts(): Observable
150150
{
151151
return unwrapObservableFromPromise($this->client->handle(
152152
new Command\BroadcastsCommand()

Diff for: src/AsyncClientInterface.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use ApiClients\Foundation\Resource\ResourceInterface;
66
use React\Promise\CancellablePromiseInterface;
77
use React\Promise\PromiseInterface;
8-
use Rx\ObservableInterface;
8+
use Rx\Observable;
99

1010
interface AsyncClientInterface
1111
{
@@ -43,10 +43,10 @@ public function repository(string $repository): CancellablePromiseInterface;
4343
* on the hooks observable before it fetches the repository information.
4444
* It could be used to only fetch new repositories.
4545
*
46-
* @param callable|null $filter
47-
* @return ObservableInterface
46+
* @param callable|null $filter
47+
* @return Observable
4848
*/
49-
public function repositories(callable $filter = null): ObservableInterface;
49+
public function repositories(callable $filter = null): Observable;
5050

5151
/**
5252
* Fetch information about the current authenticated user.
@@ -75,25 +75,25 @@ public function sshKey(int $id): PromiseInterface;
7575
* This will return a stream of Resource\Async\Hook objects.
7676
* (Requires auth token to be passed to the client!).
7777
*
78-
* @return ObservableInterface
78+
* @return Observable
7979
*/
80-
public function hooks(): ObservableInterface;
80+
public function hooks(): Observable;
8181

8282
/**
8383
* Fetch a stream of which users and orgs the currently authenticated user has access to.
8484
* This will return a stream of Resource\Async\Account objects.
8585
* (Requires auth token to be passed to the client!).
8686
*
87-
* @return ObservableInterface
87+
* @return Observable
8888
*/
89-
public function accounts(): ObservableInterface;
89+
public function accounts(): Observable;
9090

9191
/**
9292
* Fetch a list of currently active broadcast. Used by the Travis team to spread news on the site.
9393
* This will return a stream of Resource\Async\Broadcast objects.
9494
* (Requires auth token to be passed to the client!).
9595
*
96-
* @return ObservableInterface
96+
* @return Observable
9797
*/
98-
public function broadcasts(): ObservableInterface;
98+
public function broadcasts(): Observable;
9999
}

Diff for: src/Resource/Async/Repository.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use React\Promise\CancellablePromiseInterface;
1212
use React\Promise\PromiseInterface;
1313
use Rx\Observable;
14-
use Rx\ObservableInterface;
1514
use Rx\React\Promise;
1615
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
1716
use function React\Promise\resolve;
@@ -42,9 +41,9 @@ public function build(int $id): CancellablePromiseInterface
4241
}
4342

4443
/**
45-
* @return ObservableInterface
44+
* @return Observable
4645
*/
47-
public function commits(): ObservableInterface
46+
public function commits(): Observable
4847
{
4948
return unwrapObservableFromPromise($this->handleCommand(
5049
new Command\CommitsCommand($this->slug())
@@ -102,29 +101,29 @@ public function disable(): PromiseInterface
102101
}
103102

104103
/**
105-
* @return ObservableInterface
104+
* @return Observable
106105
*/
107-
public function branches(): ObservableInterface
106+
public function branches(): Observable
108107
{
109108
return unwrapObservableFromPromise($this->handleCommand(
110109
new Command\BranchesCommand($this->id())
111110
));
112111
}
113112

114113
/**
115-
* @return ObservableInterface
114+
* @return Observable
116115
*/
117-
public function vars(): ObservableInterface
116+
public function vars(): Observable
118117
{
119118
return unwrapObservableFromPromise($this->handleCommand(
120119
new Command\VarsCommand($this->id())
121120
));
122121
}
123122

124123
/**
125-
* @return ObservableInterface
124+
* @return Observable
126125
*/
127-
public function caches(): ObservableInterface
126+
public function caches(): Observable
128127
{
129128
return unwrapObservableFromPromise($this->handleCommand(
130129
new Command\CachesCommand($this->id())

0 commit comments

Comments
 (0)