From 2937ba8b95a874da32d3e424533e4a2ba9f3c613 Mon Sep 17 00:00:00 2001
From: "Alex. Plokhikh" <a.plohih@iqdev.digital>
Date: Mon, 15 Apr 2024 16:52:39 +0500
Subject: [PATCH] create controller and action menu prepare and req

---
 src/Actions/PrepareMenuAction.php        | 35 ++++++++++++++++++++++++
 src/Controller/PrepareMenuController.php | 18 ++++++------
 src/Repository/PrepareMenuRepository.php | 29 --------------------
 src/Requests/MenuRequest.php             | 32 ++++++++++++++++++++++
 4 files changed, 76 insertions(+), 38 deletions(-)
 create mode 100644 src/Actions/PrepareMenuAction.php
 delete mode 100644 src/Repository/PrepareMenuRepository.php
 create mode 100644 src/Requests/MenuRequest.php

diff --git a/src/Actions/PrepareMenuAction.php b/src/Actions/PrepareMenuAction.php
new file mode 100644
index 0000000..6c81983
--- /dev/null
+++ b/src/Actions/PrepareMenuAction.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace App\Actions;
+
+class PrepareMenuAction
+{
+    /**
+     * Группирует подразедлы в верхние разделы меню
+     * Дочерние элементы помещает в массив родителя с ключом submenu
+     * Значение под ключом depth определяет уровень раздела
+     * Массив $aMenu всегда должен начинается с элемента depth = 0,
+     * все последующие элементы с depth = 1 являются его дочерними элементами
+     *
+     * @param array $menu
+     * @return array
+     */
+    public function act(array $menu): array
+    {
+        $returnableAMenu = [];
+        foreach ($menu as $menuPoint) {
+
+            $depth = $menuPoint["depth"];
+
+            if ($depth === 1) {
+                $tempArray = array_pop($returnableAMenu);
+                $tempArray['submenu'][] = $menuPoint;
+                $returnableAMenu[] = $tempArray;
+            } else {
+                $returnableAMenu[] = $menuPoint;
+            }
+        }
+
+        return $returnableAMenu;
+    }
+}
diff --git a/src/Controller/PrepareMenuController.php b/src/Controller/PrepareMenuController.php
index fb46687..1e3334f 100644
--- a/src/Controller/PrepareMenuController.php
+++ b/src/Controller/PrepareMenuController.php
@@ -2,28 +2,28 @@
 
 namespace App\Controller;
 
+use App\Actions\PrepareMenuAction;
+use App\Requests\MenuRequest;
 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\prepareMenu;
 
 class PrepareMenuController extends AbstractController
 {
     /**
      * Контроллер реструктурирует массив меню добавляя вложенность по значению depth
      * используюя prepareMenu функцию
-     * @param Request $request
+     * @param MenuRequest $request
+     * @param PrepareMenuAction $action
      * @return Response
      */
     #[Route('/prepare/menu', name: 'app_prepare_menu', methods: ['POST'])]
-    public function index(Request $request): Response
+    public function index(MenuRequest $request, PrepareMenuAction $action): Response
     {
-        $array = $request->toArray();
-
-        $return = prepareMenu($array);
-
-        return new JsonResponse($return, Response::HTTP_OK);
+        return new JsonResponse(
+            $action->act($request->serialise()),
+            200
+        );
     }
 }
diff --git a/src/Repository/PrepareMenuRepository.php b/src/Repository/PrepareMenuRepository.php
deleted file mode 100644
index e1be3c7..0000000
--- a/src/Repository/PrepareMenuRepository.php
+++ /dev/null
@@ -1,29 +0,0 @@
-<?php
-
-namespace App\Repository;
-/**
- * Сгруппировать подразедлы в верхние разделы меню
- * Дочерние элементы поместить в массив родителя с ключом submenu
- * Значение под ключом depth определяет уровень раздела
- * Массив $aMenu всегда начинается с элемента depth = 0, все последующие элементы с depth = 1 являются его дочерними
- * элементами
- * @param array $aMenu
- * @return array
- */
-function prepareMenu(array $aMenu): array {
-    $returnableAMenu = [];
-    foreach ($aMenu as $menuPoint){
-        $depth = $menuPoint["depth"];
-        if ($depth === 1){
-            $tempArray = array_pop($returnableAMenu);
-            $tempArray['submenu'][] = $menuPoint;
-            $returnableAMenu[] = $tempArray;
-        }
-        else{
-            $returnableAMenu[] = $menuPoint;
-        }
-
-    }
-
-    return $returnableAMenu;
-}
diff --git a/src/Requests/MenuRequest.php b/src/Requests/MenuRequest.php
new file mode 100644
index 0000000..99bf783
--- /dev/null
+++ b/src/Requests/MenuRequest.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Requests;
+
+use Symfony\Component\Validator\Constraints\All;
+use Symfony\Component\Validator\Constraints\Collection;
+use Symfony\Component\Validator\Constraints\NotBlank;
+use Symfony\Component\Validator\Constraints\Type;
+
+class MenuRequest extends BaseRequest
+{
+
+    #[Type('array')]
+    #[All(
+        constraints: new Collection(fields:[
+            'name' => [
+                new NotBlank(),
+                new Type('string'),
+            ],
+            'depth' => [
+                new NotBlank(),
+                new Type('integer'),
+            ]
+        ])
+    )]
+    public $menu;
+
+    public function serialise(): mixed
+    {
+        return $this->menu;
+    }
+}
-- 
GitLab