Completed
Pull Request — develop (#256)
by
unknown
02:26
created

IdentityRepository::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Common\Collections\ArrayCollection;
22
use Doctrine\ORM\EntityManager;
23
use Doctrine\ORM\EntityRepository;
24
use Doctrine\ORM\Mapping;
25
use Surfnet\Stepup\Identity\Value\IdentityId;
26
use Surfnet\Stepup\Identity\Value\Institution;
27
use Surfnet\Stepup\Identity\Value\NameId;
28
use Surfnet\StepupMiddleware\ApiBundle\Authorization\Filter\InstitutionAuthorizationRepositoryFilter;
29
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity;
30
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\IdentityQuery;
31
32
class IdentityRepository extends EntityRepository
33
{
34
    /**
35
     * @var InstitutionAuthorizationRepositoryFilter
36
     */
37
    private $authorizationRepositoryFilter;
38
39
    public function __construct(
40
        EntityManager $em,
41
        Mapping\ClassMetadata $class,
42
        InstitutionAuthorizationRepositoryFilter $authorizationRepositoryFilter
43
    ) {
44
        parent::__construct($em, $class);
45
        $this->authorizationRepositoryFilter = $authorizationRepositoryFilter;
46
    }
47
48
    /**
49
     * @param string $id
50
     * @return Identity|null
51
     */
52
    public function find($id)
53
    {
54
        /** @var Identity|null $identity */
55
        $identity = parent::find($id);
56
57
        return $identity;
58
    }
59
60
    /**
61
     * @param Identity $identity
62
     */
63
    public function save(Identity $identity)
64
    {
65
        $entityManager = $this->getEntityManager();
66
        $entityManager->persist($identity);
67
        $entityManager->flush();
68
    }
69
70
    /**
71
     * @param IdentityQuery $query
72
     * @return \Doctrine\ORM\Query
73
     */
74
    public function createSearchQuery(
75
        IdentityQuery $query
76
    ) {
77
        $queryBuilder = $this->createQueryBuilder('i');
78
79
        // If no institution context is provided, we are not able to query with authorization context
80
        if ($query->authorizationContext) {
81
            // Modify query to filter on authorization
82
            $this->authorizationRepositoryFilter->filter($queryBuilder, $query->authorizationContext, 'i.id', 'i.institution', 'iac');
83
        }
84
85
        if ($query->institution) {
86
            $queryBuilder
87
                ->andWhere('i.institution = :institution')
88
                ->setParameter('institution', $query->institution);
89
        }
90
91
        if ($query->nameId) {
92
            $queryBuilder
93
                ->andWhere('i.nameId = :nameId')
94
                ->setParameter('nameId', $query->nameId);
95
        }
96
97
        if ($query->email) {
98
            $queryBuilder
99
                ->andWhere('MATCH_AGAINST(i.email, :email) > 0')
100
                ->setParameter('email', $query->email);
101
        }
102
103
        if ($query->commonName) {
104
            $queryBuilder
105
                ->andWhere('MATCH_AGAINST(i.commonName, :commonName) > 0')
106
                ->setParameter('commonName', $query->commonName);
107
        }
108
109
        return $queryBuilder->getQuery();
110
    }
111
112
    /**
113
     * @param string[] $nameIds
114
     * @return Identity[] Indexed by NameID.
115
     */
116
    public function findByNameIdsIndexed(array $nameIds)
117
    {
118
        return $this->getEntityManager()->createQueryBuilder()
119
            ->select('i')
120
            ->from('Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity', 'i', 'i.nameId')
121
            ->where('i.nameId IN (:nameIds)')
122
            ->setParameter('nameIds', $nameIds)
123
            ->getQuery()
124
            ->getResult();
125
    }
126
127
    /**
128
     * @param NameId      $nameId
129
     * @param Institution $institution
130
     *
131
     * @return bool
132
     */
133
    public function hasIdentityWithNameIdAndInstitution(NameId $nameId, Institution $institution)
134
    {
135
        $identityCount = $this->createQueryBuilder('i')
136
            ->select('COUNT(i.id)')
137
            ->where('i.nameId = :nameId')
138
            ->andWhere('i.institution = :institution')
139
            ->setParameter('nameId', $nameId->getNameId())
140
            ->setParameter('institution', $institution->getInstitution())
141
            ->getQuery()
142
            ->getSingleScalarResult();
143
144
        return $identityCount > 0;
145
    }
146
147
    /**
148
     * @param NameId      $nameId
149
     * @param Institution $institution
150
     * @return Identity
151
     */
152
    public function findOneByNameIdAndInstitution(NameId $nameId, Institution $institution)
153
    {
154
        return $this->createQueryBuilder('i')
155
                ->where('i.nameId = :nameId')
156
                ->setParameter('nameId', $nameId->getNameId())
157
                ->andWhere('i.institution = :institution')
158
                ->setParameter('institution', $institution->getInstitution())
159
                ->getQuery()
160
                ->getSingleResult();
161
    }
162
163
    public function removeByIdentityId(IdentityId $identityId)
164
    {
165
        $this->getEntityManager()->createQueryBuilder()
166
            ->delete($this->_entityName, 'i')
167
            ->where('i.id = :identityId')
168
            ->setParameter('identityId', $identityId->getIdentityId())
169
            ->getQuery()
170
            ->execute();
171
    }
172
173
    /**
174
     * @param Institution $institution
175
     * @return ArrayCollection|Identity[]
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
176
     */
177
    public function findByInstitution(Institution $institution)
178
    {
179
        return $this->createQueryBuilder('i')
180
            ->where('i.institution = :institution')
181
            ->setParameter('institution', $institution->getInstitution())
182
            ->getQuery()
183
            ->getResult();
184
    }
185
}
186