Passed
Push — orm-management ( 19e760...de38f8 )
by Laurent
01:44
created

DoctrineUserRepository::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the G.L.S.R. Apps package.
7
 *
8
 * (c) Dev-Int Création <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Administration\Infrastructure\Persistence\DoctrineOrm\Repositories;
15
16
use Administration\Domain\Protocol\Repository\UserRepositoryProtocol;
17
use Administration\Domain\User\Model\User as UserModel;
18
use Core\Infrastructure\Persistence\DoctrineOrm\Entities\User;
19
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
0 ignored issues
show
Bug introduced by
The type Doctrine\Bundle\Doctrine...ServiceEntityRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Doctrine\ORM\NonUniqueResultException;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\NonUniqueResultException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Doctrine\ORM\OptimisticLockException;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\OptimisticLockException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Doctrine\ORM\ORMException;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\ORMException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Doctrine\Persistence\ManagerRegistry;
0 ignored issues
show
Bug introduced by
The type Doctrine\Persistence\ManagerRegistry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
25
class DoctrineUserRepository extends ServiceEntityRepository implements UserRepositoryProtocol
26
{
27
    public function __construct(ManagerRegistry $registry)
28
    {
29
        parent::__construct($registry, User::class);
30
    }
31
32
    /**
33
     * @throws ORMException
34
     * @throws OptimisticLockException
35
     */
36
    final public function save(UserModel $user): void
37
    {
38
        $userEntity = User::fromModel($user);
39
        $this->getEntityManager()->persist($userEntity);
40
        $this->getEntityManager()->flush();
41
    }
42
43
    /**
44
     * @throws ORMException
45
     */
46
    final public function remove(UserModel $user): void
47
    {
48
        $userEntity = User::fromModel($user);
49
        $this->getEntityManager()->remove($userEntity);
50
    }
51
52
    /**
53
     * @throws NonUniqueResultException
54
     */
55
    final public function findOneByUuid(string $uuid): ?User
56
    {
57
        return $this->createQueryBuilder('u')
58
            ->where('u.uuid = :uuid')
59
            ->setParameter('uuid', $uuid)
60
            ->getQuery()
61
            ->getOneOrNullResult()
62
        ;
63
    }
64
65
    /**
66
     * @throws NonUniqueResultException
67
     */
68
    final public function existWithUsername(string $username): bool
69
    {
70
        $statement = $this->createQueryBuilder('u')
71
            ->select(['1'])
72
            ->where('u.username = :username')
73
            ->setParameter('username', $username)
74
            ->getQuery()
75
            ->getOneOrNullResult()
76
        ;
77
78
        return !(null === $statement);
79
    }
80
81
    /**
82
     * @throws NonUniqueResultException
83
     */
84
    final public function existWithEmail(string $email): bool
85
    {
86
        $statement = $this->createQueryBuilder('u')
87
            ->select(['1'])
88
            ->where('u.email = :email')
89
            ->setParameter('email', $email)
90
            ->getQuery()
91
            ->getOneOrNullResult()
92
        ;
93
94
        return !(null === $statement);
95
    }
96
}
97