From a786578cbe07c6039a4daeef77a223dc0df53352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sun, 23 Jan 2022 21:52:27 +0100 Subject: [PATCH] fix cs --- .../AccompanyingCourseWorkApiController.php | 45 ++++++----- .../Form/CreationPersonType.php | 6 +- .../AccompanyingPeriodWorkRepository.php | 79 +++++++++---------- 3 files changed, 64 insertions(+), 66 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkApiController.php b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkApiController.php index 96b3438dc..7f3caa675 100644 --- a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkApiController.php @@ -14,6 +14,8 @@ namespace Chill\PersonBundle\Controller; use Chill\MainBundle\CRUD\Controller\ApiController; use Chill\MainBundle\Serializer\Model\Collection; use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository; +use DateInterval; +use DateTimeImmutable; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; @@ -27,6 +29,26 @@ class AccompanyingCourseWorkApiController extends ApiController $this->accompanyingPeriodWorkRepository = $accompanyingPeriodWorkRepository; } + /** + * @Route("/api/1.0/person/accompanying-period/work/my-near-end") + */ + public function myWorksNearEndDate(Request $request): JsonResponse + { + $since = (new DateTimeImmutable('now')) + ->sub(new DateInterval('P' . $request->query->getInt('since', 15) . 'D')); + $until = (new DateTimeImmutable('now')) + ->add(new DateInterval('P' . $request->query->getInt('since', 15) . 'D')); + $total = $this->accompanyingPeriodWorkRepository + ->countNearEndDateByUser($this->getUser(), $since, $until); + $paginator = $this->getPaginatorFactory()->create($total); + $works = $this->accompanyingPeriodWorkRepository + ->findNearEndDateByUser($this->getUser(), $since, $until, $paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber()); + + $collection = new Collection($works, $paginator); + + return $this->json($collection, 200, [], ['groups' => ['read']]); + } + protected function getContextForSerialization(string $action, Request $request, string $_format, $entity): array { switch ($action) { @@ -39,27 +61,4 @@ class AccompanyingCourseWorkApiController extends ApiController return parent::getContextForSerialization($action, $request, $_format, $entity); } - - /** - * @Route("/api/1.0/person/accompanying-period/work/my-near-end") - * - * @param Request $request - * @return JsonResponse - */ - public function myWorksNearEndDate(Request $request): JsonResponse - { - $since = (new \DateTimeImmutable('now')) - ->sub(new \DateInterval('P'.$request->query->getInt('since', 15).'D')); - $until = (new \DateTimeImmutable('now')) - ->add(new \DateInterval('P'.$request->query->getInt('since', 15).'D')); - $total = $this->accompanyingPeriodWorkRepository - ->countNearEndDateByUser($this->getUser(), $since, $until); - $paginator = $this->getPaginatorFactory()->create($total); - $works = $this->accompanyingPeriodWorkRepository - ->findNearEndDateByUser($this->getUser(), $since, $until, $paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber()); - - $collection = new Collection($works, $paginator); - - return $this->json($collection, 200, [], ['groups' => ['read']]); - } } diff --git a/src/Bundle/ChillPersonBundle/Form/CreationPersonType.php b/src/Bundle/ChillPersonBundle/Form/CreationPersonType.php index 82437eb30..a33858c02 100644 --- a/src/Bundle/ChillPersonBundle/Form/CreationPersonType.php +++ b/src/Bundle/ChillPersonBundle/Form/CreationPersonType.php @@ -61,13 +61,13 @@ final class CreationPersonType extends AbstractType 'required' => false, ]) ->add('phonenumber', TelType::class, [ - 'required' => false + 'required' => false, ]) ->add('mobilenumber', TelType::class, [ - 'required' => false + 'required' => false, ]) ->add('email', EmailType::class, [ - 'required' => false + 'required' => false, ]); if ($this->askCenters) { diff --git a/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriod/AccompanyingPeriodWorkRepository.php b/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriod/AccompanyingPeriodWorkRepository.php index b7ea9459e..60ad31de0 100644 --- a/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriod/AccompanyingPeriodWorkRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriod/AccompanyingPeriodWorkRepository.php @@ -14,6 +14,7 @@ namespace Chill\PersonBundle\Repository\AccompanyingPeriod; use Chill\MainBundle\Entity\User; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork; +use DateTimeImmutable; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; @@ -28,6 +29,28 @@ final class AccompanyingPeriodWorkRepository implements ObjectRepository $this->repository = $entityManager->getRepository(AccompanyingPeriodWork::class); } + public function buildQueryNearEndDateByUser(User $user, DateTimeImmutable $since, DateTimeImmutable $until): QueryBuilder + { + $qb = $this->repository->createQueryBuilder('w'); + + $qb + ->join('w.accompanyingPeriod', 'period') + ->where( + $qb->expr()->andX( + $qb->expr()->eq('period.user', ':user'), + $qb->expr()->gte('w.endDate', ':since'), + $qb->expr()->lte('w.startDate', ':until') + ) + ) + ->setParameters([ + 'user' => $user, + 'since' => $since, + 'until' => $until, + ]); + + return $qb; + } + public function countByAccompanyingPeriod(AccompanyingPeriod $period): int { return $this->repository->countByAccompanyingPeriod($period); @@ -43,6 +66,12 @@ final class AccompanyingPeriodWorkRepository implements ObjectRepository ->getSingleScalarResult(); } + public function countNearEndDateByUser(User $user, DateTimeImmutable $since, DateTimeImmutable $until): int + { + return $this->buildQueryNearEndDateByUser($user, $since, $until) + ->select('count(w)')->getQuery()->getSingleScalarResult(); + } + public function find($id): ?AccompanyingPeriodWork { return $this->repository->find($id); @@ -70,6 +99,16 @@ final class AccompanyingPeriodWorkRepository implements ObjectRepository return $this->repository->findByAccompanyingPeriod($period, $orderBy, $limit, $offset); } + public function findNearEndDateByUser(User $user, DateTimeImmutable $since, DateTimeImmutable $until, int $limit = 20, int $offset = 0): array + { + return $this->buildQueryNearEndDateByUser($user, $since, $until) + ->select('w') + ->setFirstResult($offset) + ->setMaxResults($limit) + ->getQuery() + ->getResult(); + } + public function findOneBy(array $criteria): ?AccompanyingPeriodWork { return $this->repository->findOneBy($criteria); @@ -113,44 +152,4 @@ final class AccompanyingPeriodWorkRepository implements ObjectRepository return $qb; } - - public function findNearEndDateByUser(User $user, \DateTimeImmutable $since, \DateTimeImmutable $until, int $limit = 20, int $offset = 0): array - { - return $this->buildQueryNearEndDateByUser($user, $since, $until) - ->select('w') - ->setFirstResult($offset) - ->setMaxResults($limit) - ->getQuery() - ->getResult(); - } - - public function countNearEndDateByUser(User $user, \DateTimeImmutable $since, \DateTimeImmutable $until): int - { - return $this->buildQueryNearEndDateByUser($user, $since, $until) - ->select('count(w)')->getQuery()->getSingleScalarResult(); - } - - public function buildQueryNearEndDateByUser(User $user, \DateTimeImmutable $since, \DateTimeImmutable $until): QueryBuilder - { - $qb = $this->repository->createQueryBuilder('w'); - - $qb - ->join('w.accompanyingPeriod', 'period') - ->where( - $qb->expr()->andX( - $qb->expr()->eq('period.user', ':user'), - $qb->expr()->gte('w.endDate', ':since'), - $qb->expr()->lte('w.startDate', ':until') - ) - ) - ->setParameters([ - 'user' => $user, - 'since' => $since, - 'until' => $until - ]); - - return $qb; - } - - }