Completed
Push — master ( 55866d...5ae7b2 )
by Ivan
02:02
created

Mapper   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 56.99%

Importance

Changes 0
Metric Value
wmc 35
dl 0
loc 152
ccs 53
cts 93
cp 0.5699
rs 9.6
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ __construct() 0 5 1
A hp$0 ➔ __lazyProperty() 0 5 1
A hp$0 ➔ __get() 0 15 5
A hp$0 ➔ __set() 0 4 1
B hp$0 ➔ toArray() 0 21 7
A hp$0 ➔ id() 0 8 2
A __construct() 0 4 1
D entity() 0 131 17
A collection() 0 11 1
1
<?php
2
namespace vakata\database\schema;
3
4
use vakata\collection\Collection;
5
use vakata\database\DBInterface;
6
7
class Mapper
8
{
9
    protected $db;
10
    protected $objects;
11
12 2
    public function __construct(DBInterface $db)
13
    {
14 2
        $this->db = $db;
15 2
    }
16 12
    public function entity($definition, array $data)
17
    {
18 12
        $primary = [];
19 12
        foreach ($definition->getPrimaryKey() as $column) {
20 12
            $primary[$column] = $data[$column];
21
        }
22 12
        if (isset($this->objects[$definition->getName()][base64_encode(serialize($primary))])) {
23 12
            return $this->objects[$definition->getName()][base64_encode(serialize($primary))];
24
        }
25
        $entity = new class ($definition, $data) extends \StdClass {
26
            protected $definition;
27
            protected $initial = [];
28
            protected $changed = [];
29
            protected $fetched = [];
30
31 6
            public function __construct($definition, array $data = [])
32
            {
33 6
                $this->definition = $definition;
34 6
                $this->initial = $data;
35 6
            }
36 6
            public function __lazyProperty(string $property, callable $resolve)
37
            {
38 6
                $this->fetched[$property] = $resolve;
39 6
                return $this;
40
            }
41 12
            public function __get($property)
42
            {
43 12
                if (isset($this->changed[$property])) {
44
                    return $this->changed[$property];
45
                }
46 12
                if (isset($this->initial[$property])) {
47 12
                    return $this->initial[$property];
48
                }
49 6
                if (isset($this->fetched)) {
50 6
                    return is_callable($this->fetched[$property]) ?
51 2
                        $this->fetched[$property] = call_user_func($this->fetched[$property]) :
52 6
                        $this->fetched[$property];
53
                }
54
                return null;
55
            }
56
            public function __set($property, $value)
57
            {
58
                $this->changed[$property] = $value;
59
            }
60
            public function toArray(bool $fetch = false)
61
            {
62
                $data = [];
63
                foreach ($this->definition->getColumns() as $k) {
64
                    if (isset($this->fetched[$k])) {
65
                        if ($fetch) {
66
                            $this->fetched[$k] = call_user_func($this->fetched[$k]);
67
                        }
68
                        if (!is_callable($this->fetched[$k])) {
69
                            $data[$k] = $this->fetched[$k];
70
                        }
71
                    }
72
                    if (isset($this->initial[$k])) {
73
                        $data[$k] = $this->initial[$k];
74
                    }
75
                    if (isset($this->changed[$k])) {
76
                        $data[$k] = $this->changed[$k];
77
                    }
78
                }
79
                return $data;
80
            }
81
            public function id()
82
            {
83
                $primary = [];
84
                foreach ($this->definition->getPrimaryKey() as $k) {
85
                    $primary[$k] = $this->{$k};
86
                }
87
                return $primary;
88
            }
89
        };
90 6
        foreach ($definition->getColumns() as $column) {
91 6
            if (!isset($data[$column])) {
92 6
                $entity->__lazyProperty($column, function () use ($entity, $definition, $primary, $column) {
93
                    $query = $this->db->table($definition->getName());
94
                    foreach ($primary as $k => $v) {
95
                        $query->filter($k, $v);
96
                    }
97
                    return $query->select([$column])[0][$column] ?? null;
98 6
                });
99
            }
100
        }
101 6
        foreach ($definition->getRelations() as $name => $relation) {
102 6
            if (isset($data[$name])) {
103
                $entity->{$name} = $relation->many ? 
104
                    array_map(function ($v) use ($relation) {
105
                        return $this->entity($relation->table, $v);
106
                    }, $data[$name]) :
107
                    $this->entity($relation->table, $data[$name]);
108
            } else {
109 6
                $entity->__lazyProperty($name, function () use ($entity, $definition, $primary, $relation, $data) {
110 2
                    $query = $this->db->table($relation->table->getName(), true);
0 ignored issues
show
Unused Code introduced by
The call to DBInterface::table() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
111 2
                    if ($relation->sql) {
112
                        $query->where($relation->sql, $relation->par);
113
                    }
114 2
                    if ($relation->pivot) {
115 2
                        $nm = null;
116 2
                        foreach ($relation->table->getRelations() as $rname => $rdata) {
117 2
                            if ($rdata->pivot && $rdata->pivot->getName() === $relation->pivot->getName()) {
118 2
                                $nm = $rname;
119
                            }
120
                        }
121 2
                        if (!$nm) {
122
                            $nm = $definition->getName();
123
                            $relation->table->manyToMany(
124
                                $this->db->table($definition->getName()),
125
                                $relation->pivot,
126
                                $nm,
127
                                array_flip($relation->keymap),
128
                                $relation->pivot_keymap
129
                            );
130
                        }
131 2
                        foreach ($definition->getPrimaryKey() as $v) {
132 2
                            $query->filter($nm . '.' . $v, $data[$v] ?? null);
133
                        }
134
                    } else {
135 2
                        foreach ($relation->keymap as $k => $v) {
136 2
                            $query->filter($v, $entity->{$k} ?? null);
137
                        }
138
                    }
139 2
                    return $relation->many ?
140 2
                        $query->iterator() :
141 2
                        $query[0];
142 6
                });
143
            }
144
        }
145 6
        return $this->objects[$definition->getName()][base64_encode(serialize($primary))] = $entity;
146
    }
147 12
    public function collection($iterator, $definition)
148
    {
149 12
        return Collection::from($iterator)
150
            // ->mapKey(function ($v, $k) use ($definition) {
151
            //     $pk = $definition->getPrimaryKey();
152
            //     return count($pk) === 1 ? ($v[$pk[0]] ?? $k) : $k;
153
            // })
154 12
            ->map(function ($v) use ($definition) {
155 12
                return $this->entity($definition, $v);
156 12
            });
157
    }
158
}