Passed
Push — master ( 0f2aee...6821c2 )
by SignpostMarv
03:16
created

DaftObjectRepositoryByType()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 25
ccs 17
cts 17
cp 1
crap 4
rs 9.52
c 0
b 0
f 0
1
<?php
2
/**
3
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject;
10
11
use ParagonIE\EasyDB\EasyDB;
12
use PDO;
13
use PDOStatement;
14
15
abstract class AbstractDaftObjectEasyDBTree extends AbstractDaftObjectEasyDBRepository implements DaftNestedObjectTree
16
{
17 28
    public function RecallDaftNestedObjectFullTree(int $limit = null) : array
18
    {
19 28
        return $this->RecallDaftNestedObjectTreeFromArgs(null, null, $limit, false);
20
    }
21
22 40
    public function CountDaftNestedObjectFullTree(int $withLimit = null) : int
23
    {
24 40
        return $this->CountDaftNestedObjectTreeFromArgs(null, null, $withLimit, false);
25
    }
26
27 14
    public function RecallDaftNestedObjectTreeWithObject(
28
        DaftNestedObject $root,
29
        bool $withRoot,
30
        ? int $limit
31
    ) : array {
32 14
        $left = $root->GetIntNestedLeft();
33 14
        $right = $root->GetIntNestedRight();
34 14
        $limit = is_int($limit) ? ($root->GetIntNestedLevel() + $limit) : null;
35
36 14
        return $this->RecallDaftNestedObjectTreeFromArgs($left, $right, $limit, $withRoot);
37
    }
38
39 20
    public function CountDaftNestedObjectTreeWithObject(
40
        DaftNestedObject $root,
41
        bool $withRoot,
42
        ? int $limit
43
    ) : int {
44 20
        $left = $root->GetIntNestedLeft();
45 20
        $right = $root->GetIntNestedRight();
46 20
        $limit = is_int($limit) ? ($root->GetIntNestedLevel() + $limit) : null;
47
48 20
        return $this->CountDaftNestedObjectTreeFromArgs($left, $right, $limit, $withRoot);
49
    }
50
51 28
    public function RecallDaftNestedObjectTreeWithId(
52
        $id,
53
        bool $withRoot,
54
        ? int $limit
55
    ) : array {
56 28
        $object = $this->RecallDaftObject($id);
57
58
        return
59 28
            ($object instanceof DaftNestedObject)
60 4
                ? $this->RecallDaftNestedObjectTreeWithObject(
61 4
                    $object,
62 4
                    $withRoot,
63 4
                    $limit
64
                )
65
                : (
66 24
                    ((array) $id === (array) $this->GetNestedObjectTreeRootId())
67 24
                        ? $this->RecallDaftNestedObjectFullTree(0)
68 28
                        : []
69
                );
70
    }
71
72 4
    public function CountDaftNestedObjectTreeWithId(
73
        $id,
74
        bool $withRoot,
75
        ? int $limit
76
    ) : int {
77 4
        $object = $this->RecallDaftObject($id);
78
79
        return
80 4
            ($object instanceof DaftNestedObject)
81 4
                ? $this->CountDaftNestedObjectTreeWithObject(
82 4
                    $object,
83 4
                    $withRoot,
84 4
                    $limit
85
                )
86
                : (
87 4
                    ((array) $id === (array) $this->GetNestedObjectTreeRootId())
88 4
                        ? $this->CountDaftNestedObjectFullTree($limit)
89 4
                        : 0
90
                );
91
    }
92
93 4
    public function RecallDaftNestedObjectPathToObject(
94
        DaftNestedObject $leaf,
95
        bool $includeLeaf
96
    ) : array {
97 4
        return $this->RecallDaftNestedObjectTreeFromArgs(
98 4
            $leaf->GetIntNestedLeft(),
99 4
            $leaf->GetIntNestedRight(),
100 4
            null,
101 4
            $includeLeaf,
102 4
            false
103
        );
104
    }
105
106 4
    public function CountDaftNestedObjectPathToObject(
107
        DaftNestedObject $leaf,
108
        bool $includeLeaf
109
    ) : int {
110 4
        return $this->CountDaftNestedObjectTreeFromArgs(
111 4
            $leaf->GetIntNestedLeft(),
112 4
            $leaf->GetIntNestedRight(),
113 4
            null,
114 4
            $includeLeaf,
115 4
            false
116
        );
117
    }
118
119 4
    public function RecallDaftNestedObjectPathToId($id, bool $includeLeaf) : array
120
    {
121 4
        $object = $this->RecallDaftObject($id);
122
123
        return
124 4
            ($object instanceof DaftNestedObject)
125 4
                ? $this->RecallDaftNestedObjectPathToObject($object, $includeLeaf)
126 4
                : [];
127
    }
128
129 4
    public function CountDaftNestedObjectPathToId($id, bool $includeLeaf) : int
130
    {
131 4
        $object = $this->RecallDaftObject($id);
132
133
        return
134 4
            ($object instanceof DaftNestedObject)
135 4
                ? $this->CountDaftNestedObjectPathToObject($object, $includeLeaf)
136 4
                : 0;
137
    }
138
139 24
    public function CompareObjects(DaftNestedObject $a, DaftNestedObject $b) : int
140
    {
141 24
        return $a->GetIntNestedSortOrder() <=> $b->GetIntNestedSortOrder();
142
    }
143
144 50
    public static function DaftObjectRepositoryByType(
145
        string $type,
146
        ? EasyDB $db = null
147
    ) : DaftObjectRepository {
148 50
        if (is_a(static::class, DaftNestedWriteableObjectTree::class, true)) {
149 38
            if ( ! is_a($type, DaftNestedWriteableObject::class, true)) {
150 10
                throw new DaftObjectRepositoryTypeByClassMethodAndTypeException(
151 10
                    1,
152 10
                    static::class,
153 10
                    __FUNCTION__,
154 10
                    DaftNestedWriteableObject::class,
155 38
                    $type
156
                );
157
            }
158 12
        } elseif ( ! is_a($type, DaftNestedObject::class, true)) {
159 10
            throw new DaftObjectRepositoryTypeByClassMethodAndTypeException(
160 10
                1,
161 10
                static::class,
162 10
                __FUNCTION__,
163 10
                DaftNestedObject::class,
164 10
                $type
165
            );
166
        }
167
168 30
        return parent::DaftObjectRepositoryByType($type, $db);
169
    }
170
171 40
    protected function RememberDaftObjectData(DefinesOwnIdPropertiesInterface $object) : void
172
    {
173 40
        static::ThrowIfNotType($object, DaftNestedObject::class, 1, __METHOD__);
174
175 40
        parent::RememberDaftObjectData($object);
176 40
    }
177
178 40
    final protected function SelectingQueryDaftNestedObjectTreeFromArgs(bool $recall) : string
179
    {
180 40
        if ($recall) {
181
            /**
182
            * @var string[] $props
183
            */
184 28
            $props = $this->type::DaftObjectIdProperties();
185
186 28
            return implode(', ', array_map(
187
                function (string $prop) : string {
188 28
                    return $this->db->escapeIdentifier($prop);
189 28
                },
190 28
                $props
191
            ));
192
        }
193
194 40
        return 'COUNT(*)';
195
    }
196
197
    /**
198
    * @param array<int, string> $filter
199
    */
200 40
    final protected function FilterQueryDaftNestedObjectTreeFromArgs(array $filter) : string
201
    {
202 40
        return (count($filter) > 0) ? (' WHERE ' . implode(' AND ', $filter)) : '';
203
    }
204
205 40
    protected function QueryDaftNestedObjectTreeFromArgs(
206
        bool $recall,
207
        ? int $left,
208
        ? int $right,
209
        ? int $limit,
210
        bool $withRoot,
211
        bool $treeNotPath = true
212
    ) : PDOStatement {
213 40
        $queryArgs = [];
214 40
        $filter = [];
215
216 40
        $leftOp = ($withRoot ? ' >= ' : ' > ');
217 40
        $rightOp = ($withRoot ? ' <= ' : ' < ');
218
219 40
        if ( ! $treeNotPath) {
220 4
            list($leftOp, $rightOp) = [$rightOp, $leftOp];
221
        }
222
223 40
        $escapedLeft = $this->db->escapeIdentifier('intNestedLeft');
224
225
        $maybeArgs = [
226 40
            ($escapedLeft . $leftOp . ' ?') => $left,
227 40
            ($this->db->escapeIdentifier('intNestedRight') . $rightOp . ' ?') => $right,
228 40
            ($this->db->escapeIdentifier('intNestedLevel') . ' <= ?') => $limit,
229
        ];
230
231 40
        foreach (array_filter($maybeArgs, 'is_int') as $filterStr => $queryArgVar) {
232 28
            $queryArgs[] = $queryArgVar;
233 28
            $filter[] = $filterStr;
234
        }
235
236
        $query =
237
            'SELECT ' .
238 40
            $this->SelectingQueryDaftNestedObjectTreeFromArgs($recall) .
239 40
            ' FROM ' .
240 40
            $this->db->escapeIdentifier($this->DaftObjectDatabaseTable()) .
241 40
            $this->FilterQueryDaftNestedObjectTreeFromArgs($filter) .
242 40
            ' ORDER BY ' .
243 40
            $escapedLeft;
244
245 40
        $sth = $this->db->prepare($query);
246
247 40
        $sth->execute($queryArgs);
248
249 40
        return $sth;
250
    }
251
252 28
    protected function RecallDaftNestedObjectTreeFromArgs(
253
        ? int $left,
254
        ? int $right,
255
        ? int $limit,
256
        bool $withRoot,
257
        bool $treeNotPath = true
258
    ) : array {
259 28
        $sth = $this->QueryDaftNestedObjectTreeFromArgs(
260 28
            true,
261 28
            $left,
262 28
            $right,
263 28
            $limit,
264 28
            $withRoot,
265 28
            $treeNotPath
266
        );
267
268 28
        return array_filter(
269 28
            array_map([$this, 'RecallDaftObject'], $sth->fetchAll(PDO::FETCH_NUM)),
270
            function (? DaftObject $maybe) : bool {
271 28
                return $maybe instanceof DaftNestedObject;
272 28
            }
273
        );
274
    }
275
276 40
    protected function CountDaftNestedObjectTreeFromArgs(
277
        ? int $left,
278
        ? int $right,
279
        ? int $limit,
280
        bool $withRoot,
281
        bool $treeNotPath = true
282
    ) : int {
283 40
        $sth = $this->QueryDaftNestedObjectTreeFromArgs(
284 40
            false,
285 40
            $left,
286 40
            $right,
287 40
            $limit,
288 40
            $withRoot,
289 40
            $treeNotPath
290
        );
291
292 40
        return (int) $sth->fetchColumn();
293
    }
294
}
295