Skip to content

Commit 8865702

Browse files
committed
Fetch status of and add github repositories
1 parent eab1d5d commit 8865702

24 files changed

+687
-2
lines changed

examples/add-repository-async.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
3+
use ApiClients\Client\Scrutinizer\AsyncClient;
4+
use React\EventLoop\Factory;
5+
6+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
7+
8+
$loop = Factory::create();
9+
$client = AsyncClient::create($loop, require __DIR__ . DIRECTORY_SEPARATOR . 'resolve_token.php');
10+
11+
$client->github()->addRepository('php-api-clients/scrutinizer')->done(function ($response) {
12+
var_export($response);
13+
}, function ($error) {
14+
var_export($error->getResponse());
15+
});
16+
17+
$loop->run();

examples/repository-async.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
3+
use ApiClients\Client\Scrutinizer\AsyncClient;
4+
use ApiClients\Client\Scrutinizer\Resource\RepositoryInterface;
5+
use React\EventLoop\Factory;
6+
use function ApiClients\Foundation\resource_pretty_print;
7+
8+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
9+
10+
$loop = Factory::create();
11+
$client = AsyncClient::create($loop, require __DIR__ . DIRECTORY_SEPARATOR . 'resolve_token.php');
12+
13+
$client->github()->repository('php-api-clients', 'scrutinizer')->done(function (RepositoryInterface $meta) {
14+
resource_pretty_print($meta);
15+
});
16+
17+
$loop->run();

examples/resolve_token.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
$keyFile = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'token.php';
4+
5+
if (!file_exists($keyFile)) {
6+
echo 'No key file find, copy token.sample.php to token.php and add a token from https://scrutinizer-ci.com/profile/applications to run examples.', PHP_EOL;
7+
exit(1);
8+
}
9+
10+
return require $keyFile;

src/AsyncClient.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace ApiClients\Client\Scrutinizer;
66

77
use ApiClients\Client\Scrutinizer\CommandBus\Command\MetaCommand;
8+
use ApiClients\Client\Scrutinizer\Github\AsyncClient as AsyncGithubClient;
9+
use ApiClients\Client\Scrutinizer\Github\AsyncClientInterface as AsyncGithubClientInterface;
810
use ApiClients\Foundation\ClientInterface;
911
use ApiClients\Foundation\Factory;
1012
use ApiClients\Foundation\Resource\ResourceInterface;
@@ -32,9 +34,9 @@ private function __construct(ClientInterface $client)
3234
* @param array $options
3335
* @return AsyncClient
3436
*/
35-
public static function create(LoopInterface $loop, array $options = []): self
37+
public static function create(LoopInterface $loop, string $token, array $options = []): self
3638
{
37-
$options = ApiSettings::getOptions($options, 'Async');
39+
$options = ApiSettings::getOptions($token, $options, 'Async');
3840
$client = Factory::create($loop, $options);
3941

4042
return new self($client);
@@ -64,4 +66,9 @@ public function meta(): PromiseInterface
6466
{
6567
return $this->client->handle(new MetaCommand());
6668
}
69+
70+
public function github(): AsyncGithubClientInterface
71+
{
72+
return new AsyncGithubClient($this->client);
73+
}
6774
}

src/AsyncClientInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
namespace ApiClients\Client\Scrutinizer;
66

7+
use ApiClients\Client\Scrutinizer\Github\AsyncClientInterface as AsyncGithubClientInterface;
78
use React\Promise\PromiseInterface;
89

910
interface AsyncClientInterface
1011
{
12+
public function github(): AsyncGithubClientInterface;
13+
1114
public function meta(): PromiseInterface;
1215
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiClients\Client\Scrutinizer\CommandBus\Command\Github;
6+
7+
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;
8+
9+
/**
10+
* @Handler("ApiClients\Client\Scrutinizer\CommandBus\Handler\Github\AddRepositoryHandler")
11+
*/
12+
final class AddRepositoryCommand
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private $name;
18+
19+
/**
20+
* @param string $name
21+
*/
22+
public function __construct(string $name)
23+
{
24+
$this->name = $name;
25+
}
26+
27+
/**
28+
* @return string
29+
*/
30+
public function getName(): string
31+
{
32+
return $this->name;
33+
}
34+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiClients\Client\Scrutinizer\CommandBus\Command\Github;
6+
7+
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;
8+
9+
/**
10+
* @Handler("ApiClients\Client\Scrutinizer\CommandBus\Handler\Github\RepositoryHandler")
11+
*/
12+
final class RepositoryCommand
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private $login;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $name;
23+
24+
/**
25+
* @param string $login
26+
* @param string $name
27+
*/
28+
public function __construct(string $login, string $name)
29+
{
30+
$this->login = $login;
31+
$this->name = $name;
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public function getLogin(): string
38+
{
39+
return $this->login;
40+
}
41+
42+
/**
43+
* @return string
44+
*/
45+
public function getName(): string
46+
{
47+
return $this->name;
48+
}
49+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiClients\Client\Scrutinizer\CommandBus\Handler\Github;
6+
7+
use ApiClients\Client\Scrutinizer\CommandBus\Command\Github\AddRepositoryCommand;
8+
use ApiClients\Client\Scrutinizer\Resource\RepositoryInterface;
9+
use ApiClients\Foundation\Hydrator\Hydrator;
10+
use ApiClients\Foundation\Transport\Service\RequestService;
11+
use ApiClients\Middleware\Json\JsonStream;
12+
use React\Promise\PromiseInterface;
13+
use RingCentral\Psr7\Request;
14+
15+
final class AddRepositoryHandler
16+
{
17+
/**
18+
* @var RequestService
19+
*/
20+
private $requestService;
21+
22+
/**
23+
* @var Hydrator
24+
*/
25+
private $hydrator;
26+
27+
/**
28+
* @param RequestService $requestService
29+
* @param Hydrator $hydrator
30+
*/
31+
public function __construct(RequestService $requestService, Hydrator $hydrator)
32+
{
33+
$this->requestService = $requestService;
34+
$this->hydrator = $hydrator;
35+
}
36+
37+
/**
38+
* Fetch the given repository and hydrate it.
39+
*
40+
* @param AddRepositoryCommand $command
41+
* @return PromiseInterface
42+
*/
43+
public function handle(AddRepositoryCommand $command): PromiseInterface
44+
{
45+
return $this->requestService->request(
46+
new Request(
47+
'POST',
48+
'repositories/g',
49+
[],
50+
new JsonStream([
51+
'name' => $command->getName(),
52+
])
53+
)
54+
)->then(function ($repository) {
55+
return $this->hydrator->hydrate(RepositoryInterface::HYDRATE_CLASS, $repository->getBody()->getJson());
56+
});
57+
}
58+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiClients\Client\Scrutinizer\CommandBus\Handler\Github;
6+
7+
use ApiClients\Client\Scrutinizer\CommandBus\Command\Github\RepositoryCommand;
8+
use ApiClients\Client\Scrutinizer\Resource\RepositoryInterface;
9+
use ApiClients\Tools\Services\Client\FetchAndHydrateService;
10+
use React\Promise\PromiseInterface;
11+
12+
final class RepositoryHandler
13+
{
14+
/**
15+
* @var FetchAndHydrateService
16+
*/
17+
private $service;
18+
19+
/**
20+
* @param FetchAndHydrateService $service
21+
*/
22+
public function __construct(FetchAndHydrateService $service)
23+
{
24+
$this->service = $service;
25+
}
26+
27+
/**
28+
* Fetch the given repository and hydrate it.
29+
*
30+
* @param RepositoryCommand $command
31+
* @return PromiseInterface
32+
*/
33+
public function handle(RepositoryCommand $command): PromiseInterface
34+
{
35+
return $this->service->fetch(
36+
'repositories/g/' . $command->getLogin() . '/' . $command->getName(),
37+
'',
38+
RepositoryInterface::HYDRATE_CLASS
39+
);
40+
}
41+
}

src/Github/AsyncClient.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiClients\Client\Scrutinizer\Github;
6+
7+
use ApiClients\Client\Scrutinizer\CommandBus\Command\Github\AddRepositoryCommand;
8+
use ApiClients\Client\Scrutinizer\CommandBus\Command\Github\RepositoryCommand;
9+
use ApiClients\Foundation\ClientInterface as FoundationClientInterface;
10+
use ApiClients\Foundation\Resource\ResourceInterface;
11+
use React\Promise\CancellablePromiseInterface;
12+
use React\Promise\PromiseInterface;
13+
14+
final class AsyncClient implements AsyncClientInterface
15+
{
16+
/**
17+
* @var FoundationClientInterface
18+
*/
19+
protected $client;
20+
21+
/**
22+
* @param FoundationClientInterface $client
23+
*/
24+
public function __construct(FoundationClientInterface $client)
25+
{
26+
$this->client = $client;
27+
}
28+
29+
public function hydrate(string $resource): CancellablePromiseInterface
30+
{
31+
return $this->client->hydrate($resource);
32+
}
33+
34+
public function extract(ResourceInterface $resource): CancellablePromiseInterface
35+
{
36+
return $this->client->extract($resource);
37+
}
38+
39+
public function repository(string $login, string $name): PromiseInterface
40+
{
41+
return $this->client->handle(new RepositoryCommand($login, $name));
42+
}
43+
44+
public function addRepository(string $name): PromiseInterface
45+
{
46+
return $this->client->handle(new AddRepositoryCommand($name));
47+
}
48+
}

0 commit comments

Comments
 (0)