DataSourceObjectDataRepository   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Test Coverage

Coverage 86.49%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 124
ccs 32
cts 37
cp 0.8649
rs 10
wmc 19

8 Methods

Rating   Name   Duplication   Size   Complexity  
A sort() 0 5 1
A getSourceRows() 0 7 2
A matches() 0 3 1
B findBy() 0 25 7
A findOneBy() 0 9 3
A __construct() 0 7 1
A slice() 0 11 3
A findAll() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\SkeletonMapper\DataSource;
6
7
use Doctrine\SkeletonMapper\DataRepository\BasicObjectDataRepository;
8
use Doctrine\SkeletonMapper\ObjectManagerInterface;
9
use function array_slice;
10
use function count;
11
use function usort;
12
13
class DataSourceObjectDataRepository extends BasicObjectDataRepository
14
{
15
    /** @var DataSource */
16
    private $dataSource;
17
18
    /** @var mixed[][]|null */
19
    private $sourceRows;
20
21 6
    public function __construct(
22
        ObjectManagerInterface $objectManager,
23
        DataSource $dataSource,
24
        string $className
25
    ) {
26 6
        parent::__construct($objectManager, $className);
27 6
        $this->dataSource = $dataSource;
28 6
    }
29
30
    /**
31
     * @return mixed[][]
32
     */
33 1
    public function findAll() : array
34
    {
35 1
        return $this->getSourceRows();
36
    }
37
38
    /**
39
     * @param mixed[] $criteria
40
     * @param mixed[] $orderBy
41
     *
42
     * @return mixed[][]
43
     */
44 5
    public function findBy(
45
        array $criteria,
46
        ?array $orderBy = null,
47
        ?int $limit = null,
48
        ?int $offset = null
49
    ) : array {
50 5
        $rows = [];
51
52 5
        foreach ($this->getSourceRows() as $row) {
53 5
            if (! $this->matches($criteria, $row)) {
54 1
                continue;
55
            }
56
57 5
            $rows[] = $row;
58
        }
59
60 5
        if ($orderBy !== null && $orderBy !== []) {
61 1
            $rows = $this->sort($rows, $orderBy);
62
        }
63
64 5
        if ($limit !== null || $offset !== null) {
65 3
            return $this->slice($rows, $limit, $offset);
66
        }
67
68 2
        return $rows;
69
    }
70
71
    /**
72
     * @param mixed[] $criteria
73
     *
74
     * @return mixed[]|null
75
     */
76
    public function findOneBy(array $criteria) : ?array
77
    {
78
        foreach ($this->getSourceRows() as $row) {
79
            if ($this->matches($criteria, $row)) {
80
                return $row;
81
            }
82
        }
83
84
        return null;
85
    }
86
87
    /**
88
     * @param mixed[] $criteria
89
     * @param mixed[] $row
90
     */
91 5
    private function matches(array $criteria, array $row) : bool
92
    {
93 5
        return (new CriteriaMatcher($criteria, $row))->matches();
94
    }
95
96
    /**
97
     * @param mixed[][] $rows
98
     * @param string[]  $orderBy
99
     *
100
     * @return mixed[][] $rows
101
     */
102 1
    private function sort(array $rows, array $orderBy) : array
103
    {
104 1
        usort($rows, new Sorter($orderBy));
105
106 1
        return $rows;
107
    }
108
109
    /**
110
     * @param mixed[][] $rows
111
     *
112
     * @return mixed[][] $rows
113
     */
114 3
    private function slice(array $rows, ?int $limit, ?int $offset) : array
115
    {
116 3
        if ($limit === null) {
117 1
            $limit = count($rows);
118
        }
119
120 3
        if ($offset === null) {
121 1
            $offset = 0;
122
        }
123
124 3
        return array_slice($rows, $offset, $limit);
125
    }
126
127
    /**
128
     * @return mixed[][]
129
     */
130 6
    private function getSourceRows() : array
131
    {
132 6
        if ($this->sourceRows === null) {
133 6
            $this->sourceRows = $this->dataSource->getSourceRows();
134
        }
135
136 6
        return $this->sourceRows;
137
    }
138
}
139