Completed
Push — release/0.3.7 ( 136d29 )
by Mathieu
04:35
created

DBCollectionOneMany   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Test Coverage

Coverage 5.41%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 71
c 3
b 1
f 0
dl 0
loc 159
ccs 4
cts 74
cp 0.0541
rs 10
wmc 24

9 Methods

Rating   Name   Duplication   Size   Complexity  
A loadAll() 0 16 2
A getParentId() 0 3 1
A getParentIdField() 0 3 1
A getParentFilterName() 0 3 1
A getParentFilterType() 0 3 1
A save() 0 15 2
A craftItem() 0 18 5
A setParentIdForAll() 0 7 2
B loadForParentId() 0 49 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate;
6
7
class DBCollectionOneMany extends DBCollection
8
{
9
    /* @var string Name of parent identifier field */
10
    protected $parentIdField = 'parent_id';
11
12
    /* @var mixed parent identifier */
13
    protected $parentId;
14
15
    /* @var string parent field filter name */
16
    protected $parentFilterName;
17
18
    /* @var mixed parent filtering value */
19
    protected $parentFilterType;
20
21 1
    public function getParentIdField(): string
22
    {
23 1
        return $this->parentIdField;
24
    }
25
26 1
    public function getParentId()
27
    {
28 1
        return $this->parentId;
29
    }
30
31
    public function getParentFilterName()
32
    {
33
        return $this->parentFilterName;
34
    }
35
36
    public function getParentFilterType()
37
    {
38
        return $this->parentFilterType;
39
    }
40
41
    /**
42
     * Load entire table into collection
43
     * @return Collection Loaded collection
44
     */
45
    public static function loadAll()
46
    {
47
        $calledClass = get_called_class();
48
        $collection = new $calledClass();
49
        if ($collection->getParentFilterType()) {
50
            $sql = "SELECT *";
51
            $sql .= "   FROM `" . $collection->getTableName() . "`";
52
            $sql .= "WHERE " . $collection->parentFilterName . "=:type";
53
54
            $sqlParams = ['type' => $collection->parentFilterType];
55
56
            $collection->loadFromSql($sql, $sqlParams);
57
58
            return $collection;
59
        }
60
        return parent::loadAll();
61
    }
62
63
    /**
64
     * Load items linked to a parentId
65
     * @param mixed        $parentId       Parent id description
66
     * @param string       $parentIdField  Name of parent id referencing field
67
     * @param \Closure|null $validate       Callback use to validate add to items collection
68
     */
69
    public static function loadForParentId(
70
        $parentId,
71
        $parentIdField = null,
72
        $validate = null
73
    ) {
74
        $calledClass = get_called_class();
75
        $collection = new $calledClass();
76
77
        if ($parentId != '') {
78
            $sqlParams = [];
79
            $dbHandler = Suricate::Database(true);
80
81
            if ($collection->getDBConfig() !== '') {
82
                $dbHandler->setConfig($collection->getDBConfig());
83
            }
84
85
            $sql = "SELECT *";
86
            $sql .= " FROM `" . $collection->getTableName() . "`";
87
            $sql .= " WHERE";
88
            if ($parentIdField !== null) {
89
                $sql .= "`" . $parentIdField . "`=:parent_id";
90
            } else {
91
                $sql .= "`" . $collection->getParentIdField() . "`=:parent_id";
92
            }
93
94
            if ($collection->parentFilterType !== null) {
95
                $sql .=
96
                    "   AND " . $collection->parentFilterName . "=:parent_type";
97
                $sqlParams['parent_type'] = $collection->parentFilterType;
98
            }
99
100
            $sqlParams['parent_id'] = $parentId;
101
            $results = $dbHandler->query($sql, $sqlParams)->fetchAll();
102
103
            if ($results !== false) {
104
                foreach ($results as $currentResult) {
105
                    $itemName = $collection->getItemsType();
106
                    $item = $itemName::instanciate($currentResult);
107
                    $item->setLoaded();
108
                    if ($validate === null || $validate($item)) {
109
                        $collection->addItem($item);
110
                    }
111
                }
112
            }
113
114
            $collection->parentId = $parentId;
115
        }
116
117
        return $collection;
118
    }
119
120
    public function setParentIdForAll($parentId)
121
    {
122
        $this->parentId = $parentId;
123
        foreach (array_keys($this->items) as $key) {
124
            $this->items[$key]->{$this->parentIdField} = $parentId;
125
        }
126
        return $this;
127
    }
128
129
    public function craftItem($itemData)
130
    {
131
        $itemName = $this->itemsType;
132
133
        foreach ($itemData as $data) {
134
            $newItem = new $itemName();
135
            $newItem->{$this->parentIdField} = $this->parentId;
136
            $hasData = false;
137
            foreach ($data as $field => $value) {
138
                $newItem->$field = $value;
139
                if ($value != '') {
140
                    $hasData = true;
141
                }
142
            }
143
144
            // Only add item if there's data inside
145
            if ($hasData) {
146
                $this->addItem($newItem);
147
            }
148
        }
149
    }
150
151
    public function save()
152
    {
153
        // 1st step : delete all records for current parentId
154
        $sql = "DELETE FROM `" . $this->tableName . "`";
155
        $sql .= " WHERE";
156
        $sql .= "   `" . $this->parentIdField . "`=:parent_id";
157
158
        $sqlParams = ['parent_id' => $this->parentId];
159
160
        $this->connectDB();
161
        $this->dbLink->query($sql, $sqlParams);
162
163
        // 2nd step : save all current items
164
        foreach ($this->items as $currentItem) {
165
            $currentItem->save(true); // Force insert
166
        }
167
    }
168
}
169