Skip to content

Commit 5a937fd

Browse files
authored
Merge pull request #64 from php-api-clients/fetch-milestones
Fetch milestones
2 parents 713592b + 4c60501 commit 5a937fd

File tree

12 files changed

+451
-0
lines changed

12 files changed

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

src/Resource/Async/Repository.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiClients\Client\Github\CommandBus\Command\Repository\DetailedCommitCommand;
1717
use ApiClients\Client\Github\CommandBus\Command\Repository\LabelsCommand;
1818
use ApiClients\Client\Github\CommandBus\Command\Repository\LanguagesCommand;
19+
use ApiClients\Client\Github\CommandBus\Command\Repository\MilestonesCommand;
1920
use ApiClients\Client\Github\CommandBus\Command\Repository\RefCommand;
2021
use ApiClients\Client\Github\CommandBus\Command\Repository\RefsCommand;
2122
use ApiClients\Client\Github\CommandBus\Command\Repository\ReleasesCommand;
@@ -130,6 +131,13 @@ public function webHooks(): ObservableInterface
130131
));
131132
}
132133

134+
public function milestones(): ObservableInterface
135+
{
136+
return unwrapObservableFromPromise($this->handleCommand(
137+
new MilestonesCommand($this->fullName())
138+
));
139+
}
140+
133141
public function addWebHook(
134142
string $name,
135143
array $config,
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\Repository;
4+
5+
use ApiClients\Client\Github\Resource\Repository\EmptyMilestone as BaseEmptyMilestone;
6+
7+
class EmptyMilestone extends BaseEmptyMilestone
8+
{
9+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\Resource\Async\Repository;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\RefreshCommand;
6+
use ApiClients\Client\Github\Resource\Repository\Milestone as BaseMilestone;
7+
use React\Promise\PromiseInterface;
8+
9+
class Milestone extends BaseMilestone
10+
{
11+
public function refresh(): PromiseInterface
12+
{
13+
return $this->handleCommand(new RefreshCommand($this));
14+
}
15+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\Resource\Repository;
4+
5+
use ApiClients\Foundation\Resource\EmptyResourceInterface;
6+
7+
abstract class EmptyMilestone implements MilestoneInterface, EmptyResourceInterface
8+
{
9+
/**
10+
* @return int
11+
*/
12+
public function id(): int
13+
{
14+
return null;
15+
}
16+
17+
/**
18+
* @return int
19+
*/
20+
public function number(): int
21+
{
22+
return null;
23+
}
24+
25+
/**
26+
* @return string
27+
*/
28+
public function state(): string
29+
{
30+
return null;
31+
}
32+
33+
/**
34+
* @return string
35+
*/
36+
public function title(): string
37+
{
38+
return null;
39+
}
40+
41+
/**
42+
* @return string
43+
*/
44+
public function description(): string
45+
{
46+
return null;
47+
}
48+
49+
/**
50+
* @return User
51+
*/
52+
public function creator(): User
53+
{
54+
return null;
55+
}
56+
57+
/**
58+
* @return int
59+
*/
60+
public function openIssues(): int
61+
{
62+
return null;
63+
}
64+
65+
/**
66+
* @return int
67+
*/
68+
public function closedIssues(): int
69+
{
70+
return null;
71+
}
72+
73+
/**
74+
* @return string
75+
*/
76+
public function url(): string
77+
{
78+
return null;
79+
}
80+
}

src/Resource/Repository/Milestone.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\Resource\Repository;
4+
5+
use ApiClients\Foundation\Hydrator\Annotation\EmptyResource;
6+
use ApiClients\Foundation\Hydrator\Annotation\Nested;
7+
use ApiClients\Foundation\Resource\AbstractResource;
8+
9+
/**
10+
* @Nested(
11+
* creator="User"
12+
* )
13+
* @EmptyResource("Repository\EmptyMilestone")
14+
*/
15+
abstract class Milestone extends AbstractResource implements MilestoneInterface
16+
{
17+
/**
18+
* @var int
19+
*/
20+
protected $id;
21+
22+
/**
23+
* @var int
24+
*/
25+
protected $number;
26+
27+
/**
28+
* @var string
29+
*/
30+
protected $state;
31+
32+
/**
33+
* @var string
34+
*/
35+
protected $title;
36+
37+
/**
38+
* @var string
39+
*/
40+
protected $description;
41+
42+
/**
43+
* @var User
44+
*/
45+
protected $creator;
46+
47+
/**
48+
* @var int
49+
*/
50+
protected $open_issues;
51+
52+
/**
53+
* @var int
54+
*/
55+
protected $closed_issues;
56+
57+
/**
58+
* @var string
59+
*/
60+
protected $url;
61+
62+
/**
63+
* @return int
64+
*/
65+
public function id(): int
66+
{
67+
return $this->id;
68+
}
69+
70+
/**
71+
* @return int
72+
*/
73+
public function number(): int
74+
{
75+
return $this->number;
76+
}
77+
78+
/**
79+
* @return string
80+
*/
81+
public function state(): string
82+
{
83+
return $this->state;
84+
}
85+
86+
/**
87+
* @return string
88+
*/
89+
public function title(): string
90+
{
91+
return $this->title;
92+
}
93+
94+
/**
95+
* @return string
96+
*/
97+
public function description(): string
98+
{
99+
return $this->description;
100+
}
101+
102+
/**
103+
* @return User
104+
*/
105+
public function creator(): User
106+
{
107+
return $this->creator;
108+
}
109+
110+
/**
111+
* @return int
112+
*/
113+
public function openIssues(): int
114+
{
115+
return $this->open_issues;
116+
}
117+
118+
/**
119+
* @return int
120+
*/
121+
public function closedIssues(): int
122+
{
123+
return $this->closed_issues;
124+
}
125+
126+
/**
127+
* @return string
128+
*/
129+
public function url(): string
130+
{
131+
return $this->url;
132+
}
133+
}

0 commit comments

Comments
 (0)