From 4e1f46154c176045916f70d3ab79adbe2259f042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 14 Dec 2023 23:05:18 +0100 Subject: [PATCH] Remove ReportSearch class and update services config Removed the ReportSearch class from ChillReportBundle and updated the corresponding services configuration. This removal is part of a larger refactoring process to enhance maintainability, eliminate unused code and simplify application architecture. This will not affect the application's functionality as the class was not used. --- .../ChillReportBundle/Search/ReportSearch.php | 152 ------------------ .../ChillReportBundle/config/services.yaml | 19 +-- 2 files changed, 4 insertions(+), 167 deletions(-) delete mode 100644 src/Bundle/ChillReportBundle/Search/ReportSearch.php diff --git a/src/Bundle/ChillReportBundle/Search/ReportSearch.php b/src/Bundle/ChillReportBundle/Search/ReportSearch.php deleted file mode 100644 index 4737edd99..000000000 --- a/src/Bundle/ChillReportBundle/Search/ReportSearch.php +++ /dev/null @@ -1,152 +0,0 @@ -getToken()->getUser() instanceof \Chill\MainBundle\Entity\User) { - throw new \RuntimeException('an user must be associated with token'); - } - $this->user = $tokenStorage->getToken()->getUser(); - } - - public function getOrder() - { - return 10000; - } - - public function isActiveByDefault() - { - return false; - } - - public function renderResult(array $terms, $start = 0, $limit = 50, array $options = [], $format = 'html') - { - return $this->container->get('templating')->render('@ChillReport/Search/results.html.twig', [ - 'reports' => $this->getReports($terms, $start, $limit), - 'total' => $this->count($terms), - 'pattern' => $this->recomposePattern($terms, ['date'], 'report'), - ]); - } - - public function supports($domain, $format = 'html') - { - return 'report' === $domain; - } - - private function addACL(QueryBuilder $qb) - { - // adding join - $qb->join('r.person', 'p'); - - $role = 'CHILL_REPORT_SEE'; - $reachableCenters = $this->helper->getReachableCenters($this->user, $role); - - $whereElement = $qb->expr()->orX(); - $i = 0; - - foreach ($reachableCenters as $center) { - $reachableScopesId = array_map( - static fn (Scope $scope) => $scope->getId(), - $this->helper->getReachableScopes($this->user, $role, $center) - ); - $whereElement->add( - $qb->expr()->andX( - $qb->expr()->eq('p.center', ':center_'.$i), - $qb->expr()->in('r.scope', ':reachable_scopes_'.$i) - ) - ); - $qb->setParameter('center_'.$i, $center); - $qb->setParameter('reachable_scopes_'.$i, $reachableScopesId); - } - - return $whereElement; - } - - /** - * @param array $terms the terms - * - * @return \Doctrine\ORM\QueryBuilder - */ - private function buildQuery(array $terms) - { - $query = $this->em->createQueryBuilder(); - - $query->from('ChillReportBundle:Report', 'r'); - - // throw a parsing exception if key 'date' and default is set - if (\array_key_exists('date', $terms) && '' !== $terms['_default']) { - throw new ParsingException('You may not set a date argument and a date in default'); - } - // throw a parsing exception if no argument except report - if (!\array_key_exists('date', $terms) && '' === $terms['_default']) { - throw new ParsingException('You must provide either a date:YYYY-mm-dd argument or a YYYY-mm-dd default search'); - } - - if (\array_key_exists('date', $terms)) { - $query->andWhere($query->expr()->eq('r.date', ':date')) - ->setParameter('date', $this->parseDate($terms['date'])); - } elseif (\array_key_exists('_default', $terms)) { - $query->andWhere($query->expr()->eq('r.date', ':date')) - ->setParameter('date', $this->parseDate($terms['_default'])); - } - - $query->andWhere($this->addACL($query)); - - return $query; - } - - private function count(array $terms) - { - $qb = $this->buildQuery($terms); - - $qb->select('COUNT(r.id)'); - - return $qb->getQuery()->getSingleScalarResult(); - } - - private function getReports(array $terms, $start, $limit) - { - $qb = $this->buildQuery($terms); - - $qb->select('r') - ->setMaxResults($limit) - ->setFirstResult($start) - ->orderBy('r.date', 'desc'); - - $reportQuery = $qb->getQuery(); - $reportQuery->setFetchMode(\Chill\ReportBundle\Entity\Report::class, 'person', \Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER); - - return $reportQuery->getResult(); - } -} diff --git a/src/Bundle/ChillReportBundle/config/services.yaml b/src/Bundle/ChillReportBundle/config/services.yaml index 477a94746..0fcb8dfb6 100644 --- a/src/Bundle/ChillReportBundle/config/services.yaml +++ b/src/Bundle/ChillReportBundle/config/services.yaml @@ -3,18 +3,7 @@ services: # class: Chill\ReportBundle\Example # arguments: [@service_id, "plain_value", %parameter%] services: - - chill.report.search: - class: Chill\ReportBundle\Search\ReportSearch - arguments: - - '@doctrine.orm.entity_manager' - - '@chill.main.security.authorization.helper' - - '@security.token_storage' - calls: - - [setContainer, ["@service_container"]] - tags: - - { name: chill.search, alias: 'report' } - + chill.report.timeline: class: Chill\ReportBundle\Timeline\TimelineReportProvider arguments: @@ -26,7 +15,7 @@ services: tags: - { name: chill.timeline, context: 'person' } - { name: chill.timeline, context: 'center' } - + chill.report.security.authorization.report_voter: class: Chill\ReportBundle\Security\Authorization\ReportVoter arguments: @@ -34,7 +23,7 @@ services: tags: - { name: security.voter } - { name: chill.role } - + chill.report.form.report_type: class: Chill\ReportBundle\Form\ReportType arguments: @@ -44,4 +33,4 @@ services: - "@doctrine.orm.entity_manager" tags: - { name: form.type, alias: chill_reportbundle_report } - +