Passed
Push — master ( ed2761...02fcf9 )
by Jonathan
02:28
created

DataSourceObjectDataRepository::slice()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 3
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
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
    /**
89
     * @param mixed[] $criteria
90
     * @param mixed[] $row
91
     */
92 5
    private function matches(array $criteria, array $row) : bool
93
    {
94 5
        return (new CriteriaMatcher($criteria, $row))->matches();
95
    }
96
97
    /**
98
     * @param mixed[][] $rows
99
     * @param string[]  $orderBy
100
     *
101
     * @return mixed[][] $rows
102
     */
103 1
    private function sort(array $rows, array $orderBy) : array
104
    {
105 1
        usort($rows, new Sorter($orderBy));
106
107 1
        return $rows;
108
    }
109
110
    /**
111
     * @param mixed[][] $rows
112
     *
113
     * @return mixed[][] $rows
114
     */
115 3
    private function slice(array $rows, ?int $limit, ?int $offset) : array
116
    {
117 3
        if ($limit === null) {
118 1
            $limit = count($rows);
119
        }
120
121 3
        if ($offset === null) {
122 1
            $offset = 0;
123
        }
124
125 3
        return array_slice($rows, $offset, $limit);
126
    }
127
128
    /**
129
     * @return mixed[][]
130
     */
131 6
    private function getSourceRows() : array
132
    {
133 6
        if ($this->sourceRows === null) {
134 6
            $this->sourceRows = $this->dataSource->getSourceRows();
135
        }
136
137 6
        return $this->sourceRows;
138
    }
139
}
140