From dd4adf37d2b335d1c805d8a9374877e2687371cf Mon Sep 17 00:00:00 2001 From: dannc Date: Wed, 30 Dec 2020 15:07:51 +0700 Subject: [PATCH] Run cached and simple services in one command. --- cli.php | 5 ++++- src/Command/JokeCommand.php | 16 +++++++++++++--- src/Operation/JokeOperation.php | 6 ++++++ src/Service/CachedJokes.php | 4 +++- src/Service/JokeCachedService.php | 14 ++++++++++++++ src/Service/JokeService.php | 17 ++++++++++++++--- 6 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 src/Service/JokeCachedService.php diff --git a/cli.php b/cli.php index 81bf5c7..79a879b 100644 --- a/cli.php +++ b/cli.php @@ -7,9 +7,12 @@ $jokeGuzzle = new \GuzzleHttp\Client([ ]); $cache = new \Sarahman\SimpleCache\FileSystemCache(__DIR__ . '/tmp/cache'); + $jokeService = new \App\Service\JokeService($jokeGuzzle); +$jokeCacheService = new \App\Service\JokeCachedService($jokeGuzzle); $jokeOperation = new \App\Operation\JokeOperation($jokeService, $cache); +$jokeOperationWithCache = new \App\Operation\JokeOperation($jokeCacheService, $cache); $app = new \Ahc\Cli\Application('Joke App', 'v0.0.1'); -$app->add(new \App\Command\JokeCommand($jokeOperation)); +$app->add(new \App\Command\JokeCommand($jokeOperation, $jokeOperationWithCache)); $app->handle($_SERVER['argv']); diff --git a/src/Command/JokeCommand.php b/src/Command/JokeCommand.php index 59b610b..f15bc8d 100644 --- a/src/Command/JokeCommand.php +++ b/src/Command/JokeCommand.php @@ -5,11 +5,13 @@ declare(strict_types=1); namespace App\Command; use App\Operation\JokeOperation; +use Psr\SimpleCache\InvalidArgumentException; class JokeCommand extends \Ahc\Cli\Input\Command { public function __construct( - private JokeOperation $jokeOperation + private JokeOperation $jokeOperation, + private JokeOperation $jokeOperationWithCache, ) { parent::__construct('joke', 'Get some joke'); } @@ -17,7 +19,15 @@ class JokeCommand extends \Ahc\Cli\Input\Command public function execute(): void { $io = $this->app()->io(); - $joke = $this->jokeOperation->getRandomJoke(); - $io->green($joke, true); + + try { + $joke = $this->jokeOperation->getRandomJoke(); + $io->green("Joke: {$joke}", true); + + $cachedJoke = $this->jokeOperationWithCache->getRandomJoke(); + $io->green("Cached joke: {$cachedJoke}", true); + } catch (\Throwable | InvalidArgumentException $exception) { + $io->error($exception->getMessage(), true); + } } } diff --git a/src/Operation/JokeOperation.php b/src/Operation/JokeOperation.php index 40f8301..7f8f193 100644 --- a/src/Operation/JokeOperation.php +++ b/src/Operation/JokeOperation.php @@ -7,6 +7,7 @@ namespace App\Operation; use App\Service\CachedJokes; use App\Service\JokeInterface; use Psr\SimpleCache\CacheInterface; +use Psr\SimpleCache\InvalidArgumentException; class JokeOperation { @@ -16,6 +17,11 @@ class JokeOperation ) { } + /** + * Get text of random joke + * @return string + * @throws InvalidArgumentException + */ public function getRandomJoke(): string { $reflection = new \ReflectionClass($this->jokeService); diff --git a/src/Service/CachedJokes.php b/src/Service/CachedJokes.php index 35406cd..a79683d 100644 --- a/src/Service/CachedJokes.php +++ b/src/Service/CachedJokes.php @@ -4,7 +4,9 @@ declare(strict_types=1); namespace App\Service; -#[\Attribute] +use Psr\SimpleCache\CacheInterface; + +#[\Attribute(\Attribute::TARGET_METHOD)] class CachedJokes { public function getCacheKey(string $jokeType): string diff --git a/src/Service/JokeCachedService.php b/src/Service/JokeCachedService.php new file mode 100644 index 0000000..13a70c6 --- /dev/null +++ b/src/Service/JokeCachedService.php @@ -0,0 +1,14 @@ +sendRequest(static::CATEGORY_ANY); @@ -25,11 +31,16 @@ class JokeService implements JokeInterface return \json_decode($responseContent, true); } + /** + * @param string ...$categories + * @return ResponseInterface + * @throws GuzzleException + */ protected function sendRequest(string ...$categories): ResponseInterface { $categoriesStr = implode(',', $categories); return $this->guzzleClient->get("/joke/{$categoriesStr}", [ - \GuzzleHttp\RequestOptions::QUERY => [ + RequestOptions::QUERY => [ 'format' => 'json', 'amount' => 1, 'type' => 'single',