From b89d0c56b38630706a7a26b2f09b2682bbaa0fff Mon Sep 17 00:00:00 2001 From: "Alex. Plokhikh" <a.plohih@iqdev.digital> Date: Tue, 16 Apr 2024 02:06:52 +0500 Subject: [PATCH] add action request and controller --- src/Actions/HowDaysToNYAction.php | 20 ++++++++++++++++++++ src/Controller/HowDaysToNYController.php | 17 ++++++----------- src/Controller/SortPriceController.php | 10 ++-------- src/Repository/HowDaysToNYRepository.php | 19 ------------------- src/Requests/BeforeNYDateRequest.php | 19 +++++++++++++++++++ 5 files changed, 47 insertions(+), 38 deletions(-) create mode 100644 src/Actions/HowDaysToNYAction.php delete mode 100644 src/Repository/HowDaysToNYRepository.php create mode 100644 src/Requests/BeforeNYDateRequest.php diff --git a/src/Actions/HowDaysToNYAction.php b/src/Actions/HowDaysToNYAction.php new file mode 100644 index 0000000..3c133ed --- /dev/null +++ b/src/Actions/HowDaysToNYAction.php @@ -0,0 +1,20 @@ +<?php + +namespace App\Actions; + +use DateTimeImmutable; + +class HowDaysToNYAction +{ + /** + * Ð¤ÑƒÐ½ÐºÑ†Ð¸Ñ Ñ€Ð°ÑÑчитывает кол-во дней до нового года + * @param DateTime $date дата от которой, необходимо раÑÑчитать кол-во дней + * @return int + */ + public function act(DateTimeImmutable $date): int + { + $dateOfNY = $date->modify('first day of Jan +1 year'); + + return (int) $dateOfNY->diff($date)->format('%a'); + } +} \ No newline at end of file diff --git a/src/Controller/HowDaysToNYController.php b/src/Controller/HowDaysToNYController.php index 2166b8b..8311dce 100644 --- a/src/Controller/HowDaysToNYController.php +++ b/src/Controller/HowDaysToNYController.php @@ -2,29 +2,24 @@ namespace App\Controller; -use DateTimeImmutable; +use App\Actions\HowDaysToNYAction; +use App\Requests\BeforeNYDateRequest; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; -use function App\Repository\howDaysToNy; class HowDaysToNYController extends AbstractController { /** * Контролер раÑÑчитывает кол-во дней до нового года иÑÐ¿Ð¾Ð»ÑŒÐ·ÑƒÑŽÑ howDaysToNy - * @param Request $request + * @param BeforeNYDateRequest $request + * @param HowDaysToNYAction $action * @return Response - * @throws \Exception */ #[Route('/howdaystony', name: 'app_how_days_to_n_y')] - public function index(Request $request): Response + public function index(BeforeNYDateRequest $request, HowDaysToNYAction $action): Response { - $date = $request->toArray()['date']; - - $return = howDaysToNy(new DateTimeImmutable($date, null)); - - return new JsonResponse($return, Response::HTTP_OK); + return new JsonResponse($action->act($request->serialise())); } } diff --git a/src/Controller/SortPriceController.php b/src/Controller/SortPriceController.php index 313d3dc..0f1d250 100644 --- a/src/Controller/SortPriceController.php +++ b/src/Controller/SortPriceController.php @@ -3,28 +3,22 @@ namespace App\Controller; use App\Actions\SortPriceAction; -use App\Entity\PricesEntity; use App\Requests\PricesRequest; -use App\Service\ValidationService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; class SortPriceController extends AbstractController { /** * Контроллер волнÑет Ñортировку маÑÑива по убыванию цены иÑÐ¿Ð¾Ð»ÑŒÐ·ÑƒÑ sortPrice - * @param Request $request + * @param PricesRequest $request * @param SortPriceAction $action * @return JsonResponse */ #[Route('/sort/price', name: 'app_sort_price', methods: ['POST'])] public function index(PricesRequest $request, SortPriceAction $action): JsonResponse { - return new JsonResponse( - $action->act($request->serialise()), - 200 - ); + return new JsonResponse($action->act($request->serialise())); } } diff --git a/src/Repository/HowDaysToNYRepository.php b/src/Repository/HowDaysToNYRepository.php deleted file mode 100644 index 90ab47a..0000000 --- a/src/Repository/HowDaysToNYRepository.php +++ /dev/null @@ -1,19 +0,0 @@ -<?php -namespace App\Repository; - -use DateTime; -use DateTimeImmutable; - -/** - * Ð¤ÑƒÐ½ÐºÑ†Ð¸Ñ Ñ€Ð°ÑÑчитывает кол-во дней до нового года - * @param DateTimeImmutable $date дата от которой, необходимо раÑÑчитать кол-во дней - * @return int - */ -function howDaysToNy(DateTimeImmutable $date): int { - - $nextYear = (int)$date->format('Y') + 1; - $dateOfNY = (new DateTime()) - ->setDate($nextYear, 01, 01); - - return $dateOfNY->diff($date)->format('%a'); -} diff --git a/src/Requests/BeforeNYDateRequest.php b/src/Requests/BeforeNYDateRequest.php new file mode 100644 index 0000000..445bc20 --- /dev/null +++ b/src/Requests/BeforeNYDateRequest.php @@ -0,0 +1,19 @@ +<?php + +namespace App\Requests; + +use DateTimeImmutable; +use Symfony\Component\Validator\Constraints\Date; +use Symfony\Component\Validator\Constraints\Type; + +class BeforeNYDateRequest extends BaseRequest +{ + #[Date()] + #[Type('string')] + public $date; + + public function serialise(): mixed + { + return new DateTimeImmutable($this->date); + } +} -- GitLab