ArrayObjectDataRepository   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 81
ccs 28
cts 28
cp 1
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findOneBy() 0 19 5
A findAll() 0 3 1
A findBy() 0 27 5
A __construct() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\SkeletonMapper\DataRepository;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\SkeletonMapper\ObjectManagerInterface;
9
10
class ArrayObjectDataRepository extends BasicObjectDataRepository
11
{
12
    /** @var ArrayCollection */
13
    private $objects;
14
15 25
    public function __construct(
16
        ObjectManagerInterface $objectManager,
17
        ArrayCollection $objects,
18
        string $className
19
    ) {
20 25
        parent::__construct($objectManager, $className);
21 25
        $this->objects = $objects;
22 25
    }
23
24
    /**
25
     * @return mixed[][]
26
     */
27 2
    public function findAll() : array
28
    {
29 2
        return $this->objects->toArray();
30
    }
31
32
    /**
33
     * @param mixed[] $criteria
34
     * @param mixed[] $orderBy
35
     *
36
     * @return mixed[][]
37
     */
38 2
    public function findBy(
39
        array $criteria,
40
        ?array $orderBy = null,
41
        ?int $limit = null,
42
        ?int $offset = null
43
    ) : array {
44 2
        $objects = [];
45
46 2
        foreach ($this->objects as $object) {
47 2
            $matches = true;
48
49 2
            foreach ($criteria as $key => $value) {
50 2
                if ($object[$key] === $value) {
51 2
                    continue;
52
                }
53
54 1
                $matches = false;
55
            }
56
57 2
            if (! $matches) {
58 1
                continue;
59
            }
60
61 2
            $objects[] = $object;
62
        }
63
64 2
        return $objects;
65
    }
66
67
    /**
68
     * @param mixed[] $criteria
69
     *
70
     * @return mixed[]|null
71
     */
72 19
    public function findOneBy(array $criteria) : ?array
73
    {
74 19
        foreach ($this->objects as $object) {
75 19
            $matches = true;
76
77 19
            foreach ($criteria as $key => $value) {
78 19
                if ($object[$key] === $value) {
79 19
                    continue;
80
                }
81
82 8
                $matches = false;
83
            }
84
85 19
            if ($matches) {
86 19
                return $object;
87
            }
88
        }
89
90 3
        return null;
91
    }
92
}
93