mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-05 22:35:01 +00:00
Refactor WorkflowOnHoldController and improve tests
Refactored WorkflowOnHoldController to remove dependencies and improve redirect handling. Updated test cases to use PHPUnit instead of KernelTestCase and mock proper dependencies. Added relationship handling in EntityWorkflowStep to manage EntityWorkflowStepHold instances accurately.
This commit is contained in:
@@ -15,33 +15,29 @@ use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStepHold;
|
||||
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
|
||||
use Chill\MainBundle\Repository\Workflow\EntityWorkflowStepHoldRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
|
||||
class WorkflowOnHoldController extends AbstractController
|
||||
class WorkflowOnHoldController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
private readonly Security $security,
|
||||
private readonly Registry $registry,
|
||||
private readonly EntityWorkflowStepHoldRepository $entityWorkflowStepHoldRepository,
|
||||
private readonly EntityWorkflowRepository $entityWorkflowRepository,
|
||||
private readonly UrlGeneratorInterface $urlGenerator,
|
||||
) {}
|
||||
|
||||
#[Route(path: '/{_locale}/main/workflow/{id}/hold', name: 'chill_main_workflow_on_hold')]
|
||||
public function putOnHold(EntityWorkflow $entityWorkflow, Request $request): Response
|
||||
{
|
||||
$entityWorkflow = $this->entityWorkflowRepository->find($entityWorkflow);
|
||||
|
||||
$currentStep = $entityWorkflow->getCurrentStep();
|
||||
$currentUser = $this->security->getUser();
|
||||
|
||||
@@ -53,7 +49,7 @@ class WorkflowOnHoldController extends AbstractController
|
||||
$enabledTransitions = $workflow->getEnabledTransitions($entityWorkflow);
|
||||
|
||||
if (0 === count($enabledTransitions)) {
|
||||
throw $this->createAccessDeniedException('You are not allowed to apply any transitions to this workflow, therefore you cannot toggle the hold status.');
|
||||
throw new AccessDeniedHttpException('You are not allowed to apply any transitions to this workflow, therefore you cannot toggle the hold status.');
|
||||
}
|
||||
|
||||
$stepHold = new EntityWorkflowStepHold($currentStep, $currentUser);
|
||||
@@ -61,11 +57,16 @@ class WorkflowOnHoldController extends AbstractController
|
||||
$this->entityManager->persist($stepHold);
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('chill_main_workflow_show', ['id' => $entityWorkflow->getId()]);
|
||||
return new RedirectResponse(
|
||||
$this->urlGenerator->generate(
|
||||
'chill_main_workflow_show',
|
||||
['id' => $entityWorkflow->getId()]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[Route(path: '/{_locale}/main/workflow/{id}/remove_hold', name: 'chill_main_workflow_remove_hold')]
|
||||
public function removeOnHold(EntityWorkflowStep $entityWorkflowStep, Request $request): Response
|
||||
public function removeOnHold(EntityWorkflowStep $entityWorkflowStep): Response
|
||||
{
|
||||
$user = $this->security->getUser();
|
||||
|
||||
@@ -77,7 +78,7 @@ class WorkflowOnHoldController extends AbstractController
|
||||
throw new AccessDeniedHttpException('You are not allowed to remove workflow on hold');
|
||||
}
|
||||
|
||||
$hold = $this->entityWorkflowStepHoldRepository->findOneByStepAndUser($entityWorkflowStep, $user);
|
||||
$hold = $entityWorkflowStep->getHoldsOnStep()->findFirst(fn(int $index, EntityWorkflowStepHold $entityWorkflowStepHold) => $user === $entityWorkflowStepHold->getByUser());
|
||||
|
||||
if (null === $hold) {
|
||||
// this should not happens...
|
||||
@@ -87,6 +88,11 @@ class WorkflowOnHoldController extends AbstractController
|
||||
$this->entityManager->remove($hold);
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('chill_main_workflow_show', ['id' => $entityWorkflowStep->getEntityWorkflow()->getId()]);
|
||||
return new RedirectResponse(
|
||||
$this->urlGenerator->generate(
|
||||
'chill_main_workflow_show',
|
||||
['id' => $entityWorkflowStep->getEntityWorkflow()->getId()]
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user