From b22f361368d2d2e4dfa81e335ea0657446223efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 24 May 2022 23:04:24 +0200 Subject: [PATCH] fix cs --- .../MSGraphRemoteCalendarConnector.php | 4 +- .../Repository/CalendarRangeRepository.php | 115 +++++++++--------- 2 files changed, 61 insertions(+), 58 deletions(-) diff --git a/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraphRemoteCalendarConnector.php b/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraphRemoteCalendarConnector.php index caca3cecb..177005f48 100644 --- a/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraphRemoteCalendarConnector.php +++ b/src/Bundle/ChillCalendarBundle/RemoteCalendar/Connector/MSGraphRemoteCalendarConnector.php @@ -96,7 +96,7 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface ] )->toArray(); - $ids = array_map(function ($item) { return $item['id']; }, $bareEvents['value']); + $ids = array_map(static function ($item) { return $item['id']; }, $bareEvents['value']); $existingIdsInRange = $this->calendarRangeRepository->findRemoteIdsPresent($ids); return array_values( @@ -107,7 +107,7 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface // filter all event to keep only the one not in range array_filter( $bareEvents['value'], - function ($item) use ($existingIdsInRange) { + static function ($item) use ($existingIdsInRange) { return (!$existingIdsInRange[$item['id']]) ?? true; } ) diff --git a/src/Bundle/ChillCalendarBundle/Repository/CalendarRangeRepository.php b/src/Bundle/ChillCalendarBundle/Repository/CalendarRangeRepository.php index 70746073f..34dcf136d 100644 --- a/src/Bundle/ChillCalendarBundle/Repository/CalendarRangeRepository.php +++ b/src/Bundle/ChillCalendarBundle/Repository/CalendarRangeRepository.php @@ -13,26 +13,28 @@ namespace Chill\CalendarBundle\Repository; use Chill\CalendarBundle\Entity\CalendarRange; use Chill\MainBundle\Entity\User; +use DateTimeImmutable; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\Query\ResultSetMapping; use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ObjectRepository; +use function count; class CalendarRangeRepository implements ObjectRepository { - private EntityRepository $repository; - private EntityManagerInterface $em; + private EntityRepository $repository; + public function __construct(EntityManagerInterface $entityManager) { $this->em = $entityManager; $this->repository = $entityManager->getRepository(CalendarRange::class); } - public function countByAvailableRangesForUser(User $user, \DateTimeImmutable $from, \DateTimeImmutable $to): int + public function countByAvailableRangesForUser(User $user, DateTimeImmutable $from, DateTimeImmutable $to): int { return $this->buildQueryAvailableRangesForUser($user, $from, $to) ->select('COUNT(cr)') @@ -65,8 +67,8 @@ class CalendarRangeRepository implements ObjectRepository */ public function findByAvailableRangesForUser( User $user, - \DateTimeImmutable $from, - \DateTimeImmutable $to, + DateTimeImmutable $from, + DateTimeImmutable $to, ?int $limit = null, ?int $offset = null ): array { @@ -88,12 +90,63 @@ class CalendarRangeRepository implements ObjectRepository return $this->repository->findOneBy($criteria); } + /** + * Given a list of remote ids, return an array where + * keys are the remoteIds, and value is a boolean, true if the + * id is present in database. + * + * @param array|list $remoteIds + * + * @return array + */ + public function findRemoteIdsPresent(array $remoteIds): array + { + if (0 === count($remoteIds)) { + return []; + } + + $sql = 'SELECT + sq.remoteId as remoteid, + EXISTS (SELECT 1 FROM chill_calendar.calendar_range cr WHERE cr.remoteId = sq.remoteId) AS present + FROM + ( + VALUES %remoteIds% + ) AS sq(remoteId); + '; + + $remoteIdsStr = implode( + ', ', + array_fill(0, count($remoteIds), '((?))') + ); + + $rsm = new ResultSetMapping(); + $rsm + ->addScalarResult('remoteid', 'remoteId', Types::STRING) + ->addScalarResult('present', 'present', Types::BOOLEAN); + + $rows = $this->em + ->createNativeQuery( + strtr($sql, ['%remoteIds%' => $remoteIdsStr]), + $rsm + ) + ->setParameters(array_values($remoteIds)) + ->getResult(); + + $results = []; + + foreach ($rows as $r) { + $results[$r['remoteId']] = $r['present']; + } + + return $results; + } + public function getClassName(): string { return CalendarRange::class; } - private function buildQueryAvailableRangesForUser(User $user, \DateTimeImmutable $from, \DateTimeImmutable $to): QueryBuilder + private function buildQueryAvailableRangesForUser(User $user, DateTimeImmutable $from, DateTimeImmutable $to): QueryBuilder { $qb = $this->repository->createQueryBuilder('cr'); @@ -112,54 +165,4 @@ class CalendarRangeRepository implements ObjectRepository 'endDate' => $to, ]); } - - /** - * Given a list of remote ids, return an array where - * keys are the remoteIds, and value is a boolean, true if the - * id is present in database. - * - * @param array|list $remoteIds - * @return array - */ - public function findRemoteIdsPresent(array $remoteIds): array - { - if (0 === count($remoteIds)) { - return []; - } - - $sql = "SELECT - sq.remoteId as remoteid, - EXISTS (SELECT 1 FROM chill_calendar.calendar_range cr WHERE cr.remoteId = sq.remoteId) AS present - FROM - ( - VALUES %remoteIds% - ) AS sq(remoteId); - "; - - $remoteIdsStr = \implode( - ', ', - array_fill(0, count($remoteIds), '((?))') - ); - - $rsm = new ResultSetMapping(); - $rsm - ->addScalarResult('remoteid', 'remoteId', Types::STRING) - ->addScalarResult('present', 'present', Types::BOOLEAN); - - $rows = $this->em - ->createNativeQuery( - \strtr($sql, ['%remoteIds%' => $remoteIdsStr]), - $rsm - ) - ->setParameters(array_values($remoteIds)) - ->getResult(); - - $results = []; - - foreach ($rows as $r) { - $results[$r['remoteId']] = $r['present']; - } - - return $results; - } }