Completed
Push — master ( 355804...f8d19f )
by James Ekow Abaka
02:36
created

RecordWrapper::getDataAdapter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 8
rs 9.4285
ccs 2
cts 2
cp 1
cc 2
eloc 4
nc 2
nop 0
crap 2
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
use ntentan\panie\InjectionContainer;
31
32
class RecordWrapper implements \ArrayAccess, \Countable, \Iterator
33
{
34
    use \ntentan\panie\ComponentContainerTrait;
35
    
36
    protected $hasMany = [];
37
    protected $belongsTo = [];
38
    protected $manyHaveMany = [];
39
    protected $behaviours = [];
40
    protected $table;
41
    private $modelData = [];
42
    private $invalidFields;
43
    private $dynamicOperations;
44
    private $index = 0;
45
    private $dataSet = false;
46
    protected $adapter;
47
48 36
    public function __construct(DriverAdapter $adapter)
49
    {
50 36
        $this->table = Nibii::getModelTable($this);
51 36
        $this->adapter = $adapter;
52
        foreach($this->behaviours as $behaviour) {
53 36
            $behaviourInstance = $this->getComponentInstance($behaviour);
54
            $behaviourInstance->setModel($this);
55 36
        }
56 36
    }
57
58
    /**
59 36
     *
60 36
     * @return \ntentan\nibii\DriverAdapter
61
     */
62
    //protected function getDataAdapter()
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
    //{
64
        
65
        /*if(!$this->adapter)
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
66 34
        {
67
            $this->adapter = DriverAdapter::getDefaultInstance();
68 34
        }
69 34
        return $this->adapter;*/
70 34
    //}
71 34
72 34
    /**
73
     * 
74
     * @return ModelDescription
75
     */
76
    public function getDescription()
77
    {
78
        return Cache::read(
79 28
            (new \ReflectionClass($this))->getName() . '::desc',
80
            function() 
81 28
            {
82 28
                return new ModelDescription($this);
83
            }
84
        );
85 8
    }
86
    
87 28
    public function count()
88
    {
89
        if ($this->dataSet) {
90 10
            return count($this->getData());
91
        } else {
92 10
            return $this->__call('count', []);
93 10
        }
94
    }
95
96
    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...
97
    {
98
        $relationships = $this->getDescription()->getRelationships();
99 12
        if(isset($relationships[$key])) {
100
            return $this->fetchRelatedFields($relationships[$key]);
101 12
        } else {
102 12
            return isset($this->modelData[$key]) ? $this->modelData[$key] : null;
103 8
        }
104
    }
105 4
106
    /**
107
     * Create a new instance of this Model
108
     * @return \ntentan\nibii\RecordWrapper
109
     */
110
    public static function createNew()
111
    {
112
        $class = get_called_class();
113 34
        return InjectionContainer::resolve($class);
114
    }
115 34
116 34
    /**
117
     * @method
118
     * @param type $name
119
     * @param type $arguments
120
     * @return type
121
     */
122
    public function __call($name, $arguments)
123
    {
124
        if($this->dynamicOperations === null) {
125 34
            $this->dynamicOperations = new Operations($this, InjectionContainer::singleton(DriverAdapter::class));
126
        }
127 34
        return $this->dynamicOperations->perform($name, $arguments);
128 34
    }
129 34
130
    public static function __callStatic($name, $arguments)
131 34
    {
132
        return call_user_func_array([self::createNew(), $name], $arguments);
133
    }
134 26
135
    public function __set($name, $value)
136 26
    {
137
        $this->dataSet = true;
138
        $this->modelData[\ntentan\utils\Text::deCamelize($name)] = $value;
139 8
    }
140
141 8
    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...
142 8
    {
143 8
        return $this->retrieveItem(\ntentan\utils\Text::deCamelize($name));
144
    }
145 12
146
    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...
147 12
    {
148
        return $this->table;
149
    }
150 32
    
151
    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...
152 32
    {
153
        foreach($relationships as $name => $relationship) {
154
            $array[$name] = $this->fetchRelatedFields($relationship, $index)->toArray($depth);
155 4
        }
156
        return $array;
157 4
    }
158 4
159 4
    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...
160 4
    {
161
        $relationships = $this->getDescription()->getRelationships();
162
        $array = $this->modelData;
163 16
        if($depth > 0) {
164
            if($this->hasMultipleData()) {
165 16
                foreach($array as $i => $value) {
166 16
                    $array[$i] = $this->expandArrayValue($value, $relationships, $depth - 1, $i);
167 16
                }
168 4
            } else {
169
                $array = $this->expandArrayValue($array, $relationships, $depth - 1);
170
            }
171
        }
172
        return $array;
173 4
    }
174
    
175 4
    public function save()
176 16
    {
177
        $return = $this->__call('save', [$this->hasMultipleData()]);
178
        $this->invalidFields = $this->dynamicOperations->getInvalidFields();
179 10
        return $return;
180
    }
181 10
    
182 10
    public function isValid()
183 10
    {
184
        return $this->__call('isValid', []);
185
    }
186 18
187
    private function hasMultipleData()
188 18
    {
189 16
        if(count($this->modelData) > 0) {
190
            return is_numeric(array_keys($this->modelData)[0]);
191 2
        } else {
192
            return false;
193
        }
194
    }
195 14
196
    public function getData()
197 14
    {
198
        $data = [];
199 14
                
200 6
        if(count($this->modelData) == 0) {
201 14
            $data = $this->modelData;
202 2
        } else if($this->hasMultipleData()) {
203 12
            $data = $this->modelData;
204 12
        } else if(count($this->modelData) > 0) {
205 12
            $data[] = $this->modelData;
206
        }
207 14
        
208
        return $data;
209
    }
210 26
211
    public function setData($data)
212 26
    {
213 26
        $this->dataSet = true;
214 26
        $this->modelData = $data;
215
    }
216
    
217
    public function mergeData($data)
218
    {
219
        foreach($data as $key => $value) {
220
            $this->modelData[$key] = $value;
221
        }
222
        $this->dataSet = true;
223
    }
224 2
225
    public function offsetExists($offset)
226 2
    {
227
        return isset($this->modelData[$offset]);
228
    }
229 2
230
    public function offsetGet($offset)
231 2
    {
232 2
        if (is_numeric($offset)) {
233
            return $this->wrap($offset);
234 2
        } else {
235
            return $this->retrieveItem($offset);
236
        }
237
    }
238 2
239
    public function offsetSet($offset, $value)
240 2
    {
241 2
        $this->dataSet = true;
242 2
        $this->modelData[$offset] = $value;
243
    }
244
245
    public function offsetUnset($offset)
246
    {
247
        unset($this->modelData[$offset]);
248
    }
249 6
250
    private function wrap($offset)
251 6
    {
252 6
        if(isset($this->modelData[$offset])) {
253 6
            $newInstance = $this->createNew();
254 6
            $newInstance->setData($this->modelData[$offset]);
255
            return $newInstance;
256
        } else {
257
            return null;
258
        }
259
    }
260 4
261
    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...
262 4
    {
263
        return $this->invalidFields;
264
    }
265
266
    public function getHasMany()
267
    {
268
        return $this->hasMany;
269
    }
270
271
    public function getBelongsTo()
272
    {
273
        return $this->belongsTo;
274
    }
275 4
276
    public function current()
277 4
    {
278
        return $this->wrap($this->index);
279
    }
280
281
    public function key()
282
    {
283
        return $this->index;
284
    }
285 4
286
    public function next()
287 4
    {
288 4
        $this->index++;
289
    }
290 4
291
    public function rewind()
292 4
    {
293 4
        $this->index = 0;
294
    }
295 4
296
    public function valid()
297 4
    {
298
        return isset($this->modelData[$this->index]);
299
    }
300 12
301
    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...
302 12
    {
303 12
        if($index === null) {
304 12
            $data = $this->modelData;
305
        } else {
306
            $data = $this->modelData[$index];
307 12
        }
308 12
        $model = $relationship->getModelInstance();
309
        if(empty($data)) {
310
            return $model;
311 12
        } else {
312
            return $model->fetch($relationship->getQuery($data));
313
        }
314
    }
315 8
316
    public function getRelationships()
317
    {
318 8
        return [
319 8
            'HasMany' => $this->hasMany,
320 8
            'BelongsTo' => $this->belongsTo,
321 8
            'ManyHaveMany' => $this->manyHaveMany
322
        ];
323
    }
324
    
325
    public function usetField($field)
326
    {
327
        unset($this->modelData[$field]);
328
    }
329 8
    
330
    public function preSaveCallback()
331
    {
332 8
        
333
    }
334 4
    
335
    public function postSaveCallback($id)
336
    {
337 4
        
338
    }
339 2
    
340
    public function preUpdateCallback()
341
    {
342 2
        
343
    }
344 2
    
345
    public function postUpdateCallback()
346
    {
347 2
        
348
    }
349 10
    
350
    public function getBehaviours()
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...
351 10
    {
352
        return $this->loadedDependencies;
353
    }
354
}
355