Skip to content

Commit f57b5d3

Browse files
committed
User events
1 parent 685152e commit f57b5d3

28 files changed

+735
-0
lines changed

examples/user-events-async.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
use ApiClients\Client\Github\AsyncClient;
3+
use ApiClients\Client\Github\Resource\Async\Event;
4+
use ApiClients\Client\Github\Resource\Async\User;
5+
use React\EventLoop\Factory;
6+
use function ApiClients\Foundation\resource_pretty_print;
7+
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;
8+
9+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
10+
11+
$loop = Factory::create();
12+
13+
$client = AsyncClient::create($loop, require 'resolve_token.php');
14+
15+
unwrapObservableFromPromise($client->user($argv[1] ?? 'php-api-clients')->then(function (User $user) use ($argv) {
16+
resource_pretty_print($user);
17+
18+
return $user->events();
19+
}))->subscribe(function (Event $event) {
20+
resource_pretty_print($event);
21+
}, 'display_throwable');
22+
23+
$loop->run();
24+
25+
displayState($client->getRateLimitState());
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+
3+
namespace ApiClients\Client\Github\CommandBus\Command\User;
4+
5+
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;
6+
7+
/**
8+
* @Handler("ApiClients\Client\Github\CommandBus\Handler\User\EventsHandler")
9+
*/
10+
final class EventsCommand
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $login;
16+
17+
/**
18+
* @param string $login
19+
*/
20+
public function __construct(string $login)
21+
{
22+
$this->login = $login;
23+
}
24+
25+
/**
26+
* @return string
27+
*/
28+
public function getLogin(): string
29+
{
30+
return $this->login;
31+
}
32+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Handler\User;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\User\EventsCommand;
6+
use ApiClients\Client\Github\Resource\EventInterface;
7+
use ApiClients\Client\Github\Service\IteratePagesService;
8+
use ApiClients\Foundation\Hydrator\Hydrator;
9+
use React\Promise\PromiseInterface;
10+
use function ApiClients\Tools\Rx\observableFromArray;
11+
use function React\Promise\resolve;
12+
13+
final class EventsHandler
14+
{
15+
/**
16+
* @var IteratePagesService
17+
*/
18+
private $service;
19+
20+
/**
21+
* @var Hydrator
22+
*/
23+
private $hydrator;
24+
25+
/**
26+
* @param IteratePagesService $service
27+
* @param Hydrator $hydrator
28+
*/
29+
public function __construct(IteratePagesService $service, Hydrator $hydrator)
30+
{
31+
$this->service = $service;
32+
$this->hydrator = $hydrator;
33+
}
34+
35+
/**
36+
* @param EventsCommand $command
37+
* @return PromiseInterface
38+
*/
39+
public function handle(EventsCommand $command): PromiseInterface
40+
{
41+
return resolve(
42+
$this->service->iterate('users/' . $command->getLogin() . '/events')
43+
->flatMap(function ($events) {
44+
return observableFromArray($events);
45+
})->map(function ($event) {
46+
return $this->hydrator->hydrate(EventInterface::HYDRATE_CLASS, $event);
47+
})
48+
);
49+
}
50+
}

src/Resource/Async/EmptyEvent.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\Resource\Async;
4+
5+
use ApiClients\Client\Github\Resource\EmptyEvent as BaseEmptyEvent;
6+
7+
class EmptyEvent extends BaseEmptyEvent
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\Resource\Async;
4+
5+
use ApiClients\Client\Github\Resource\EmptyRepositorySimple as BaseEmptyRepositorySimple;
6+
7+
class EmptyRepositorySimple extends BaseEmptyRepositorySimple
8+
{
9+
}

src/Resource/Async/Event.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\Resource\Async;
4+
5+
use ApiClients\Client\Github\Resource\Event as BaseEvent;
6+
7+
class Event extends BaseEvent
8+
{
9+
public function refresh(): Event
10+
{
11+
throw new \Exception('TODO: create refresh method!');
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\Resource\Async;
4+
5+
use ApiClients\Client\Github\Resource\RepositorySimple as BaseRepositorySimple;
6+
7+
class RepositorySimple extends BaseRepositorySimple
8+
{
9+
public function refresh(): RepositorySimple
10+
{
11+
throw new \Exception('TODO: create refresh method!');
12+
}
13+
}

src/Resource/Async/User.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ApiClients\Client\Github\Resource\Async;
44

55
use ApiClients\Client\Github\CommandBus\Command\RefreshCommand;
6+
use ApiClients\Client\Github\CommandBus\Command\User\EventsCommand;
67
use ApiClients\Client\Github\CommandBus\Command\User\OrganizationsCommand;
78
use ApiClients\Client\Github\CommandBus\Command\User\RepositoriesCommand;
89
use ApiClients\Client\Github\CommandBus\Command\User\RepositoryCommand;
@@ -40,4 +41,11 @@ public function organizations(): ObservableInterface
4041
new OrganizationsCommand($this->login())
4142
));
4243
}
44+
45+
public function events(): ObservableInterface
46+
{
47+
return unwrapObservableFromPromise($this->handleCommand(
48+
new EventsCommand($this->login())
49+
));
50+
}
4351
}

src/Resource/EmptyEvent.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\Resource;
4+
5+
use ApiClients\Foundation\Resource\EmptyResourceInterface;
6+
use DateTimeInterface;
7+
8+
abstract class EmptyEvent implements EventInterface, EmptyResourceInterface
9+
{
10+
/**
11+
* @return int
12+
*/
13+
public function id(): int
14+
{
15+
return null;
16+
}
17+
18+
/**
19+
* @return string
20+
*/
21+
public function type(): string
22+
{
23+
return null;
24+
}
25+
26+
/**
27+
* @return User
28+
*/
29+
public function actor(): User
30+
{
31+
return null;
32+
}
33+
34+
/**
35+
* @return RepositorySimple
36+
*/
37+
public function repo(): RepositorySimple
38+
{
39+
return null;
40+
}
41+
42+
/**
43+
* @return array
44+
*/
45+
public function payload(): array
46+
{
47+
return null;
48+
}
49+
50+
/**
51+
* @return bool
52+
*/
53+
public function public(): bool
54+
{
55+
return null;
56+
}
57+
58+
/**
59+
* @return DateTimeInterface
60+
*/
61+
public function createdAt(): DateTimeInterface
62+
{
63+
return null;
64+
}
65+
}
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+
3+
namespace ApiClients\Client\Github\Resource;
4+
5+
use ApiClients\Foundation\Resource\EmptyResourceInterface;
6+
7+
abstract class EmptyRepositorySimple implements RepositorySimpleInterface, EmptyResourceInterface
8+
{
9+
/**
10+
* @return int
11+
*/
12+
public function id(): int
13+
{
14+
return null;
15+
}
16+
17+
/**
18+
* @return string
19+
*/
20+
public function name(): string
21+
{
22+
return null;
23+
}
24+
25+
/**
26+
* @return string
27+
*/
28+
public function url(): string
29+
{
30+
return null;
31+
}
32+
}

0 commit comments

Comments
 (0)