SraaRepository::findByNameId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
22
use Doctrine\Persistence\ManagerRegistry;
23
use Surfnet\Stepup\Identity\Value\NameId;
24
use Surfnet\StepupMiddleware\ApiBundle\Exception\InvalidArgumentException;
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Sraa;
26
27
/**
28
 * @extends ServiceEntityRepository<Sraa>
29
 */
30
class SraaRepository extends ServiceEntityRepository
31
{
32
    public function __construct(ManagerRegistry $registry)
33
    {
34
        parent::__construct($registry, Sraa::class);
35
    }
36
37
    /**
38
     * Removes all SRAA's from the database
39
     */
40
    public function removeAll(): void
41
    {
42
        $this
43
            ->getEntityManager()
44
            ->createQuery(
45
                'DELETE FROM '.Sraa::class,
0 ignored issues
show
Coding Style introduced by
Expected at least 1 space before "."; 0 found
Loading history...
Coding Style introduced by
Expected at least 1 space after "."; 0 found
Loading history...
46
            )
47
            ->execute();
48
49
        $this->getEntityManager()->clear();
50
    }
51
52
    /**
53
     * Saves all SRAAs to the database, using inserts only
54
     */
55
    public function saveAll(array $sraaList): void
56
    {
57
        $invalid = [];
58
        foreach ($sraaList as $index => $sraa) {
59
            if (!$sraa instanceof Sraa) {
60
                $invalid[$index] = $sraa;
61
            }
62
        }
63
64
        if ($invalid !== []) {
65
            $invalidIndications = [];
66
            foreach ($invalid as $index => $value) {
67
                $invalidIndications[] = sprintf(
68
                    '"%s" at index "%d"',
69
                    get_debug_type($value),
70
                    $index
71
                );
72
            }
73
74
            throw new InvalidArgumentException(
75
                sprintf(
76
                    'Expected array of Raa Objects, got %s',
77
                    implode(', ', $invalidIndications),
78
                ),
79
            );
80
        }
81
82
        $entityManager = $this->getEntityManager();
83
84
        foreach ($sraaList as $sraa) {
85
            $entityManager->persist($sraa);
86
        }
87
88
        $entityManager->flush();
89
    }
90
91
    public function findByNameId(NameId $nameId): ?Sraa
92
    {
93
        return $this->findOneBy(['nameId' => (string)$nameId]);
94
    }
95
96
    /**
97
     * @return boolean
98
     */
99
    public function contains(NameId $nameId): bool
100
    {
101
        return $this->findByNameId($nameId) !== null;
102
    }
103
}
104