From e3c8eb91030b24a95dde65b6e097abf10b3b5a5e Mon Sep 17 00:00:00 2001
From: Camille Simiand <camille.simiand@tetras-libre.fr>
Date: Fri, 18 Feb 2022 10:20:22 +0100
Subject: [PATCH] Refacto group all does not have to be saved in db

---
 src/Controller/CapsuleController.php      | 52 ++++++++++-------------
 src/Controller/CapsuleGroupController.php |  9 ----
 src/DataFixtures/GroupFixtures.php        |  1 +
 src/Entity/Group.php                      | 15 -------
 4 files changed, 23 insertions(+), 54 deletions(-)

diff --git a/src/Controller/CapsuleController.php b/src/Controller/CapsuleController.php
index d713918..fa48abb 100755
--- a/src/Controller/CapsuleController.php
+++ b/src/Controller/CapsuleController.php
@@ -12,7 +12,6 @@ use App\Helper\StringHelper;
 use App\Repository\CapsuleRepository;
 use App\Builder\CapsuleBuilder;
 use App\Form\CreateCapsuleFormType;
-use App\Repository\GroupRepository;
 use Doctrine\ORM\EntityManagerInterface;
 use Knp\Component\Pager\PaginatorInterface;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -29,18 +28,15 @@ class CapsuleController extends AbstractController
     private CapsuleRepository $capsule_repository;
     private TranslatorInterface $translator;
     private EntityManagerInterface $entity_manager;
-    private GroupRepository $group_repository;
 
     public function __construct(
         CapsuleRepository $capsule_repository,
         TranslatorInterface $translator,
-        EntityManagerInterface $entity_manager,
-        GroupRepository $group_repository
+        EntityManagerInterface $entity_manager
     ) {
         $this->capsule_repository = $capsule_repository;
         $this->translator = $translator;
         $this->entity_manager = $entity_manager;
-        $this->group_repository = $group_repository;
     }
 
     /**
@@ -59,7 +55,9 @@ class CapsuleController extends AbstractController
 
         // get all capsules without any filter
         $all_capsules = $current_user->getCapsules()->toArray();
-        $form = $this->createGroupFilterForm($current_user);
+        $groups = $this->prependGroupAllToUserGroups($current_user);
+
+        $form = $this->createForm(FilterByGroupFormType::class, ['groups' => $groups]);
         $form->handleRequest($request);
 
         if ($form->isSubmitted() && $form->isValid()) {
@@ -69,8 +67,8 @@ class CapsuleController extends AbstractController
                 throw new \Exception('Group not found');
             }
 
-            if ($group->getName() !== $this->translator->trans('groups.filter.no_filter')) {
-                $all_capsules = $group->getCapsulesIntersection($all_capsules);
+            if ($group->getId() !== Group::$GROUP_ALL_ID) {
+                $all_capsules = $group->getCapsules()->toArray();
             }
         }
 
@@ -325,33 +323,27 @@ class CapsuleController extends AbstractController
         return $capsule;
     }
 
-    private function createGroupFilterForm(User $current_user): FormInterface
-    {
-        $this->createGroupAll($current_user);
-        $groups = $current_user->getGroups()->toArray();
-        return $this->createForm(FilterByGroupFormType::class, ['groups' => $groups]);
-    }
-
     private function createGroupAll(User $current_user): Group
     {
-        $group_all = $this->group_repository->findOneBy(
-            [
-                'name' => $this->translator->trans('groups.filter.no_filter'),
-                'author' => $current_user->getId()
-            ]
-        );
-
-        if ($group_all instanceof Group) {
-            return $group_all;
-        }
-
         $group_all = new Group();
-        $group_all->setId($group_all::$GROUP_ALL_ID);
-        $group_all->setAuthor($current_user);
         $group_all->setName($this->translator->trans('groups.filter.no_filter'));
-        $this->entity_manager->persist($group_all);
-        $this->entity_manager->flush();
+        $group_all->setId(Group::$GROUP_ALL_ID);
+        $group_all->setAuthor($current_user);
 
         return $group_all;
     }
+
+    /**
+     * @return array<Group>
+     */
+    private function prependGroupAllToUserGroups(User $current_user): array
+    {
+        $group_all = $this->createGroupAll($current_user);
+
+        $author_groups_without_group_all = $current_user->getGroups()->toArray();
+        $groups[] = $group_all;
+        $groups = array_merge($groups, $author_groups_without_group_all);
+
+        return $groups;
+    }
 }
diff --git a/src/Controller/CapsuleGroupController.php b/src/Controller/CapsuleGroupController.php
index 798e905..d488c5d 100755
--- a/src/Controller/CapsuleGroupController.php
+++ b/src/Controller/CapsuleGroupController.php
@@ -54,15 +54,6 @@ class CapsuleGroupController extends AbstractController
             throw new \Exception("Capsule does not exist");
         }
 
-        $group_all = $this->group_repository->findOneBy(
-            [
-                'name' => $this->translator->trans('groups.filter.no_filter'),
-                'author' => $current_user->getId()
-            ]
-        );
-
-        $current_user->getGroups()->removeElement($group_all);
-
         $capsule_groups_by_author = $current_user->getGroupsByCapsule($capsule);
         $author_capsule_groups_not_added = $capsule->getNotAddedGroupsByAuthor($current_user);
 
diff --git a/src/DataFixtures/GroupFixtures.php b/src/DataFixtures/GroupFixtures.php
index 11ef8ba..129f6ba 100644
--- a/src/DataFixtures/GroupFixtures.php
+++ b/src/DataFixtures/GroupFixtures.php
@@ -26,6 +26,7 @@ class GroupFixtures extends Fixture implements DependentFixtureInterface
             throw new \Exception("User does not exist.");
         }
 
+        //TODO: refacto with real group
         $group_all = new Group();
         $group_all->setName($this->translator->trans('groups.filter.no_filter'));
         $group_all->setAuthor($group_author);
diff --git a/src/Entity/Group.php b/src/Entity/Group.php
index 50bde46..64b3e07 100755
--- a/src/Entity/Group.php
+++ b/src/Entity/Group.php
@@ -122,19 +122,4 @@ class Group
 
         return $this;
     }
-
-    /**
-     * @param array<Capsule> $capsules
-     * @return array<Capsule>
-     */
-    public function getCapsulesIntersection(array $capsules): array
-    {
-        return array_uintersect(
-            $capsules,
-            $this->getCapsules()->toArray(),
-            function ($obj_a, $obj_b) {
-                return $obj_a->getId() - $obj_b->getId();
-            }
-        );
-    }
 }
-- 
GitLab