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
32 changes: 32 additions & 0 deletions examples/user-repository-replace-topics-async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

use ApiClients\Client\Github\AsyncClient;
use ApiClients\Client\Github\Resource\Async\Repository;
use ApiClients\Client\Github\Resource\UserInterface;
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->user($argv[1] ?? 'WyriHaximus')->then(function (UserInterface $user) use ($argv) {
resource_pretty_print($user);

return $user->repository($argv[2] ?? 'awesome-volkswagen');
})->then(function (Repository $repository) use ($argv) {
resource_pretty_print($repository);

return $repository->replaceTopics(
(string)($argv[3] ?? 'test-' . time()),
(string)($argv[4] ?? random_int(100000, 9999999))
);
})->done(function (array $topics) {
var_export($topics);
echo 'Done!', PHP_EOL;
}, 'display_throwable');

$loop->run();

displayState($client->getRateLimitState());
47 changes: 47 additions & 0 deletions src/CommandBus/Command/Repository/ReplaceTopicsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\CommandBus\Command\Repository;

use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;

/**
* @Handler("ApiClients\Client\Github\CommandBus\Handler\Repository\ReplaceTopicsHandler")
*/
final class ReplaceTopicsCommand
{
/**
* @var string
*/
private $repository;

/**
* @var string[]
*/
private $topics;

/**
* @param string $repository
* @param string[] $topics
*/
public function __construct($repository, string ...$topics)
{
$this->repository = $repository;
$this->topics = $topics;
}

/**
* @return string
*/
public function getRepository(): string
{
return $this->repository;
}

/**
* @return string[]
*/
public function getTopics(): array
{
return $this->topics;
}
}
45 changes: 45 additions & 0 deletions src/CommandBus/Handler/Repository/ReplaceTopicsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\CommandBus\Handler\Repository;

use ApiClients\Client\Github\CommandBus\Command\Repository\ReplaceTopicsCommand;
use ApiClients\Foundation\Transport\Service\RequestService;
use ApiClients\Middleware\Json\JsonStream;
use React\Promise\PromiseInterface;
use RingCentral\Psr7\Request;

final class ReplaceTopicsHandler
{
/**
* @var RequestService
*/
private $requestService;

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

/**
* @param ReplaceTopicsCommand $command
* @return PromiseInterface
*/
public function handle(ReplaceTopicsCommand $command): PromiseInterface
{
return $this->requestService->request(
new Request(
'PUT',
'repos/' . $command->getRepository() . '/topics',
[],
new JsonStream([
'names' => $command->getTopics(),
])
)
)->then(function ($response) {
return $response->getBody()->getJson()['names'];
});
}
}
8 changes: 8 additions & 0 deletions src/Resource/Async/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use ApiClients\Client\Github\CommandBus\Command\Repository\LabelsCommand;
use ApiClients\Client\Github\CommandBus\Command\Repository\LanguagesCommand;
use ApiClients\Client\Github\CommandBus\Command\Repository\ReleasesCommand;
use ApiClients\Client\Github\CommandBus\Command\Repository\ReplaceTopicsCommand;
use ApiClients\Client\Github\CommandBus\Command\Repository\SubscribeCommand;
use ApiClients\Client\Github\CommandBus\Command\Repository\TagsCommand;
use ApiClients\Client\Github\CommandBus\Command\Repository\UnSubscribeCommand;
Expand Down Expand Up @@ -129,4 +130,11 @@ public function unSubscribe(): PromiseInterface
new UnSubscribeCommand($this->fullName())
);
}

public function replaceTopics(string ...$topics): PromiseInterface
{
return $this->handleCommand(
new ReplaceTopicsCommand($this->fullName(), ...$topics)
);
}
}
45 changes: 45 additions & 0 deletions tests/CommandBus/Handler/Repository/ReplaceTopicsHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace ApiClients\Tests\Github\CommandBus\Handler\Repository;

use ApiClients\Client\Github\CommandBus\Command\Repository\ReplaceTopicsCommand;
use ApiClients\Client\Github\CommandBus\Handler\Repository\ReplaceTopicsHandler;
use ApiClients\Foundation\Transport\Service\RequestService;
use ApiClients\Middleware\Json\JsonStream;
use ApiClients\Tools\TestUtilities\TestCase;
use RingCentral\Psr7\Request;
use RingCentral\Psr7\Response;
use function React\Promise\resolve;

final class ReplaceTopicsHandlerTest extends TestCase
{
public function testCommand()
{
$topics = [
'foo',
'bar',
];
$repository = 'repository';
$request = new Request(
'PUT',
'repos/' . $repository . '/topics',
[],
new JsonStream([
'names' => $topics,
])
);
$response = new Response(
200,
[],
new JsonStream([
'names' => $topics,
])
);

$service = $this->prophesize(RequestService::class);
$service->request($request)->shouldBeCalled()->willReturn(resolve($response));

$handler = new ReplaceTopicsHandler($service->reveal());
$handler->handle(new ReplaceTopicsCommand($repository, ...$topics));
}
}