Completed
Push — feature/add-properties-to-orde... ( 90ff61 )
by A.
13:39
created

AuditLogRepository   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 8.11 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 6
dl 9
loc 111
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
D createSecondFactorSearchQuery() 0 30 9
A findEntriesWhereIdentityIsActorOnly() 0 9 1
A removeByIdentityId() 9 9 1
A save() 0 6 1
A saveAll() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Doctrine\ORM\Query;
23
use Surfnet\Stepup\Identity\Value\IdentityId;
24
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\AuditLogEntry;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Query\SecondFactorAuditLogQuery;
27
28
class AuditLogRepository extends EntityRepository
29
{
30
    /**
31
     * An array of event FQCNs that pertain to second factors (verification, vetting, revocation etc.).
32
     *
33
     * @var string[]
34
     */
35
    private static $secondFactorEvents = [
36
        'Surfnet\Stepup\Identity\Event\YubikeySecondFactorBootstrappedEvent',
37
        'Surfnet\Stepup\Identity\Event\GssfPossessionProvenEvent',
38
        'Surfnet\Stepup\Identity\Event\PhonePossessionProvenEvent',
39
        'Surfnet\Stepup\Identity\Event\YubikeyPossessionProvenEvent',
40
        'Surfnet\Stepup\Identity\Event\EmailVerifiedEvent',
41
        'Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent',
42
        'Surfnet\Stepup\Identity\Event\UnverifiedSecondFactorRevokedEvent',
43
        'Surfnet\Stepup\Identity\Event\VerifiedSecondFactorRevokedEvent',
44
        'Surfnet\Stepup\Identity\Event\VettedSecondFactorRevokedEvent',
45
        'Surfnet\Stepup\Identity\Event\CompliedWithUnverifiedSecondFactorRevocationEvent',
46
        'Surfnet\Stepup\Identity\Event\CompliedWithVerifiedSecondFactorRevocationEvent',
47
        'Surfnet\Stepup\Identity\Event\CompliedWithVettedSecondFactorRevocationEvent',
48
        'Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaaEvent',
49
        'Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaEvent',
50
        'Surfnet\Stepup\Identity\Event\AppointedAsRaaEvent',
51
        'Surfnet\Stepup\Identity\Event\AppointedAsRaEvent',
52
        'Surfnet\Stepup\Identity\Event\RegistrationAuthorityRetractedEvent',
53
    ];
54
55
    /**
56
     * @param SecondFactorAuditLogQuery $query
57
     * @return Query
58
     */
59
    public function createSecondFactorSearchQuery(SecondFactorAuditLogQuery $query)
60
    {
61
        $queryBuilder = $this
62
            ->createQueryBuilder('al')
63
            ->where('al.identityInstitution = :identityInstitution')
64
            ->setParameter('identityInstitution', $query->identityInstitution)
65
            ->andWhere('al.identityId = :identityId')
66
            ->andWhere('al.event IN (:secondFactorEvents)')
67
            ->setParameter('identityId', $query->identityId)
68
            ->setParameter('secondFactorEvents', self::$secondFactorEvents);
69
70
        switch ($query->orderBy) {
71
            case 'secondFactorId':
72
            case 'secondFactorType':
73
            case 'secondFactorIdentifier':
74
            case 'event':
75
            case 'recordedOn':
76
            case 'actorId':
77
            case 'actorCommonName':
78
                $queryBuilder->orderBy(
79
                    sprintf('al.%s', $query->orderBy),
80
                    $query->orderDirection === 'desc' ? 'DESC' : 'ASC'
81
                );
82
                break;
83
            default:
84
                throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy));
85
        }
86
87
        return $queryBuilder->getQuery();
88
    }
89
90
    /**
91
     * @param IdentityId $actorId
92
     * @return AuditLogEntry[]
93
     */
94
    public function findEntriesWhereIdentityIsActorOnly(IdentityId $actorId)
95
    {
96
        return $this->createQueryBuilder('al')
97
            ->where('al.actorId = :actorId')
98
            ->andWhere('al.identityId != :actorId')
99
            ->setParameter('actorId', $actorId)
100
            ->getQuery()
101
            ->getResult();
102
    }
103
104
    /**
105
     * @param IdentityId $identityId
106
     * @return void
107
     */
108 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...
109
    {
110
        $this->getEntityManager()->createQueryBuilder()
111
            ->delete($this->_entityName, 'al')
112
            ->where('al.identityId = :identityId')
113
            ->setParameter('identityId', $identityId->getIdentityId())
114
            ->getQuery()
115
            ->execute();
116
    }
117
118
    /**
119
     * @param AuditLogEntry $entry
120
     */
121
    public function save(AuditLogEntry $entry)
122
    {
123
        $entityManager = $this->getEntityManager();
124
        $entityManager->persist($entry);
125
        $entityManager->flush();
126
    }
127
128
    public function saveAll(array $entries)
129
    {
130
        $entityManager = $this->getEntityManager();
131
132
        foreach ($entries as $entry) {
133
            $entityManager->persist($entry);
134
        }
135
136
        $entityManager->flush();
137
    }
138
}
139