Completed
Push — release-1.x ( c1f7bf...3be149 )
by Boy
09:15 queued 05:00
created

AuditLogRepository   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 109
Duplicated Lines 8.26 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 9
loc 109
wmc 12
lcom 2
cbo 6
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findEntriesWhereIdentityIsActorOnly() 0 9 1
A removeByIdentityId() 9 9 1
A save() 0 6 1
A saveAll() 0 10 2
C createSecondFactorSearchQuery() 0 28 7

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 'event':
74
            case 'recordedOn':
75
            case 'actorId':
76
                $queryBuilder->orderBy(
77
                    sprintf('al.%s', $query->orderBy),
78
                    $query->orderDirection === 'desc' ? 'DESC' : 'ASC'
79
                );
80
                break;
81
            default:
82
                throw new RuntimeException(sprintf('Unknown order by column "%s"', $query->orderBy));
83
        }
84
85
        return $queryBuilder->getQuery();
86
    }
87
88
    /**
89
     * @param IdentityId $actorId
90
     * @return AuditLogEntry[]
91
     */
92
    public function findEntriesWhereIdentityIsActorOnly(IdentityId $actorId)
93
    {
94
        return $this->createQueryBuilder('al')
95
            ->where('al.actorId = :actorId')
96
            ->andWhere('al.identityId != :actorId')
97
            ->setParameter('actorId', $actorId)
98
            ->getQuery()
99
            ->getResult();
100
    }
101
102
    /**
103
     * @param IdentityId $identityId
104
     * @return void
105
     */
106 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...
107
    {
108
        $this->getEntityManager()->createQueryBuilder()
109
            ->delete($this->_entityName, 'al')
110
            ->where('al.identityId = :identityId')
111
            ->setParameter('identityId', $identityId->getIdentityId())
112
            ->getQuery()
113
            ->execute();
114
    }
115
116
    /**
117
     * @param AuditLogEntry $entry
118
     */
119
    public function save(AuditLogEntry $entry)
120
    {
121
        $entityManager = $this->getEntityManager();
122
        $entityManager->persist($entry);
123
        $entityManager->flush();
124
    }
125
126
    public function saveAll(array $entries)
127
    {
128
        $entityManager = $this->getEntityManager();
129
130
        foreach ($entries as $entry) {
131
            $entityManager->persist($entry);
132
        }
133
134
        $entityManager->flush();
135
    }
136
}
137