Skip to content

Commit e21ecdd

Browse files
committed
Replace topics on a repository
1 parent 3ef44b2 commit e21ecdd

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
use ApiClients\Client\Github\AsyncClient;
4+
use ApiClients\Client\Github\Resource\Async\Repository;
5+
use ApiClients\Client\Github\Resource\UserInterface;
6+
use React\EventLoop\Factory;
7+
use function ApiClients\Foundation\resource_pretty_print;
8+
9+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
10+
11+
$loop = Factory::create();
12+
$client = AsyncClient::create($loop, require 'resolve_token.php');
13+
14+
$client->user($argv[1] ?? 'WyriHaximus')->then(function (UserInterface $user) use ($argv) {
15+
resource_pretty_print($user);
16+
17+
return $user->repository($argv[2] ?? 'awesome-volkswagen');
18+
})->then(function (Repository $repository) use ($argv) {
19+
resource_pretty_print($repository);
20+
21+
return $repository->replaceTopics(
22+
(string)($argv[3] ?? 'test-' . time()),
23+
(string)($argv[4] ?? random_int(100000, 9999999))
24+
);
25+
})->done(function (array $topics) {
26+
var_export($topics);
27+
echo 'Done!', PHP_EOL;
28+
}, 'display_throwable');
29+
30+
$loop->run();
31+
32+
displayState($client->getRateLimitState());
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Command\Repository;
4+
5+
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;
6+
7+
/**
8+
* @Handler("ApiClients\Client\Github\CommandBus\Handler\Repository\ReplaceTopicsHandler")
9+
*/
10+
final class ReplaceTopicsCommand
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $repository;
16+
17+
/**
18+
* @var string[]
19+
*/
20+
private $topics;
21+
22+
/**
23+
* @param string $repository
24+
* @param string[] $topics
25+
*/
26+
public function __construct($repository, string ...$topics)
27+
{
28+
$this->repository = $repository;
29+
$this->topics = $topics;
30+
}
31+
32+
/**
33+
* @return string
34+
*/
35+
public function getRepository(): string
36+
{
37+
return $this->repository;
38+
}
39+
40+
/**
41+
* @return string[]
42+
*/
43+
public function getTopics(): array
44+
{
45+
return $this->topics;
46+
}
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Handler\Repository;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\Repository\ReplaceTopicsCommand;
6+
use ApiClients\Foundation\Transport\Service\RequestService;
7+
use ApiClients\Middleware\Json\JsonStream;
8+
use React\Promise\PromiseInterface;
9+
use RingCentral\Psr7\Request;
10+
11+
final class ReplaceTopicsHandler
12+
{
13+
/**
14+
* @var RequestService
15+
*/
16+
private $requestService;
17+
18+
/**
19+
* @param RequestService $requestService
20+
*/
21+
public function __construct(RequestService $requestService)
22+
{
23+
$this->requestService = $requestService;
24+
}
25+
26+
/**
27+
* @param ReplaceTopicsCommand $command
28+
* @return PromiseInterface
29+
*/
30+
public function handle(ReplaceTopicsCommand $command): PromiseInterface
31+
{
32+
return $this->requestService->request(
33+
new Request(
34+
'PUT',
35+
'repos/' . $command->getRepository() . '/topics',
36+
[],
37+
new JsonStream([
38+
'names' => $command->getTopics(),
39+
])
40+
)
41+
)->then(function ($response) {
42+
return $response->getBody()->getJson()['names'];
43+
});
44+
}
45+
}

src/Resource/Async/Repository.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use ApiClients\Client\Github\CommandBus\Command\Repository\LabelsCommand;
1313
use ApiClients\Client\Github\CommandBus\Command\Repository\LanguagesCommand;
1414
use ApiClients\Client\Github\CommandBus\Command\Repository\ReleasesCommand;
15+
use ApiClients\Client\Github\CommandBus\Command\Repository\ReplaceTopicsCommand;
1516
use ApiClients\Client\Github\CommandBus\Command\Repository\SubscribeCommand;
1617
use ApiClients\Client\Github\CommandBus\Command\Repository\TagsCommand;
1718
use ApiClients\Client\Github\CommandBus\Command\Repository\UnSubscribeCommand;
@@ -129,4 +130,11 @@ public function unSubscribe(): PromiseInterface
129130
new UnSubscribeCommand($this->fullName())
130131
);
131132
}
133+
134+
public function replaceTopics(string ...$topics): PromiseInterface
135+
{
136+
return $this->handleCommand(
137+
new ReplaceTopicsCommand($this->fullName(), ...$topics)
138+
);
139+
}
132140
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Tests\Github\CommandBus\Handler\Repository;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\Repository\ReplaceTopicsCommand;
6+
use ApiClients\Client\Github\CommandBus\Handler\Repository\ReplaceTopicsHandler;
7+
use ApiClients\Foundation\Transport\Service\RequestService;
8+
use ApiClients\Middleware\Json\JsonStream;
9+
use ApiClients\Tools\TestUtilities\TestCase;
10+
use RingCentral\Psr7\Request;
11+
use RingCentral\Psr7\Response;
12+
use function React\Promise\resolve;
13+
14+
final class ReplaceTopicsHandlerTest extends TestCase
15+
{
16+
public function testCommand()
17+
{
18+
$topics = [
19+
'foo',
20+
'bar',
21+
];
22+
$repository = 'repository';
23+
$request = new Request(
24+
'PUT',
25+
'repos/' . $repository . '/topics',
26+
[],
27+
new JsonStream([
28+
'names' => $topics,
29+
])
30+
);
31+
$response = new Response(
32+
200,
33+
[],
34+
new JsonStream([
35+
'names' => $topics,
36+
])
37+
);
38+
39+
$service = $this->prophesize(RequestService::class);
40+
$service->request($request)->shouldBeCalled()->willReturn(resolve($response));
41+
42+
$handler = new ReplaceTopicsHandler($service->reveal());
43+
$handler->handle(new ReplaceTopicsCommand($repository, ...$topics));
44+
}
45+
}

0 commit comments

Comments
 (0)