]> Raphaël G. Git Repositories - userbundle/commitdiff
Add entity and user repositories
authorRaphaël Gertz <git@rapsys.eu>
Mon, 11 Dec 2023 02:36:21 +0000 (03:36 +0100)
committerRaphaël Gertz <git@rapsys.eu>
Mon, 11 Dec 2023 02:36:21 +0000 (03:36 +0100)
Repository/EntityRepository.php [new file with mode: 0644]
Repository/UserRepository.php [new file with mode: 0644]

diff --git a/Repository/EntityRepository.php b/Repository/EntityRepository.php
new file mode 100644 (file)
index 0000000..db1921c
--- /dev/null
@@ -0,0 +1,149 @@
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys UserBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Rapsys\UserBundle\Repository;
+
+use Doctrine\ORM\EntityManagerInterface;
+use Doctrine\ORM\EntityRepository as BaseEntityRepository;
+use Doctrine\ORM\Mapping\ClassMetadata;
+use Symfony\Component\Routing\RouterInterface;
+use Symfony\Contracts\Translation\TranslatorInterface;
+
+use Rapsys\PackBundle\Util\SluggerUtil;
+
+/**
+ * EntityRepository
+ *
+ * {@inheritdoc}
+ */
+class EntityRepository extends BaseEntityRepository {
+       /**
+        * The RouterInterface instance
+        *
+        * @var RouterInterface
+        */
+       protected RouterInterface $router;
+
+       /**
+        * The SluggerUtil instance
+        *
+        * @var SluggerUtil
+        */
+       protected SluggerUtil $slugger;
+
+       /**
+        * The table keys array
+        *
+        * @var array
+        */
+       protected array $tableKeys;
+
+       /**
+        * The table values array
+        *
+        * @var array
+        */
+       protected array $tableValues;
+
+       /**
+        * The TranslatorInterface instance
+        *
+        * @var TranslatorInterface
+        */
+       protected TranslatorInterface $translator;
+
+       /**
+        * The list of languages
+        *
+        * @var string[]
+        */
+       protected array $languages = [];
+
+       /**
+        * The current locale
+        *
+        * @var string
+        */
+       protected string $locale;
+
+       /**
+        * Initializes a new LocationRepository instance
+        *
+        * @param EntityManagerInterface $manager The EntityManagerInterface instance
+        * @param ClassMetadata $class The ClassMetadata instance
+        * @param RouterInterface $router The router instance
+        * @param SluggerUtil $slugger The SluggerUtil instance
+        * @param TranslatorInterface $translator The TranslatorInterface instance
+        * @param array $languages The languages list
+        * @param string $locale The current locale
+        */
+       public function __construct(EntityManagerInterface $manager, ClassMetadata $class, RouterInterface $router, SluggerUtil $slugger, TranslatorInterface $translator, array $languages, string $locale) {
+               //Call parent constructor
+               parent::__construct($manager, $class);
+
+               //Set languages
+               $this->languages = $languages;
+
+               //Set locale
+               $this->locale = $locale;
+
+               //Set router
+               $this->router = $router;
+
+               //Set slugger
+               $this->slugger = $slugger;
+
+               //Set translator
+               $this->translator = $translator;
+
+               //Get quote strategy
+               $qs = $manager->getConfiguration()->getQuoteStrategy();
+               $dp = $manager->getConnection()->getDatabasePlatform();
+
+               //Set quoted table names
+               //XXX: this allow to make this code table name independent
+               //XXX: remember to place longer prefix before shorter to avoid strange replacings
+               $tables = [
+                       //Set entities
+                       'RapsysUserBundle:UserGroup' => $qs->getJoinTableName($manager->getClassMetadata('Rapsys\UserBundle\Entity\User')->getAssociationMapping('groups'), $manager->getClassMetadata('Rapsys\UserBundle\Entity\User'), $dp),
+                       'RapsysUserBundle:Civility' => $qs->getTableName($manager->getClassMetadata('Rapsys\UserBundle\Entity\Civility'), $dp),
+                       'RapsysUserBundle:Group' => $qs->getTableName($manager->getClassMetadata('Rapsys\UserBundle\Entity\Group'), $dp),
+                       'RapsysUserBundle:User' => $qs->getTableName($manager->getClassMetadata('Rapsys\UserBundle\Entity\User'), $dp),
+                       //Set locale
+                       //XXX: or $manager->getConnection()->quote($this->locale) ???
+                       ':locale' => $dp->quoteStringLiteral($this->locale),
+                       //Set limit
+                       //XXX: Set limit used to workaround mariadb subselect optimization
+                       ':limit' => PHP_INT_MAX,
+                       //Set cleanup
+                       "\t" => '',
+                       "\r" => ' ',
+                       "\n" => ' '
+               ];
+
+               //Set quoted table name keys
+               $this->tableKeys = array_keys($tables);
+
+               //Set quoted table name values
+               $this->tableValues = array_values($tables);
+       }
+
+       /**
+        * Get replaced query
+        *
+        * @param string $req The request to replace
+        * @return string The replaced request
+        */
+       protected function replace(string $req): string {
+               //Replace bundle entity name by table name
+               return str_replace($this->tableKeys, $this->tableValues, $req);
+       }
+}
diff --git a/Repository/UserRepository.php b/Repository/UserRepository.php
new file mode 100644 (file)
index 0000000..d75e229
--- /dev/null
@@ -0,0 +1,145 @@
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys UserBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Rapsys\UserBundle\Repository;
+
+use Doctrine\ORM\Query\ResultSetMapping;
+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
+
+/**
+ * UserRepository
+ */
+class UserRepository extends EntityRepository {
+       /**
+        * Find user count as int
+        *
+        * @return integer The keywords count
+        */
+       public function findCountAsInt(): int {
+               //Set the request
+               $req = <<<SQL
+SELECT COUNT(u.id) AS count
+FROM RapsysUserBundle:User AS u
+SQL;
+
+               //Get result set mapping instance
+               $req = $this->replace($req);
+
+               //Get result set mapping instance
+               //XXX: DEBUG: see ../blog.orig/src/Rapsys/BlogBundle/Repository/ArticleRepository.php
+               $rsm = new ResultSetMapping();
+
+               //Declare all fields
+               //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
+               //addScalarResult($sqlColName, $resColName, $type = 'string');
+               $rsm->addScalarResult('count', 'count', 'integer');
+
+               //Get result
+               return $this->_em
+                       ->createNativeQuery($req, $rsm)
+                       ->getSingleScalarResult();
+       }
+
+       /**
+        * Find all users as array
+        *
+        * @param integer $page The page
+        * @param integer $count The count
+        * @return array The users sorted by id
+        */
+       public function findAllAsArray(int $page, int $count): array {
+               //Set the request
+               $req = <<<SQL
+SELECT
+       u.id,
+       u.mail,
+       u.forename,
+       u.surname,
+       CONCAT_WS(" ", u.forename, u.surname) AS pseudonym,
+       c.id AS c_id,
+       c.title AS c_title,
+       GROUP_CONCAT(g.id ORDER BY g.id SEPARATOR "\\n") AS g_ids,
+       GROUP_CONCAT(g.title ORDER BY g.id SEPARATOR "\\n") AS g_titles
+FROM RapsysUserBundle:User AS u
+JOIN RapsysUserBundle:UserGroup AS gu ON (gu.user_id = u.id)
+JOIN RapsysUserBundle:Group AS g ON (g.id = gu.group_id)
+JOIN RapsysUserBundle:Civility AS c ON (c.id = u.civility_id)
+GROUP BY u.id
+ORDER BY u.id ASC
+LIMIT :offset, :count
+SQL;
+
+               //Replace bundle entity name by table name
+               $req = $this->replace($req);
+
+               //Get result set mapping instance
+               //XXX: DEBUG: see ../blog.orig/src/Rapsys/UserBundle/Repository/ArticleRepository.php
+               $rsm = new ResultSetMapping();
+
+               //Declare all fields
+               //XXX: see vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Types.php
+               //addScalarResult($sqlColName, $resColName, $type = 'string');
+               $rsm->addScalarResult('id', 'id', 'integer')
+                       ->addScalarResult('mail', 'mail', 'string')
+                       ->addScalarResult('forename', 'forename', 'string')
+                       ->addScalarResult('surname', 'surname', 'string')
+                       ->addScalarResult('pseudonym', 'pseudonym', 'string')
+                       ->addScalarResult('c_id', 'c_id', 'integer')
+                       ->addScalarResult('c_title', 'c_title', 'string')
+                       //XXX: is a string because of \n separator
+                       ->addScalarResult('g_ids', 'g_ids', 'string')
+                       //XXX: is a string because of \n separator
+                       ->addScalarResult('g_titles', 'g_titles', 'string');
+
+               //Fetch result
+               $res = $this->_em
+                       ->createNativeQuery($req, $rsm)
+                       ->setParameter('offset', $page * $count)
+                       ->setParameter('count', $count)
+                       ->getResult();
+
+               //Init return
+               $ret = [];
+
+               //Process result
+               foreach($res as $data) {
+                       //Set data
+                       $ret[$data['id']] = [
+                               'mail' => $data['mail'],
+                               'forename' => $data['forename'],
+                               'surname' => $data['surname'],
+                               'pseudonym' => $data['pseudonym'],
+                               'groups' => [],
+                               'slug' => $this->slugger->slug($data['pseudonym']),
+                               'link' => $this->router->generate('rapsys_user_edit', ['mail' => $short = $this->slugger->short($data['mail']), 'hash' => $this->slugger->hash($short)])
+                       ];
+
+                       //With groups
+                       if (!empty($data['g_ids'])) {
+                               //Set titles
+                               $titles = explode("\n", $data['g_titles']);
+
+                               //Iterate on each group
+                               foreach(explode("\n", $data['g_ids']) as $k => $id) {
+                                       //Add group
+                                       $ret[$data['id']]['groups'][$id] = [
+                                               'title' => $group = $this->translator->trans($titles[$k]),
+                                               #'slug' => $this->slugger->slug($group)
+                                               #'link' => $this->router->generate('rapsys_user_group_view', ['id' => $id, 'slug' => $this->slugger->short($group)])
+                                       ];
+                               }
+                       }
+               }
+
+               //Send result
+               return $ret;
+       }
+}