Skip to content

Resource: License #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/licenses-async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use ApiClients\Client\Github\AsyncClient;
use ApiClients\Client\Github\Resource\LicenseInterface;
use ApiClients\Client\Github\Resource\OrganizationInterface;
use React\EventLoop\Factory;
use function ApiClients\Foundation\resource_pretty_print;

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

$loop = Factory::create();
$client = AsyncClient::create($loop, require 'resolve_token.php');

$client->licenses()->subscribeCallback(function (LicenseInterface $license) {
resource_pretty_print($license);
}, 'display_throwable');

$loop->run();
5 changes: 5 additions & 0 deletions src/AsyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public function myOrganizations(): Observable
return unwrapObservableFromPromise($this->client->handle(new Command\MyOrganizationsCommand()));
}

public function licenses(): Observable
{
return unwrapObservableFromPromise($this->client->handle(new Command\LicensesCommand()));
}

/**
* @return RateLimitState
*/
Expand Down
2 changes: 2 additions & 0 deletions src/AsyncClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public function whoami(): PromiseInterface;

public function myOrganizations(): Observable;

public function licenses(): Observable;

public function getRateLimitState(): RateLimitState;
}
12 changes: 12 additions & 0 deletions src/CommandBus/Command/LicensesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\CommandBus\Command;

use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;

/**
* @Handler("ApiClients\Client\Github\CommandBus\Handler\LicensesHandler")
*/
final class LicensesCommand
{
}
42 changes: 42 additions & 0 deletions src/CommandBus/Handler/LicensesHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\CommandBus\Handler;

use ApiClients\Client\Github\CommandBus\Command\LicensesCommand;
use ApiClients\Client\Github\CommandBus\Command\MyOrganizationsCommand;
use ApiClients\Client\Github\Resource\LicenseInterface;
use ApiClients\Tools\Services\Client\FetchAndIterateService;
use React\Promise\PromiseInterface;
use function React\Promise\resolve;

final class LicensesHandler
{
/**
* @var FetchAndIterateService
*/
private $service;

/**
* LicensesHandler constructor.
* @param FetchAndIterateService $service
*/
public function __construct(FetchAndIterateService $service)
{
$this->service = $service;
}

/**
* @param MyOrganizationsCommand $command
* @return PromiseInterface
*/
public function handle(LicensesCommand $command): PromiseInterface
{
return resolve(
$this->service->iterate(
'licenses',
'',
LicenseInterface::HYDRATE_CLASS
)
);
}
}
34 changes: 34 additions & 0 deletions tests/CommandBus/Handler/LicensesHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace ApiClients\Tests\Github\CommandBus\Handler;

use ApiClients\Client\Github\CommandBus\Command\LicensesCommand;
use ApiClients\Client\Github\CommandBus\Handler\LicensesHandler;
use ApiClients\Client\Github\Resource\LicenseInterface;
use ApiClients\Tools\Services\Client\FetchAndIterateService;
use ApiClients\Tools\TestUtilities\TestCase;
use Rx\Observable;
use Rx\React\Promise;

final class LicensesHandlerTest extends TestCase
{
public function testCommand()
{
$license = $this->prophesize(LicenseInterface::class)->reveal();

$command = new LicensesCommand();

$service = $this->prophesize(FetchAndIterateService::class);
$service->iterate(
'licenses',
'',
LicenseInterface::HYDRATE_CLASS
)->shouldBeCalled()->willReturn(Observable::fromArray([$license]));

$licensesHandler = new LicensesHandler(
$service->reveal()
);

self::assertSame($license, $this->await(Promise::fromObservable($this->await($licensesHandler->handle($command)))));
}
}