| 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\ORM\EntityRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Surfnet\Stepup\Identity\Value\IdentityId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Surfnet\Stepup\Identity\Value\Institution; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Surfnet\Stepup\Identity\Value\NameId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\IdentityQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | class IdentityRepository extends EntityRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return Identity|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function find($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         /** @var Identity|null $identity */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $identity = parent::find($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return $identity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param Identity $identity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function save(Identity $identity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $entityManager = $this->getEntityManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $entityManager->persist($identity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $entityManager->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param IdentityQuery $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return \Doctrine\ORM\Query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function createSearchQuery(IdentityQuery $query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $queryBuilder = $this->createQueryBuilder('i'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $queryBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             ->where('i.institution = :institution') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ->setParameter('institution', $query->institution); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if ($query->nameId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $queryBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 ->andWhere('i.nameId = :nameId') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 ->setParameter('nameId', $query->nameId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if ($query->email) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $queryBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 ->andWhere('MATCH_AGAINST(i.email, :email) > 0') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 ->setParameter('email', $query->email); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if ($query->commonName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $queryBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 ->andWhere('MATCH_AGAINST(i.commonName, :commonName) > 0') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 ->setParameter('commonName', $query->commonName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $queryBuilder->getQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param string[] $nameIds | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return Identity[] Indexed by NameID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function findByNameIdsIndexed(array $nameIds) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return $this->getEntityManager()->createQueryBuilder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             ->select('i') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ->from('Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity', 'i', 'i.nameId') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             ->where('i.nameId IN (:nameIds)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             ->setParameter('nameIds', $nameIds) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ->getQuery() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ->getResult(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param NameId      $nameId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param Institution $institution | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function hasIdentityWithNameIdAndInstitution(NameId $nameId, Institution $institution) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $identityCount = $this->createQueryBuilder('i') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             ->select('COUNT(i.id)') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             ->where('i.nameId = :nameId') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             ->andWhere('i.institution = :institution') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             ->setParameter('nameId', $nameId->getNameId()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             ->setParameter('institution', $institution->getInstitution()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             ->getQuery() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             ->getSingleScalarResult(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return $identityCount > 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param NameId      $nameId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param Institution $institution | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @return Identity | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     public function findOneByNameIdAndInstitution(NameId $nameId, Institution $institution) | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |         return $this->createQueryBuilder('i') | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |                 ->where('i.nameId = :nameId') | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |                 ->setParameter('nameId', $nameId->getNameId()) | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |                 ->andWhere('i.institution = :institution') | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |                 ->setParameter('institution', $institution->getInstitution()) | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |                 ->getQuery() | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |                 ->getSingleResult(); | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 136 |  | View Code Duplication |     public function removeByIdentityId(IdentityId $identityId) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $this->getEntityManager()->createQueryBuilder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             ->delete($this->_entityName, 'i') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             ->where('i.id = :identityId') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             ->setParameter('identityId', $identityId->getIdentityId()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             ->getQuery() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             ->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 145 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |  | 
            
                        
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.