mirror of
https://github.com/Dannecron/test-php8-attributes.git
synced 2025-12-25 13:22:34 +03:00
Run cached and simple services in one command.
This commit is contained in:
5
cli.php
5
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']);
|
||||
|
||||
@@ -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();
|
||||
|
||||
try {
|
||||
$joke = $this->jokeOperation->getRandomJoke();
|
||||
$io->green($joke, true);
|
||||
$io->green("Joke: {$joke}", true);
|
||||
|
||||
$cachedJoke = $this->jokeOperationWithCache->getRandomJoke();
|
||||
$io->green("Cached joke: {$cachedJoke}", true);
|
||||
} catch (\Throwable | InvalidArgumentException $exception) {
|
||||
$io->error($exception->getMessage(), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
14
src/Service/JokeCachedService.php
Normal file
14
src/Service/JokeCachedService.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
class JokeCachedService extends JokeService implements JokeInterface
|
||||
{
|
||||
#[CachedJokes]
|
||||
public function getRandomJoke(): array
|
||||
{
|
||||
return parent::getRandomJoke();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
@@ -13,11 +16,14 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class JokeService implements JokeInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected \GuzzleHttp\Client $guzzleClient
|
||||
protected Client $guzzleClient
|
||||
) {
|
||||
}
|
||||
|
||||
#[CachedJokes]
|
||||
/**
|
||||
* @return array
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function getRandomJoke(): array
|
||||
{
|
||||
$response = $this->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',
|
||||
|
||||
Reference in New Issue
Block a user