Completed
Push — master ( 2d50ed...6bb3cd )
by James Ekow Abaka
10:18
created

RecordWrapper::postUpdateCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * The MIT License
4
 *
5
 * Copyright 2015 ekow.
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
26
namespace ntentan\nibii;
27
28
use ntentan\utils\Utils;
29
use ntentan\kaikai\Cache;
30
31
class RecordWrapper implements \ArrayAccess, \Countable, \Iterator
32
{
33
    use \ntentan\utils\DependencyInjector;
34
    
35
    protected $hasMany = [];
36
    protected $belongsTo = [];
37
    protected $manyHaveMany = [];
38
    protected $behaviours = [];
39
40
    protected $table;
41
    private $modelData = [];
42
    private $invalidFields;
43
    private $dynamicOperations;
44
    private $index = 0;
45
    private $dataSet = false;
46
    private $adapter;
47
48 36
    public function __construct()
49
    {
50 36
        Utils::factory(
51 36
            $this->table,
52
            function() {
53 36
                return Nibii::getModelTable($this);
54
            }
55 36
        );
56 36
        foreach($this->behaviours as $behaviour) {
57
            $behaviourInstance = $this->loadDependency($behaviour);
58
            $behaviourInstance->setModel($this);
59 36
        }
60 36
    }
61
62
    /**
63
     *
64
     * @return \ntentan\nibii\DriverAdapter
65
     */
66 34
    protected function getDataAdapter()
67
    {
68 34
        if(!$this->adapter)
69 34
        {
70 34
            $this->adapter = DriverAdapter::getDefaultInstance();
71 34
        }
72 34
        return $this->adapter;
73
    }
74
75
    /**
76
     * 
77
     * @return ModelDescription
78
     */
79 28
    public function getDescription()
80
    {
81 28
        return Cache::read(
82 28
            (new \ReflectionClass($this))->getName() . '::desc',
83
            function() 
84
            {
85 8
                return new ModelDescription($this);
86
            }
87 28
        );
88
    }
89
    
90 10
    public function count()
91
    {
92 10
        if ($this->dataSet) {
93 10
            return count($this->getData());
94
        } else {
95
            return $this->__call('count', []);
0 ignored issues
show
Documentation introduced by
'count' is of type string, but the function expects a object<ntentan\nibii\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
array() is of type array, but the function expects a object<ntentan\nibii\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
        }
97
    }
98
99 12
    private function retrieveItem($key)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
100
    {
101 12
        $relationships = $this->getDescription()->getRelationships();
102 12
        if(isset($relationships[$key])) {
103 8
            return $this->fetchRelatedFields($relationships[$key]);
104
        } else {
105 4
            return isset($this->modelData[$key]) ? $this->modelData[$key] : null;
106
        }
107
    }
108
109
    /**
110
     * Create a new instance of this Model
111
     * @return \ntentan\nibii\RecordWrapper
112
     */
113 34
    public static function createNew()
114
    {
115 34
        $class = get_called_class();
116 34
        return new $class();
117
    }
118
119
    /**
120
     * @method
121
     * @param type $name
122
     * @param type $arguments
123
     * @return type
124
     */
125 34
    public function __call($name, $arguments)
126
    {
127 34
        return Utils::factory($this->dynamicOperations,
128 34
            function() {
129 34
                return new Operations($this, $this->getDataAdapter());
130
            }
131 34
        )->perform($name, $arguments);
132
    }
133
134 26
    public static function __callStatic($name, $arguments)
135
    {
136 26
        return call_user_func_array([self::createNew(), $name], $arguments);
137
    }
138
139 8
    public function __set($name, $value)
140
    {
141 8
        $this->dataSet = true;
142 8
        $this->modelData[$name] = $value;
143 8
    }
144
145 12
    public function __get($name)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
146
    {
147 12
        return $this->retrieveItem($name);
148
    }
149
150 32
    public function getTable()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
151
    {
152 32
        return $this->table;
153
    }
154
    
155 4
    private function expandArrayValue($array, $relationships, $depth, $index = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
156
    {
157 4
        foreach($relationships as $name => $relationship) {
158 4
            $array[$name] = $this->fetchRelatedFields($relationship, $index)->toArray($depth);
159 4
        }
160 4
        return $array;
161
    }
162
163 16
    public function toArray($depth = 0)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
164
    {
165 16
        $relationships = $this->getDescription()->getRelationships();
166 16
        $array = $this->modelData;
167 16
        if($depth > 0) {
168 4
            if($this->hasMultipleData()) {
169
                foreach($array as $i => $value) {
170
                    $array[$i] = $this->expandArrayValue($value, $relationships, $depth - 1, $i);
171
                }
172
            } else {
173 4
                $array = $this->expandArrayValue($array, $relationships, $depth - 1);
174
            }
175 4
        }
176 16
        return $array;
177
    }
178
    
179 10
    public function save()
180
    {
181 10
        $return = $this->__call('save', [$this->hasMultipleData()]);
0 ignored issues
show
Documentation introduced by
'save' is of type string, but the function expects a object<ntentan\nibii\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
array($this->hasMultipleData()) is of type array<integer,boolean,{"0":"boolean"}>, but the function expects a object<ntentan\nibii\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
182 10
        $this->invalidFields = $this->dynamicOperations->getInvalidFields();
183 10
        return $return;
184
    }
185
186 18
    private function hasMultipleData()
187
    {
188 18
        if(count($this->modelData) > 0) {
189 16
            return is_numeric(array_keys($this->modelData)[0]);
190
        } else {
191 2
            return false;
192
        }
193
    }
194
195 14
    public function getData()
196
    {
197 14
        $data = [];
198
                
199 14
        if(count($this->modelData) == 0) {
200 6
            $data = $this->modelData;
201 14
        } else if($this->hasMultipleData()) {
202 2
            $data = $this->modelData;
203 12
        } else if(count($this->modelData) > 0) {
204 12
            $data[] = $this->modelData;
205 12
        }
206
        
207 14
        return $data;
208
    }
209
210 26
    public function setData($data)
211
    {
212 26
        $this->dataSet = true;
213 26
        $this->modelData = $data;
214 26
    }
215
    
216
    public function mergeData($data)
217
    {
218
        foreach($data as $key => $value) {
219
            $this->modelData[$key] = $value;
220
        }
221
        $this->dataSet = true;
222
    }
223
224 2
    public function offsetExists($offset)
225
    {
226 2
        return isset($this->modelData[$offset]);
227
    }
228
229 2
    public function offsetGet($offset)
230
    {
231 2
        if (is_numeric($offset)) {
232 2
            return $this->wrap($offset);
233
        } else {
234 2
            return $this->retrieveItem($offset);
235
        }
236
    }
237
238 2
    public function offsetSet($offset, $value)
239
    {
240 2
        $this->dataSet = true;
241 2
        $this->modelData[$offset] = $value;
242 2
    }
243
244
    public function offsetUnset($offset)
245
    {
246
        unset($this->modelData[$offset]);
247
    }
248
249 6
    private function wrap($offset)
250
    {
251 6
        if(isset($this->modelData[$offset])) {
252 6
            $newInstance = $this->createNew();
253 6
            $newInstance->setData($this->modelData[$offset]);
254 6
            return $newInstance;
255
        } else {
256
            return null;
257
        }
258
    }
259
260 4
    public function getInvalidFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
261
    {
262 4
        return $this->invalidFields;
263
    }
264
265
    public function getHasMany()
266
    {
267
        return $this->hasMany;
268
    }
269
270
    public function getBelongsTo()
271
    {
272
        return $this->belongsTo;
273
    }
274
275 4
    public function current()
276
    {
277 4
        return $this->wrap($this->index);
278
    }
279
280
    public function key()
281
    {
282
        return $this->index;
283
    }
284
285 4
    public function next()
286
    {
287 4
        $this->index++;
288 4
    }
289
290 4
    public function rewind()
291
    {
292 4
        $this->index = 0;
293 4
    }
294
295 4
    public function valid()
296
    {
297 4
        return isset($this->modelData[$this->index]);
298
    }
299
300 12
    private function fetchRelatedFields($relationship, $index = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
301
    {
302 12
        if($index === null) {
303 12
            $data = $this->modelData;
304 12
        } else {
305
            $data = $this->modelData[$index];
306
        }
307 12
        $model = $relationship->getModelInstance();
308 12
        if(empty($data)) {
309
            return $model;
310
        } else {
311 12
            return $model->fetch($relationship->getQuery($data));
312
        }
313
    }
314
315 8
    public function getRelationships()
316
    {
317
        return [
318 8
            'HasMany' => $this->hasMany,
319 8
            'BelongsTo' => $this->belongsTo,
320 8
            'ManyHaveMany' => $this->manyHaveMany
321 8
        ];
322
    }
323
    
324
    public function usetField($field)
325
    {
326
        unset($this->modelData[$field]);
327
    }
328
    
329 8
    public function preSaveCallback()
330
    {
331
        
332 8
    }
333
    
334 4
    public function postSaveCallback($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
335
    {
336
        
337 4
    }
338
    
339 2
    public function preUpdateCallback()
340
    {
341
        
342 2
    }
343
    
344 2
    public function postUpdateCallback()
345
    {
346
        
347 2
    }
348
    
349 10
    public function getBehaviours()
350
    {
351 10
        return $this->loadedDependencies;
352
    }
353
}
354