Completed
Push — master ( 5467e4...183ea4 )
by Boy
05:06 queued 01:02
created

SraaRepository::saveAll()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 34
rs 6.7272
cc 7
eloc 19
nc 12
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 Surfnet\Stepup\Identity\Value\NameId;
23
use Surfnet\StepupMiddleware\ApiBundle\Exception\InvalidArgumentException;
24
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Sraa;
25
26
class SraaRepository extends EntityRepository
27
{
28
    /**
29
     * Removes all SRAA's from the database
30
     */
31
    public function removeAll()
32
    {
33
        $this
34
            ->getEntityManager()
35
            ->createQuery(
36
                'DELETE FROM Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Sraa'
37
            )
38
            ->execute();
39
    }
40
41
    /**
42
     * Saves all SRAAs to the database, using inserts only
43
     *
44
     * @param array $sraaList
45
     */
46
    public function saveAll(array $sraaList)
47
    {
48
        $invalid = [];
49
        foreach ($sraaList as $index => $sraa) {
50
            if (!$sraa instanceof Sraa) {
51
                $invalid[$index] = $sraa;
52
            }
53
        }
54
55
        if (count($invalid)) {
56
            $invalidIndications = [];
57
            foreach ($invalid as $index => $value) {
58
                $invalidIndications[] = sprintf(
59
                    '"%s" at index "%d"',
60
                    is_object($value) ? get_class($value) : gettype($value)
61
                );
62
            }
63
64
            throw new InvalidArgumentException(
65
                sprintf(
66
                    'Expected array of Raa Objects, got %s',
67
                    implode(', ', $invalidIndications)
68
                )
69
            );
70
        }
71
72
        $entityManager = $this->getEntityManager();
73
74
        foreach ($sraaList as $sraa) {
75
            $entityManager->persist($sraa);
76
        }
77
78
        $entityManager->flush();
79
    }
80
81
    /**
82
     * @param NameId $nameId
83
     * @return null|\Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Sraa
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
84
     */
85
    public function findByNameId(NameId $nameId)
86
    {
87
        return $this->findOneBy(['nameId' => (string) $nameId]);
88
    }
89
90
    /**
91
     * @param NameId $nameId
92
     * @return boolean
93
     */
94
    public function contains(NameId $nameId)
95
    {
96
        return $this->findByNameId($nameId) !== null;
97
    }
98
}
99