diff --git a/examples/user-repository-replace-topics-async.php b/examples/user-repository-replace-topics-async.php new file mode 100644 index 0000000000..24f0afc7be --- /dev/null +++ b/examples/user-repository-replace-topics-async.php @@ -0,0 +1,32 @@ +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()); diff --git a/src/CommandBus/Command/Repository/ReplaceTopicsCommand.php b/src/CommandBus/Command/Repository/ReplaceTopicsCommand.php new file mode 100644 index 0000000000..8fab5e2a3b --- /dev/null +++ b/src/CommandBus/Command/Repository/ReplaceTopicsCommand.php @@ -0,0 +1,47 @@ +repository = $repository; + $this->topics = $topics; + } + + /** + * @return string + */ + public function getRepository(): string + { + return $this->repository; + } + + /** + * @return string[] + */ + public function getTopics(): array + { + return $this->topics; + } +} diff --git a/src/CommandBus/Handler/Repository/ReplaceTopicsHandler.php b/src/CommandBus/Handler/Repository/ReplaceTopicsHandler.php new file mode 100644 index 0000000000..cab5be31e6 --- /dev/null +++ b/src/CommandBus/Handler/Repository/ReplaceTopicsHandler.php @@ -0,0 +1,45 @@ +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']; + }); + } +} diff --git a/src/Resource/Async/Repository.php b/src/Resource/Async/Repository.php index 0f714b9740..6d1ec4f7b0 100644 --- a/src/Resource/Async/Repository.php +++ b/src/Resource/Async/Repository.php @@ -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; @@ -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) + ); + } } diff --git a/tests/CommandBus/Handler/Repository/ReplaceTopicsHandlerTest.php b/tests/CommandBus/Handler/Repository/ReplaceTopicsHandlerTest.php new file mode 100644 index 0000000000..e01decd709 --- /dev/null +++ b/tests/CommandBus/Handler/Repository/ReplaceTopicsHandlerTest.php @@ -0,0 +1,45 @@ + $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)); + } +}