mirror of
https://github.com/Dannecron/test-php8-attributes.git
synced 2025-12-25 21:32:35 +03:00
initial commit
This commit is contained in:
26
src/Command/JokeCommand.php
Normal file
26
src/Command/JokeCommand.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Operation\JokeOperation;
|
||||
|
||||
class JokeCommand extends \Ahc\Cli\Input\Command
|
||||
{
|
||||
private JokeOperation $jokeOperation;
|
||||
|
||||
public function __construct(JokeOperation $jokeOperation)
|
||||
{
|
||||
parent::__construct('joke', 'Get some joke');
|
||||
|
||||
$this->jokeOperation = $jokeOperation;
|
||||
}
|
||||
|
||||
public function execute(): void
|
||||
{
|
||||
$io = $this->app()->io();
|
||||
$joke = $this->jokeOperation->getRandomJoke();
|
||||
$io->green($joke, true);
|
||||
}
|
||||
}
|
||||
43
src/Operation/JokeOperation.php
Normal file
43
src/Operation/JokeOperation.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Operation;
|
||||
|
||||
use App\Service\CachedJokes;
|
||||
use App\Service\JokeInterface;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
|
||||
class JokeOperation
|
||||
{
|
||||
protected JokeInterface $jokeService;
|
||||
protected CacheInterface $cache;
|
||||
|
||||
public function __construct(JokeInterface $jokeService, CacheInterface $cache)
|
||||
{
|
||||
$this->jokeService = $jokeService;
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
public function getRandomJoke(): string
|
||||
{
|
||||
$reflection = new \ReflectionClass($this->jokeService);
|
||||
$cachedJokesAttr = $reflection->getMethod('getRandomJoke')->getAttributes(CachedJokes::class);
|
||||
if (count($cachedJokesAttr) > 0) {
|
||||
/** @var CachedJokes $cache */
|
||||
$cache = $cachedJokesAttr[0]->newInstance();
|
||||
$cacheKey = $cache->getCacheKey(JokeInterface::CATEGORY_ANY);
|
||||
if ($this->cache->has($cacheKey)) {
|
||||
return $this->cache->get($cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
$randomJoke = $this->jokeService->getRandomJoke();
|
||||
$randomJokeStr = $randomJoke['joke'];
|
||||
if (isset($cacheKey)) {
|
||||
$this->cache->set($cacheKey, $randomJokeStr);
|
||||
}
|
||||
|
||||
return $randomJokeStr;
|
||||
}
|
||||
}
|
||||
14
src/Service/CachedJokes.php
Normal file
14
src/Service/CachedJokes.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
#[\Attribute]
|
||||
class CachedJokes
|
||||
{
|
||||
public function getCacheKey(string $jokeType): string
|
||||
{
|
||||
return "jokes-{$jokeType}";
|
||||
}
|
||||
}
|
||||
18
src/Service/JokeInterface.php
Normal file
18
src/Service/JokeInterface.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
interface JokeInterface
|
||||
{
|
||||
public const CATEGORY_ANY = 'Any';
|
||||
public const CATEGORY_MISC = 'Miscellaneous';
|
||||
public const CATEGORY_PROGRAMMING = 'Programming';
|
||||
public const CATEGORY_DARK = 'Dark';
|
||||
public const CATEGORY_PUN = 'Pun';
|
||||
public const CATEGORY_SPOOKY = 'Spooky';
|
||||
public const CATEGORY_CHRISTMAS = 'Christmas';
|
||||
|
||||
public function getRandomJoke(): array;
|
||||
}
|
||||
41
src/Service/JokeService.php
Normal file
41
src/Service/JokeService.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* Class JokeService
|
||||
* @link https://sv443.net/jokeapi/v2
|
||||
*/
|
||||
class JokeService implements JokeInterface
|
||||
{
|
||||
protected \GuzzleHttp\Client $guzzleClient;
|
||||
|
||||
public function __construct(\GuzzleHttp\Client $guzzleClient)
|
||||
{
|
||||
$this->guzzleClient = $guzzleClient;
|
||||
}
|
||||
|
||||
#[CachedJokes]
|
||||
public function getRandomJoke(): array
|
||||
{
|
||||
$response = $this->sendRequest(static::CATEGORY_ANY);
|
||||
$responseContent = (string) $response->getBody();
|
||||
return \json_decode($responseContent, true);
|
||||
}
|
||||
|
||||
protected function sendRequest(string ...$categories): ResponseInterface
|
||||
{
|
||||
$categoriesStr = implode(',', $categories);
|
||||
return $this->guzzleClient->get("/joke/{$categoriesStr}", [
|
||||
\GuzzleHttp\RequestOptions::QUERY => [
|
||||
'format' => 'json',
|
||||
'amount' => 1,
|
||||
'type' => 'single',
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user