Skip to content
Snippets Groups Projects
Commit e3c8eb91 authored by Camille Simiand's avatar Camille Simiand
Browse files

Refacto group all does not have to be saved in db

parent 6ee50d08
No related branches found
No related tags found
1 merge request!59tuleap-170-capsule-groups-should-be-own-by-user
...@@ -12,7 +12,6 @@ use App\Helper\StringHelper; ...@@ -12,7 +12,6 @@ use App\Helper\StringHelper;
use App\Repository\CapsuleRepository; use App\Repository\CapsuleRepository;
use App\Builder\CapsuleBuilder; use App\Builder\CapsuleBuilder;
use App\Form\CreateCapsuleFormType; use App\Form\CreateCapsuleFormType;
use App\Repository\GroupRepository;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface; use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
...@@ -29,18 +28,15 @@ class CapsuleController extends AbstractController ...@@ -29,18 +28,15 @@ class CapsuleController extends AbstractController
private CapsuleRepository $capsule_repository; private CapsuleRepository $capsule_repository;
private TranslatorInterface $translator; private TranslatorInterface $translator;
private EntityManagerInterface $entity_manager; private EntityManagerInterface $entity_manager;
private GroupRepository $group_repository;
public function __construct( public function __construct(
CapsuleRepository $capsule_repository, CapsuleRepository $capsule_repository,
TranslatorInterface $translator, TranslatorInterface $translator,
EntityManagerInterface $entity_manager, EntityManagerInterface $entity_manager
GroupRepository $group_repository
) { ) {
$this->capsule_repository = $capsule_repository; $this->capsule_repository = $capsule_repository;
$this->translator = $translator; $this->translator = $translator;
$this->entity_manager = $entity_manager; $this->entity_manager = $entity_manager;
$this->group_repository = $group_repository;
} }
/** /**
...@@ -59,7 +55,9 @@ class CapsuleController extends AbstractController ...@@ -59,7 +55,9 @@ class CapsuleController extends AbstractController
// get all capsules without any filter // get all capsules without any filter
$all_capsules = $current_user->getCapsules()->toArray(); $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); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
...@@ -69,8 +67,8 @@ class CapsuleController extends AbstractController ...@@ -69,8 +67,8 @@ class CapsuleController extends AbstractController
throw new \Exception('Group not found'); throw new \Exception('Group not found');
} }
if ($group->getName() !== $this->translator->trans('groups.filter.no_filter')) { if ($group->getId() !== Group::$GROUP_ALL_ID) {
$all_capsules = $group->getCapsulesIntersection($all_capsules); $all_capsules = $group->getCapsules()->toArray();
} }
} }
...@@ -325,33 +323,27 @@ class CapsuleController extends AbstractController ...@@ -325,33 +323,27 @@ class CapsuleController extends AbstractController
return $capsule; 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 private function createGroupAll(User $current_user): Group
{ {
$group_all = $this->group_repository->findOneBy( $group_all = new Group();
[ $group_all->setName($this->translator->trans('groups.filter.no_filter'));
'name' => $this->translator->trans('groups.filter.no_filter'), $group_all->setId(Group::$GROUP_ALL_ID);
'author' => $current_user->getId() $group_all->setAuthor($current_user);
]
);
if ($group_all instanceof Group) {
return $group_all; return $group_all;
} }
$group_all = new Group(); /**
$group_all->setId($group_all::$GROUP_ALL_ID); * @return array<Group>
$group_all->setAuthor($current_user); */
$group_all->setName($this->translator->trans('groups.filter.no_filter')); private function prependGroupAllToUserGroups(User $current_user): array
$this->entity_manager->persist($group_all); {
$this->entity_manager->flush(); $group_all = $this->createGroupAll($current_user);
return $group_all; $author_groups_without_group_all = $current_user->getGroups()->toArray();
$groups[] = $group_all;
$groups = array_merge($groups, $author_groups_without_group_all);
return $groups;
} }
} }
...@@ -54,15 +54,6 @@ class CapsuleGroupController extends AbstractController ...@@ -54,15 +54,6 @@ class CapsuleGroupController extends AbstractController
throw new \Exception("Capsule does not exist"); 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); $capsule_groups_by_author = $current_user->getGroupsByCapsule($capsule);
$author_capsule_groups_not_added = $capsule->getNotAddedGroupsByAuthor($current_user); $author_capsule_groups_not_added = $capsule->getNotAddedGroupsByAuthor($current_user);
......
...@@ -26,6 +26,7 @@ class GroupFixtures extends Fixture implements DependentFixtureInterface ...@@ -26,6 +26,7 @@ class GroupFixtures extends Fixture implements DependentFixtureInterface
throw new \Exception("User does not exist."); throw new \Exception("User does not exist.");
} }
//TODO: refacto with real group
$group_all = new Group(); $group_all = new Group();
$group_all->setName($this->translator->trans('groups.filter.no_filter')); $group_all->setName($this->translator->trans('groups.filter.no_filter'));
$group_all->setAuthor($group_author); $group_all->setAuthor($group_author);
......
...@@ -122,19 +122,4 @@ class Group ...@@ -122,19 +122,4 @@ class Group
return $this; 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();
}
);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment