updateStatusByIdentityIdToForgotten()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 12
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\ApiBundle\Identity\Repository;
20
21
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
22
use Doctrine\ORM\Query;
23
use Doctrine\Persistence\ManagerRegistry;
24
use Surfnet\Stepup\Identity\Value\CommonName;
25
use Surfnet\Stepup\Identity\Value\Email;
26
use Surfnet\Stepup\Identity\Value\IdentityId;
27
use Surfnet\Stepup\Identity\Value\Institution;
28
use Surfnet\Stepup\Identity\Value\NameId;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity;
30
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\IdentityQuery;
31
32
/**
33
 * @extends ServiceEntityRepository<Identity>
34
 */
35
class IdentityRepository extends ServiceEntityRepository
36
{
37
    public function __construct(
38
        ManagerRegistry $registry,
39
    ) {
40
        parent::__construct($registry, Identity::class);
41
    }
42
43
    public function find(mixed $id, $lockMode = null, $lockVersion = null): ?Identity
44
    {
45
        /** @var Identity|null $identity */
46
        $identity = parent::find($id);
47
48
        return $identity;
49
    }
50
51
    public function save(Identity $identity): void
52
    {
53
        $entityManager = $this->getEntityManager();
54
        $entityManager->persist($identity);
55
        $entityManager->flush();
56
    }
57
58
    /**
59
     * @return Query
60
     */
61
    public function createSearchQuery(
62
        IdentityQuery $query,
63
    ): Query {
64
        $queryBuilder = $this->createQueryBuilder('i');
65
66
        if ($query->institution) {
67
            $queryBuilder
68
                ->andWhere('i.institution = :institution')
69
                ->setParameter('institution', $query->institution);
70
        }
71
72
        if ($query->nameId) {
73
            $queryBuilder
74
                ->andWhere('i.nameId = :nameId')
75
                ->setParameter('nameId', $query->nameId);
76
        }
77
78
        if ($query->email) {
79
            $queryBuilder
80
                ->andWhere('i.email LIKE :email')
81
                ->setParameter('email', sprintf('%%%s%%', $query->email));
82
        }
83
84
        if ($query->commonName) {
85
            $queryBuilder
86
                ->andWhere('i.commonName LIKE :commonName')
87
                ->setParameter('commonName', sprintf('%%%s%%', $query->commonName));
88
        }
89
90
        return $queryBuilder->getQuery();
91
    }
92
93
    /**
94
     * @param string[] $nameIds
95
     * @return Identity[] Indexed by NameID.
96
     */
97
    public function findByNameIdsIndexed(array $nameIds): array
98
    {
99
        return $this->getEntityManager()->createQueryBuilder()
100
            ->select('i')
101
            ->from(Identity::class, 'i', 'i.nameId')
102
            ->where('i.nameId IN (:nameIds)')
103
            ->setParameter('nameIds', $nameIds)
104
            ->getQuery()
105
            ->getResult();
106
    }
107
108
    /**
109
     *
110
     * @return bool
111
     */
112
    public function hasIdentityWithNameIdAndInstitution(NameId $nameId, Institution $institution): bool
113
    {
114
        $identityCount = $this->createQueryBuilder('i')
115
            ->select('COUNT(i.id)')
116
            ->where('i.nameId = :nameId')
117
            ->andWhere('i.institution = :institution')
118
            ->setParameter('nameId', $nameId->getNameId())
119
            ->setParameter('institution', $institution->getInstitution())
120
            ->getQuery()
121
            ->getSingleScalarResult();
122
123
        return $identityCount > 0;
124
    }
125
126
    /**
127
     * @return Identity
128
     */
129
    public function findOneByNameIdAndInstitution(NameId $nameId, Institution $institution): Identity
130
    {
131
        return $this->createQueryBuilder('i')
132
            ->where('i.nameId = :nameId')
133
            ->setParameter('nameId', $nameId->getNameId())
134
            ->andWhere('i.institution = :institution')
135
            ->setParameter('institution', $institution->getInstitution())
136
            ->getQuery()
137
            ->getSingleResult();
138
    }
139
140
    public function findOneByNameId(string $nameId): ?Identity
141
    {
142
        return $this->findOneBy(['nameId' => $nameId]);
143
    }
144
145
    public function updateStatusByIdentityIdToForgotten(IdentityId $identityId): void
146
    {
147
        $this->getEntityManager()->createQueryBuilder()
148
            ->update($this->getEntityName(), 'i')
149
            ->set('i.commonName', ":name")
150
            ->set('i.email', ":email")
151
            ->where('i.id = :id')
152
            ->setParameter('id', $identityId->getIdentityId())
153
            ->setParameter('name', CommonName::unknown())
154
            ->setParameter('email', Email::unknown())
155
            ->getQuery()
156
            ->execute();
157
    }
158
}
159