ModelResultset   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 66
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A initialize() 0 8 2
A getPrototype() 0 4 1
A current() 0 10 2
1
<?php
2
3
namespace NwLaravel\Resultset;
4
5
use PDOStatement;
6
use RuntimeException;
7
use \Illuminate\Database\Eloquent\Model as EloquentModel;
8
9
/**
10
 * Class ModelResultset
11
 */
12
class ModelResultset extends AbstractResultset
13
{
14
    /**
15
     * @var \Illuminate\Database\Eloquent\Model
16
     */
17
    protected $model = null;
18
19
    /**
20
     * @var \Illuminate\Database\Eloquent\Model
21
     */
22
    private $prototype = null;
23
24
    /**
25
     * Initialize
26
     *
27
     * @param PDOStatement  $statement PDO Statement
28
     * @param EloquentModel $model     Model
29
     */
30 3
    public function __construct(PDOStatement $statement, EloquentModel $model)
31
    {
32 3
        $this->statement = $statement;
33 3
        $this->model = $model;
34
35 3
        $this->initialize();
36 3
    }
37
38
    /**
39
     * Initialize
40
     *
41
     * @return void
42
     */
43 3
    public function initialize()
44
    {
45 3
        if (! $this->initialized) {
46 3
            $this->prototype = $this->model->newFromBuilder();
47 3
        }
48
49 3
        $this->initialized = true;
50 3
    }
51
52
    /**
53
     * Get Prototype
54
     *
55
     * @return Illuminate\Database\Eloquent\Model
56
     */
57 1
    public function getPrototype()
58
    {
59 1
        return $this->prototype;
60
    }
61
62
    /**
63
     * Get the data
64
     *
65
     * @return object|bool
66
     */
67 2
    public function current()
68
    {
69 2
        if (!$data = parent::current()) {
70 1
            return false;
71
        }
72
73 1
        $newInstance = clone $this->getPrototype();
74 1
        $newInstance->setRawAttributes((array) $data, true);
75 1
        return $newInstance;
76
    }
77
}
78