]> Raphaƫl G. Git Repositories - blogbundle/blobdiff - Repository/UserRepository.php
Version 0.0.2
[blogbundle] / Repository / UserRepository.php
index befe69528cd941e3e2cd0bf2909d2c0271893ceb..0a385bf8914a9ae5d8d4c4231c7d33dc14b9ab7c 100644 (file)
@@ -12,6 +12,9 @@
 namespace Rapsys\BlogBundle\Repository;
 
 use Doctrine\ORM\Query\ResultSetMapping;
+
+use Rapsys\BlogBundle\Repository;
+
 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
 use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
@@ -19,7 +22,7 @@ use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
 /**
  * UserRepository
  */
-class UserRepository extends EntityRepository implements PasswordUpgraderInterface {
+class UserRepository extends Repository implements PasswordUpgraderInterface {
        /**
         * Find user count as int
         *
@@ -45,7 +48,7 @@ SQL;
                $rsm->addScalarResult('count', 'count', 'integer');
 
                //Get result
-               return $this->_em
+               return $this->manager
                        ->createNativeQuery($req, $rsm)
                        ->getSingleScalarResult();
        }
@@ -151,9 +154,9 @@ SQL;
                        ->addScalarResult('surname', 'surname', 'string')
                        ->addScalarResult('pseudonym', 'pseudonym', 'string')
                        ->addScalarResult('slug', 'slug', 'string')
-                       ->addScalarResult('created', 'created', 'datetime')
-                       ->addScalarResult('updated', 'updated', 'datetime')
-                       ->addScalarResult('modified', 'modified', 'datetime')
+                       ->addScalarResult('created', 'created', 'datetime_immutable')
+                       ->addScalarResult('updated', 'updated', 'datetime_immutable')
+                       ->addScalarResult('modified', 'modified', 'datetime_immutable')
                        ->addScalarResult('civility', 'civility', 'string')
                        //XXX: is a string because of \n separator
                        ->addScalarResult('g_ids', 'g_ids', 'string')
@@ -175,7 +178,7 @@ SQL;
                        ->addScalarResult('kt_titles', 'kt_titles', 'string');
 
                //Fetch result
-               $res = $this->_em
+               $res = $this->manager
                        ->createNativeQuery($req, $rsm)
                        ->setParameter('offset', $page * $count)
                        ->setParameter('count', $count)
@@ -197,8 +200,8 @@ SQL;
                                'modified' => $data['modified'],
                                'civility' => $data['civility'],
                                #'slug' => $data['slug'],
-                               'link' => $this->router->generate('rapsys_blog_user_view', ['id' => $data['id'], 'slug' => $data['slug']]),
-                               'edit' => $this->router->generate('rapsys_user_edit', ['mail' => $short = $this->slugger->short($data['mail']), 'hash' => $this->slugger->hash($short)]),
+                               'link' => $this->router->generate('rapsysblog_user_view', ['id' => $data['id'], 'slug' => $data['slug']]),
+                               'edit' => $this->router->generate('rapsysuser_edit', ['mail' => $short = $this->slugger->short($data['mail']), 'hash' => $this->slugger->hash($short)]),
                                'articles' => [],
                                'groups' => []
                        ];
@@ -214,7 +217,7 @@ SQL;
                                        $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)])
+                                               #'link' => $this->router->generate('rapsysuser_group_view', ['id' => $id, 'slug' => $this->slugger->short($group)])
                                        ];
                                }
                        }
@@ -246,7 +249,7 @@ SQL;
                                                        'description' => $descriptions[$k],
                                                        #'slug' => $slugs[$k],
                                                        'title' => $titles[$k],
-                                                       'link' => $this->router->generate('rapsys_blog_article_view', ['id' => $id, 'slug' => $slugs[$k]]),
+                                                       'link' => $this->router->generate('rapsysblog_article_view', ['id' => $id, 'slug' => $slugs[$k]]),
                                                        //TODO: replace with keywords !!!
                                                        'keywords' => []
                                                ];
@@ -265,7 +268,7 @@ SQL;
                                                                $ret[$data['id']]['articles'][$id]['keywords'][$kid] = [
                                                                        #'slug' => $slugs[$k],
                                                                        'title' => $titles[$k],
-                                                                       'link' => $this->router->generate('rapsys_blog_keyword_view', ['id' => $kid, 'slug' => $slugs[$k]]),
+                                                                       'link' => $this->router->generate('rapsysblog_keyword_view', ['id' => $kid, 'slug' => $slugs[$k]]),
                                                                ];
                                                        }
                                                }
@@ -282,9 +285,9 @@ SQL;
         * Find user by id as array
         *
         * @param integer $id The user id
-        * @return array The article array
+        * @return ?array The article array
         */
-       public function findByIdAsArray(int $id): array {
+       public function findByIdAsArray(int $id): ?array {
                //Set the request
                $req = <<<SQL
 SELECT
@@ -377,9 +380,9 @@ SQL;
                        ->addScalarResult('surname', 'surname', 'string')
                        ->addScalarResult('pseudonym', 'pseudonym', 'string')
                        ->addScalarResult('slug', 'slug', 'string')
-                       ->addScalarResult('created', 'created', 'datetime')
-                       ->addScalarResult('updated', 'updated', 'datetime')
-                       ->addScalarResult('modified', 'modified', 'datetime')
+                       ->addScalarResult('created', 'created', 'datetime_immutable')
+                       ->addScalarResult('updated', 'updated', 'datetime_immutable')
+                       ->addScalarResult('modified', 'modified', 'datetime_immutable')
                        ->addScalarResult('civility', 'civility', 'string')
                        //XXX: is a string because of \n separator
                        ->addScalarResult('g_ids', 'g_ids', 'string')
@@ -401,10 +404,13 @@ SQL;
                        ->addScalarResult('kt_titles', 'kt_titles', 'string');
 
                //Get data
-               $data = $this->_em
+               if (($data = $this->manager
                        ->createNativeQuery($req, $rsm)
                        ->setParameter('id', $id)
-                       ->getOneOrNullResult();
+                       ->getOneOrNullResult()) === null) {
+                       //Return null
+                       return $data;
+               }
 
                //Set return
                $return = [
@@ -418,8 +424,8 @@ SQL;
                        'modified' => $data['modified'],
                        'civility' => $data['civility'],
                        'slug' => $data['slug'],
-                       'link' => $this->router->generate('rapsys_blog_user_view', ['id' => $data['id'], 'slug' => $data['slug']]),
-                       'edit' => $this->router->generate('rapsys_user_edit', ['mail' => $short = $this->slugger->short($data['mail']), 'hash' => $this->slugger->hash($short)]),
+                       'link' => $this->router->generate('rapsysblog_user_view', ['id' => $data['id'], 'slug' => $data['slug']]),
+                       'edit' => $this->router->generate('rapsysuser_edit', ['mail' => $short = $this->slugger->short($data['mail']), 'hash' => $this->slugger->hash($short)]),
                        'articles' => [],
                        'groups' => []
                ];
@@ -435,7 +441,7 @@ SQL;
                                $return['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)])
+                                       #'link' => $this->router->generate('rapsysuser_group_view', ['id' => $id, 'slug' => $this->slugger->short($group)])
                                ];
                        }
                }
@@ -467,7 +473,7 @@ SQL;
                                                'description' => $descriptions[$k],
                                                #'slug' => $slugs[$k],
                                                'title' => $titles[$k],
-                                               'link' => $this->router->generate('rapsys_blog_article_view', ['id' => $id, 'slug' => $slugs[$k]]),
+                                               'link' => $this->router->generate('rapsysblog_article_view', ['id' => $id, 'slug' => $slugs[$k]]),
                                                //TODO: replace with keywords !!!
                                                'keywords' => []
                                        ];
@@ -486,7 +492,7 @@ SQL;
                                                        $return['articles'][$id]['keywords'][$kid] = [
                                                                #'slug' => $slugs[$k],
                                                                'title' => $titles[$k],
-                                                               'link' => $this->router->generate('rapsys_blog_keyword_view', ['id' => $kid, 'slug' => $slugs[$k]]),
+                                                               'link' => $this->router->generate('rapsysblog_keyword_view', ['id' => $kid, 'slug' => $slugs[$k]]),
                                                        ];
                                                }
                                        }