Skip to content

Commit 94a45f2

Browse files
committed
Basic client with meta method
1 parent 9207c54 commit 94a45f2

22 files changed

+538
-0
lines changed

examples/meta-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\MetaInterface;
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);
12+
13+
$client->meta()->done(function (MetaInterface $meta) {
14+
resource_pretty_print($meta);
15+
});
16+
17+
$loop->run();

examples/meta.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
use ApiClients\Client\Scrutinizer\Client;
4+
use function ApiClients\Foundation\resource_pretty_print;
5+
6+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
7+
8+
$client = Client::create();
9+
10+
$meta = $client->meta();
11+
resource_pretty_print($meta);

src/ApiSettings.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiClients\Client\Scrutinizer;
6+
7+
use ApiClients\Foundation\Hydrator\Options as HydratorOptions;
8+
use ApiClients\Foundation\Options as FoundationOptions;
9+
use ApiClients\Foundation\Transport\Options as TransportOptions;
10+
use ApiClients\Middleware\HttpExceptions\HttpExceptionsMiddleware;
11+
use ApiClients\Middleware\Json\JsonDecodeMiddleware;
12+
use ApiClients\Middleware\Json\JsonEncodeMiddleware;
13+
use ApiClients\Middleware\UserAgent\Options as UserAgentMiddlewareOptions;
14+
use ApiClients\Middleware\UserAgent\UserAgentMiddleware;
15+
use ApiClients\Middleware\UserAgent\UserAgentStrategies;
16+
use function ApiClients\Foundation\options_merge;
17+
18+
final class ApiSettings
19+
{
20+
const NAMESPACE = 'ApiClients\\Client\\Scrutinizer\\Resource';
21+
const TRANSPORT_OPTIONS = [
22+
FoundationOptions::HYDRATOR_OPTIONS => [
23+
HydratorOptions::NAMESPACE => self::NAMESPACE,
24+
HydratorOptions::NAMESPACE_DIR => __DIR__ . DIRECTORY_SEPARATOR . 'Resource' . DIRECTORY_SEPARATOR,
25+
],
26+
FoundationOptions::TRANSPORT_OPTIONS => [
27+
TransportOptions::HOST => 'scrutinizer-ci.com',
28+
TransportOptions::PATH => '/api/',
29+
TransportOptions::MIDDLEWARE => [
30+
HttpExceptionsMiddleware::class,
31+
UserAgentMiddleware::class,
32+
JsonDecodeMiddleware::class,
33+
JsonEncodeMiddleware::class,
34+
],
35+
TransportOptions::DEFAULT_REQUEST_OPTIONS => [
36+
UserAgentMiddleware::class => [
37+
UserAgentMiddlewareOptions::STRATEGY => UserAgentStrategies::PACKAGE_VERSION,
38+
UserAgentMiddlewareOptions::PACKAGE => 'api-clients/scrutinizer',
39+
],
40+
],
41+
],
42+
];
43+
44+
public static function getOptions(array $suppliedOptions, string $suffix): array
45+
{
46+
$options = options_merge(self::TRANSPORT_OPTIONS, $suppliedOptions);
47+
$options[FoundationOptions::HYDRATOR_OPTIONS][HydratorOptions::NAMESPACE_SUFFIX] = $suffix;
48+
49+
return $options;
50+
}
51+
}

src/AsyncClient.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiClients\Client\Scrutinizer;
6+
7+
use ApiClients\Client\Scrutinizer\CommandBus\Command\MetaCommand;
8+
use ApiClients\Foundation\ClientInterface;
9+
use ApiClients\Foundation\Factory;
10+
use ApiClients\Foundation\Resource\ResourceInterface;
11+
use React\EventLoop\LoopInterface;
12+
use React\Promise\CancellablePromiseInterface;
13+
use React\Promise\PromiseInterface;
14+
15+
final class AsyncClient implements AsyncClientInterface
16+
{
17+
/**
18+
* @var ClientInterface
19+
*/
20+
private $client;
21+
22+
/**
23+
* @param ClientInterface $client
24+
*/
25+
private function __construct(ClientInterface $client)
26+
{
27+
$this->client = $client;
28+
}
29+
30+
/**
31+
* @param LoopInterface $loop
32+
* @param array $options
33+
* @return AsyncClient
34+
*/
35+
public static function create(LoopInterface $loop, array $options = []): self
36+
{
37+
$options = ApiSettings::getOptions($options, 'Async');
38+
$client = Factory::create($loop, $options);
39+
40+
return new self($client);
41+
}
42+
43+
/**
44+
* @internal
45+
* @param ClientInterface $client
46+
* @return AsyncClient
47+
*/
48+
public static function createFromClient(ClientInterface $client): self
49+
{
50+
return new self($client);
51+
}
52+
53+
public function hydrate(string $resource): CancellablePromiseInterface
54+
{
55+
return $this->client->hydrate($resource);
56+
}
57+
58+
public function extract(ResourceInterface $resource): CancellablePromiseInterface
59+
{
60+
return $this->client->extract($resource);
61+
}
62+
63+
public function meta(): PromiseInterface
64+
{
65+
return $this->client->handle(new MetaCommand());
66+
}
67+
}

src/AsyncClientInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiClients\Client\Scrutinizer;
6+
7+
use React\Promise\PromiseInterface;
8+
9+
interface AsyncClientInterface
10+
{
11+
public function meta(): PromiseInterface;
12+
}

src/Client.php

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;
6+
7+
use ApiClients\Client\Scrutinizer\Resource\MetaInterface;
8+
use ApiClients\Foundation\Factory as FoundationClientFactory;
9+
use React\EventLoop\Factory;
10+
use React\EventLoop\LoopInterface;
11+
use function ApiClients\Tools\Rx\setAsyncScheduler;
12+
use function Clue\React\Block\await;
13+
14+
final class Client implements ClientInterface
15+
{
16+
/**
17+
* @var LoopInterface
18+
*/
19+
private $loop;
20+
/**
21+
* @var AsyncClient
22+
*/
23+
private $client;
24+
25+
/**
26+
* @param LoopInterface $loop
27+
* @param AsyncClient $client
28+
*/
29+
private function __construct(LoopInterface $loop, AsyncClient $client)
30+
{
31+
$this->loop = $loop;
32+
$this->client = $client;
33+
}
34+
35+
/**
36+
* @param array $options
37+
* @return Client
38+
*/
39+
public static function create(array $options = []): self
40+
{
41+
$loop = Factory::create();
42+
$options = ApiSettings::getOptions($options, 'Sync');
43+
$client = FoundationClientFactory::create($loop, $options);
44+
setAsyncScheduler($loop);
45+
$asyncClient = AsyncClient::createFromClient($client);
46+
47+
return new self($loop, $asyncClient);
48+
}
49+
50+
/**
51+
* @param string $input
52+
* @return MetaInterface
53+
*/
54+
public function meta(): MetaInterface
55+
{
56+
return await($this->client->meta(), $this->loop);
57+
}
58+
}

src/ClientInterface.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+
namespace ApiClients\Client\Scrutinizer;
4+
5+
use ApiClients\Client\Scrutinizer\Resource\MetaInterface;
6+
7+
interface ClientInterface
8+
{
9+
public function meta(): MetaInterface;
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiClients\Client\Scrutinizer\CommandBus\Command;
6+
7+
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;
8+
9+
/**
10+
* @Handler("ApiClients\Client\Scrutinizer\CommandBus\Handler\MetaHandler")
11+
*/
12+
final class MetaCommand
13+
{
14+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ApiClients\Client\Scrutinizer\CommandBus\Handler;
6+
7+
use ApiClients\Client\Scrutinizer\CommandBus\Command\MetaCommand;
8+
use ApiClients\Client\Scrutinizer\Resource\MetaInterface;
9+
use ApiClients\Tools\Services\Client\FetchAndHydrateService;
10+
use React\Promise\PromiseInterface;
11+
12+
final class MetaHandler
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 MetaCommand $command
31+
* @return PromiseInterface
32+
*/
33+
public function handle(MetaCommand $command): PromiseInterface
34+
{
35+
return $this->service->fetch('meta', '', MetaInterface::HYDRATE_CLASS);
36+
}
37+
}

src/Resource/Async/EmptyMeta.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\Scrutinizer\Resource\Async;
4+
5+
use ApiClients\Client\Scrutinizer\Resource\EmptyMeta as BaseEmptyMeta;
6+
7+
class EmptyMeta extends BaseEmptyMeta
8+
{
9+
}

0 commit comments

Comments
 (0)