Completed
Push — master ( b4f572...d20dd2 )
by Alex
13s queued 11s
created

ReferenceRepository::addCollectionReference()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasDoctrineFixtures\Service\Repository;
6
7
/**
8
 * @author  Alex Patterson <[email protected]>
9
 * @package Arp\LaminasDoctrineFixtures\Service\Repository
10
 */
11
class ReferenceRepository extends \Doctrine\Common\DataFixtures\ReferenceRepository
12
{
13
    /**
14
     * @var iterable|array
15
     */
16
    private $collectionReferences = [];
17
18
    /**
19
     * @param string $name
20
     *
21
     * @return bool
22
     */
23
    public function hasCollectionReference(string $name): bool
24
    {
25
        return array_key_exists($name, $this->collectionReferences);
0 ignored issues
show
Bug introduced by
It seems like $this->collectionReferences can also be of type iterable; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        return array_key_exists($name, /** @scrutinizer ignore-type */ $this->collectionReferences);
Loading history...
26
    }
27
28
    /**
29
     * @param string $name
30
     *
31
     * @return iterable
32
     */
33
    public function getCollectionReference(string $name): iterable
34
    {
35
        if (! $this->getCollectionReference($name)) {
36
            throw new \OutOfBoundsException(sprintf('Collection reference to "%s" does not exist', $name));
37
        }
38
        return $this->collectionReferences[$name];
39
    }
40
41
42
    /**
43
     * @param string   $name
44
     * @param iterable $collection
45
     */
46
    public function setCollectionReference(string $name, iterable $collection): void
47
    {
48
        $this->collectionReferences[$name] = $collection;
49
    }
50
51
    /**
52
     * @param string   $name
53
     * @param iterable $collection
54
     *
55
     * @throws \BadFunctionCallException
56
     */
57
    public function addCollectionReference(string $name, iterable $collection): void
58
    {
59
        if (isset($this->collectionReferences[$name])) {
60
            throw new \BadMethodCallException(
61
                sprintf(
62
                    'Reference to "%s" already exists, use method setCollectionReference in order to override it',
63
                    $name
64
                )
65
            );
66
        }
67
        $this->setCollectionReference($name, $collection);
68
    }
69
}
70