Completed
Push — develop ( 800b48...35e2e2 )
by Michiel
13s
created

hasIdentityWithNameIdAndInstitution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 ($query->institution) {
80
            $queryBuilder
81
                ->andWhere('i.institution = :institution')
82
                ->setParameter('institution', $query->institution);
83
        }
84
85
        if ($query->nameId) {
86
            $queryBuilder
87
                ->andWhere('i.nameId = :nameId')
88
                ->setParameter('nameId', $query->nameId);
89
        }
90
91
        if ($query->email) {
92
            $queryBuilder
93
                ->andWhere('MATCH_AGAINST(i.email, :email) > 0')
94
                ->setParameter('email', $query->email);
95
        }
96
97
        if ($query->commonName) {
98
            $queryBuilder
99
                ->andWhere('MATCH_AGAINST(i.commonName, :commonName) > 0')
100
                ->setParameter('commonName', $query->commonName);
101
        }
102
103
        return $queryBuilder->getQuery();
104
    }
105
106
    /**
107
     * @param string[] $nameIds
108
     * @return Identity[] Indexed by NameID.
109
     */
110
    public function findByNameIdsIndexed(array $nameIds)
111
    {
112
        return $this->getEntityManager()->createQueryBuilder()
113
            ->select('i')
114
            ->from('Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity', 'i', 'i.nameId')
115
            ->where('i.nameId IN (:nameIds)')
116
            ->setParameter('nameIds', $nameIds)
117
            ->getQuery()
118
            ->getResult();
119
    }
120
121
    /**
122
     * @param NameId      $nameId
123
     * @param Institution $institution
124
     *
125
     * @return bool
126
     */
127
    public function hasIdentityWithNameIdAndInstitution(NameId $nameId, Institution $institution)
128
    {
129
        $identityCount = $this->createQueryBuilder('i')
130
            ->select('COUNT(i.id)')
131
            ->where('i.nameId = :nameId')
132
            ->andWhere('i.institution = :institution')
133
            ->setParameter('nameId', $nameId->getNameId())
134
            ->setParameter('institution', $institution->getInstitution())
135
            ->getQuery()
136
            ->getSingleScalarResult();
137
138
        return $identityCount > 0;
139
    }
140
141
    /**
142
     * @param NameId      $nameId
143
     * @param Institution $institution
144
     * @return Identity
145
     */
146
    public function findOneByNameIdAndInstitution(NameId $nameId, Institution $institution)
147
    {
148
        return $this->createQueryBuilder('i')
149
                ->where('i.nameId = :nameId')
150
                ->setParameter('nameId', $nameId->getNameId())
151
                ->andWhere('i.institution = :institution')
152
                ->setParameter('institution', $institution->getInstitution())
153
                ->getQuery()
154
                ->getSingleResult();
155
    }
156
157 View Code Duplication
    public function removeByIdentityId(IdentityId $identityId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        $this->getEntityManager()->createQueryBuilder()
160
            ->delete($this->_entityName, 'i')
161
            ->where('i.id = :identityId')
162
            ->setParameter('identityId', $identityId->getIdentityId())
163
            ->getQuery()
164
            ->execute();
165
    }
166
167
    /**
168
     * @param Institution $institution
169
     * @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...
170
     */
171
    public function findByInstitution(Institution $institution)
172
    {
173
        return $this->createQueryBuilder('i')
174
            ->where('i.institution = :institution')
175
            ->setParameter('institution', $institution->getInstitution())
176
            ->getQuery()
177
            ->getResult();
178
    }
179
}
180