|
@@ -1,10 +1,5 @@
|
|
|
<?php
|
|
|
-/**
|
|
|
- * Spiral Framework.
|
|
|
- *
|
|
|
- * @license MIT
|
|
|
- * @author Anton Titov (Wolfy-J)
|
|
|
- */
|
|
|
+
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
namespace App\Controller;
|
|
@@ -13,61 +8,32 @@ use App\Model\Fortune;
|
|
|
use App\Model\Repository\FortuneRepository;
|
|
|
use App\Model\Repository\WorldRepository;
|
|
|
use App\Model\World;
|
|
|
-use Cycle\ORM\Transaction;
|
|
|
+use Cycle\ORM\EntityManagerInterface;
|
|
|
use Nyholm\Psr7\Response;
|
|
|
+use Psr\Http\Message\ResponseInterface;
|
|
|
use Spiral\Core\Container\SingletonInterface;
|
|
|
-use Spiral\Views\ViewInterface;
|
|
|
use Spiral\Views\ViewsInterface;
|
|
|
|
|
|
final class BenchmarkController implements SingletonInterface
|
|
|
{
|
|
|
- /** @var ViewInterface */
|
|
|
- private $view;
|
|
|
-
|
|
|
- /** @var Transaction */
|
|
|
- private $transaction;
|
|
|
-
|
|
|
- /** @var FortuneRepository */
|
|
|
- private $fortunes;
|
|
|
-
|
|
|
- /** @var WorldRepository */
|
|
|
- private $worlds;
|
|
|
-
|
|
|
- /** @var Response */
|
|
|
- private $plain;
|
|
|
+ private ResponseInterface $plain;
|
|
|
|
|
|
- /**
|
|
|
- * @param ViewsInterface $views
|
|
|
- * @param Transaction $transaction
|
|
|
- * @param FortuneRepository $fortunes
|
|
|
- * @param WorldRepository $worlds
|
|
|
- */
|
|
|
public function __construct(
|
|
|
- ViewsInterface $views,
|
|
|
- Transaction $transaction,
|
|
|
- FortuneRepository $fortunes,
|
|
|
- WorldRepository $worlds
|
|
|
+ private readonly ViewsInterface $views,
|
|
|
+ private readonly EntityManagerInterface $entityManager,
|
|
|
+ private readonly FortuneRepository $fortunes,
|
|
|
+ private readonly WorldRepository $worlds
|
|
|
) {
|
|
|
- $this->view = $views->get('fortunes');
|
|
|
-
|
|
|
- $this->transaction = $transaction;
|
|
|
- $this->fortunes = $fortunes;
|
|
|
- $this->worlds = $worlds;
|
|
|
-
|
|
|
$this->plain = new Response(200, ['Content-Type' => 'text/plain', 'Server' => 'Spiral']);
|
|
|
$this->plain->getBody()->write('Hello, World!');
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public function json()
|
|
|
+ public function json(): array
|
|
|
{
|
|
|
return ['message' => 'Hello, World!'];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @return World|null
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
public function db(): ?World
|
|
@@ -76,11 +42,9 @@ final class BenchmarkController implements SingletonInterface
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param int $queries
|
|
|
- * @return array
|
|
|
* @throws \Exception
|
|
|
*/
|
|
|
- public function queries($queries = 1): array
|
|
|
+ public function queries(mixed $queries = null): array
|
|
|
{
|
|
|
$queries = $this->clamp($queries);
|
|
|
|
|
@@ -105,51 +69,41 @@ final class BenchmarkController implements SingletonInterface
|
|
|
|
|
|
$fortunes[] = $fortune;
|
|
|
|
|
|
- usort($fortunes, function ($left, $right) {
|
|
|
- return strcmp($left->message, $right->message);
|
|
|
- });
|
|
|
+ \usort($fortunes, static fn ($left, $right): int => \strcmp($left->message, $right->message));
|
|
|
|
|
|
- return $this->view->render(compact('fortunes'));
|
|
|
+ return $this->views->render('fortunes', ['fortunes' => $fortunes]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param int $queries
|
|
|
- * @return array
|
|
|
* @throws \Throwable
|
|
|
*/
|
|
|
- public function updates($queries = 1): array
|
|
|
+ public function updates(mixed $queries = null): array
|
|
|
{
|
|
|
$queries = $this->clamp($queries);
|
|
|
|
|
|
$worlds = [];
|
|
|
-
|
|
|
while ($queries--) {
|
|
|
$world = $this->worlds->findRandom();
|
|
|
- $world->randomNumber = random_int(1, 10000);
|
|
|
+ $world->randomNumber = \random_int(1, 10000);
|
|
|
|
|
|
- $this->transaction->persist($world)->run();
|
|
|
+ $this->entityManager->persist($world);
|
|
|
|
|
|
$worlds[] = $world;
|
|
|
}
|
|
|
|
|
|
+ $this->entityManager->run();
|
|
|
+
|
|
|
return $worlds;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @return Response
|
|
|
- */
|
|
|
- public function plaintext(): Response
|
|
|
+ public function plaintext(): ResponseInterface
|
|
|
{
|
|
|
return $this->plain;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @param $value
|
|
|
- * @return int
|
|
|
- */
|
|
|
- private function clamp($value): int
|
|
|
+ private function clamp(mixed $value): int
|
|
|
{
|
|
|
- if (!is_numeric($value) || $value < 1) {
|
|
|
+ if (!\is_numeric($value) || $value < 1) {
|
|
|
return 1;
|
|
|
}
|
|
|
|