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

AuditLogRepository::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
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\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