Skip to content
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
38 changes: 38 additions & 0 deletions examples/organization-async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

use ApiClients\Client\Github\AsyncClient;
use ApiClients\Client\Github\Resource\Async\Organization;
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');

$organizations = [
'php-api-clients',
'reactphp',
'recoilphp',
];

if (count($argv) > 1) {
unset($argv[0]);
foreach ($argv as $organization) {
$organizations[] = $organization;
}
}

foreach ($organizations as $organization) {
$client->organization($organization)->then(function (Organization $user) {
resource_pretty_print($user);

return $user->refresh();
})->done(function (Organization $user) {
resource_pretty_print($user);
}, 'display_throwable');
}

$loop->run();

displayState($client->getRateLimitState());
9 changes: 9 additions & 0 deletions src/AsyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ public function user(string $user): PromiseInterface
return $this->client->handle(new Command\UserCommand($user));
}

/**
* @param string $organization
* @return PromiseInterface
*/
public function organization(string $organization): PromiseInterface
{
return $this->client->handle(new Command\OrganizationCommand($organization));
}

/**
* @return PromiseInterface
*/
Expand Down
2 changes: 2 additions & 0 deletions src/AsyncClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function meta(): PromiseInterface;

public function user(string $user): PromiseInterface;

public function organization(string $organization): PromiseInterface;

public function whoami(): PromiseInterface;

public function emojis(): Observable;
Expand Down
32 changes: 32 additions & 0 deletions src/CommandBus/Command/OrganizationCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\CommandBus\Command;

use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;

/**
* @Handler("ApiClients\Client\Github\CommandBus\Handler\OrganizationHandler")
*/
final class OrganizationCommand
{
/**
* @var string
*/
private $organization;

/**
* @param string $organization
*/
public function __construct(string $organization = '')
{
$this->organization = $organization;
}

/**
* @return string
*/
public function getOrganization(): string
{
return $this->organization;
}
}
35 changes: 35 additions & 0 deletions src/CommandBus/Handler/OrganizationHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\CommandBus\Handler;

use ApiClients\Client\Github\CommandBus\Command\OrganizationCommand;
use ApiClients\Client\Github\Resource\OrganizationInterface;
use ApiClients\Tools\Services\Client\FetchAndHydrateService;
use React\Promise\PromiseInterface;

final class OrganizationHandler
{
/**
* @var FetchAndHydrateService
*/
private $service;

/**
* @param FetchAndHydrateService $service
*/
public function __construct(FetchAndHydrateService $service)
{
$this->service = $service;
}

/**
* Fetch the given repository and hydrate it.
*
* @param OrganizationCommand $command
* @return PromiseInterface
*/
public function handle(OrganizationCommand $command): PromiseInterface
{
return $this->service->fetch('orgs/' . $command->getOrganization(), '', OrganizationInterface::HYDRATE_CLASS);
}
}
34 changes: 32 additions & 2 deletions src/Resource/Async/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,42 @@

namespace ApiClients\Client\Github\Resource\Async;

use ApiClients\Client\Github\CommandBus\Command\RefreshCommand;
use ApiClients\Client\Github\CommandBus\Command\User\OrganizationsCommand;
use ApiClients\Client\Github\CommandBus\Command\User\RepositoriesCommand;
use ApiClients\Client\Github\CommandBus\Command\User\RepositoryCommand;
use ApiClients\Client\Github\Resource\Organization as BaseOrganization;
use React\Promise\PromiseInterface;
use Rx\ObservableInterface;
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;

class Organization extends BaseOrganization
{
public function refresh(): Organization
public function refresh(): PromiseInterface
{
throw new \Exception('TODO: create refresh method!');
return $this->handleCommand(
new RefreshCommand($this)
);
}

public function repository(string $repository): PromiseInterface
{
return $this->handleCommand(
new RepositoryCommand($this->login(), $repository)
);
}

public function repositories(): ObservableInterface
{
return unwrapObservableFromPromise($this->handleCommand(
new RepositoriesCommand($this->login())
));
}

public function organizations(): ObservableInterface
{
return unwrapObservableFromPromise($this->handleCommand(
new OrganizationsCommand($this->login())
));
}
}
22 changes: 22 additions & 0 deletions tests/CommandBus/Handler/OrganizationHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace ApiClients\Tests\Github\CommandBus\Handler;

use ApiClients\Client\Github\CommandBus\Command\OrganizationCommand;
use ApiClients\Client\Github\CommandBus\Handler\OrganizationHandler;
use ApiClients\Client\Github\Resource\OrganizationInterface;
use ApiClients\Tools\Services\Client\FetchAndHydrateService;
use ApiClients\Tools\TestUtilities\TestCase;

final class OrganizationHandlerTest extends TestCase
{
public function testCommand()
{
$service = $this->prophesize(FetchAndHydrateService::class);
$service->fetch('orgs/php-api-clients', '', OrganizationInterface::HYDRATE_CLASS)->shouldBeCalled();

$handler = new OrganizationHandler($service->reveal());

$handler->handle(new OrganizationCommand('php-api-clients'));
}
}