collection   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 29
c 3
b 1
f 0
dl 0
loc 61
ccs 34
cts 34
cp 1
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A apply_qb_constraints() 0 4 2
A find() 0 5 1
A purge() 0 12 3
A is_empty() 0 4 1
A get_qb() 0 5 1
A delete() 0 12 3
1
<?php
2
/**
3
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
4
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
6
 */
7
8
namespace midgard\portable\storage;
9
10
use midgard_query_builder;
11
12
class collection
13
{
14
    private readonly string $classname;
15
16 16
    public function __construct(string $classname)
17
    {
18 16
        $this->classname = $classname;
0 ignored issues
show
Bug introduced by
The property classname is declared read-only in midgard\portable\storage\collection.
Loading history...
19
    }
20
21 2
    public function is_empty(string $guid) : bool
22
    {
23 2
        $qb = $this->get_qb($guid);
24 2
        return $qb->count() == 0;
25
    }
26
27 15
    public function find(string $guid, array $constraints) : array
28
    {
29 15
        $qb = $this->get_qb($guid);
30 15
        $this->apply_qb_constraints($qb, $constraints);
31 15
        return $qb->execute();
32
    }
33
34 1
    public function delete(string $guid, array $constraints) : int
35
    {
36 1
        $qb = $this->get_qb($guid);
37 1
        $this->apply_qb_constraints($qb, $constraints);
38 1
        $params = $qb->execute();
39 1
        $deleted_count = 0;
40 1
        foreach ($params as $param) {
41 1
            if ($param->delete()) {
42 1
                $deleted_count++;
43
            }
44
        }
45 1
        return $deleted_count;
46
    }
47
48 1
    public function purge(string $guid, array $constraints) : int
49
    {
50 1
        $qb = $this->get_qb($guid);
51 1
        $this->apply_qb_constraints($qb, $constraints);
52 1
        $params = $qb->execute();
53 1
        $purged_count = 0;
54 1
        foreach ($params as $param) {
55 1
            if ($param->purge()) {
56 1
                $purged_count++;
57
            }
58
        }
59 1
        return $purged_count;
60
    }
61
62 16
    private function get_qb(string $guid) : midgard_query_builder
63
    {
64 16
        $qb = new midgard_query_builder(connection::get_fqcn($this->classname));
65 16
        $qb->add_constraint('parentguid', '=', $guid);
66 16
        return $qb;
67
    }
68
69 15
    private function apply_qb_constraints(midgard_query_builder $qb, array $constraints)
70
    {
71 15
        foreach ($constraints as $name => $value) {
72 14
            $qb->add_constraint($name, '=', $value);
73
        }
74
    }
75
}
76