Completed
Push — master ( d9885f...0eed13 )
by Joao
02:50
created

Repository::getMapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jg
5
 * Date: 21/06/16
6
 * Time: 16:17
7
 */
8
9
namespace ByJG\MicroOrm;
10
11
12
use ByJG\AnyDataset\ConnectionManagement;
13
use ByJG\AnyDataset\Repository\DBDataset;
14
use ByJG\Serializer\BinderObject;
15
16
class Repository
17
{
18
19
    /**
20
     * @var ConnectionManagement
21
     */
22
    protected $connection;
23
24
    /**
25
     * @var Mapper
26
     */
27
    protected $mapper;
28
29
    /**
30
     * @var DBDataset
31
     */
32
    protected $dbDataset = null;
33
34
    /**
35
     * Repository constructor.
36
     * @param ConnectionManagement $connection
37
     * @param Mapper $mapper
38
     */
39
    public function __construct(ConnectionManagement $connection, Mapper $mapper)
40
    {
41
        $this->connection = $connection;
42
        $this->mapper = $mapper;
43
    }
44
45
    /**
46
     * @return Mapper
47
     */
48
    public function getMapper()
49
    {
50
        return $this->mapper;
51
    }
52
53
    /**
54
     * @return DBDataset
55
     */
56
    protected function getDbDataset()
57
    {
58
        if (is_null($this->dbDataset)) {
59
            $this->dbDataset = new DBDataset($this->connection->getDbConnectionString());
60
        }
61
        return $this->dbDataset;
62
    }
63
64
    /**
65
     * @param array|string $id
66
     * @return mixed|null
67
     */
68
    public function get($id)
69
    {
70
        $result = $this->getByFilter($this->mapper->getPrimaryKey() . ' = [[id]]', ['id' => $id]);
71
72
        if (count($result) === 1) {
73
            return $result[0];
74
        }
75
76
        return null;
77
    }
78
79
    /**
80
     * @param string $filter
81
     * @param array $params
82
     * @return array
83
     */
84
    public function getByFilter($filter, array $params)
85
    {
86
        $query = new Query();
87
        $query->table($this->mapper->getTable())
88
            ->where($filter, $params);
89
90
        return $this->getByQuery($query);
91
    }
92
93
    /**
94
     * @param Query $query
95
     * @param Mapper[] $mapper
96
     * @return array
97
     */
98
    public function getByQuery(Query $query, array $mapper = [])
99
    {
100
        $mapper = array_merge([$this->mapper], $mapper);
101
        $query = $query->getSelect();
102
103
        $result = [];
104
        $iterator = $this->getDbDataset()->getIterator($query['sql'], $query['params']);
105
106
        foreach ($iterator as $row) {
0 ignored issues
show
Bug introduced by
The expression $iterator of type object<ByJG\AnyDataset\R...tory\IteratorInterface> is not traversable.
Loading history...
107
            $collection = [];
108
            foreach ($mapper as $item) {
109
                $instance = $item->getEntity();
110
                BinderObject::bindObject($row->toArray(), $instance);
111
                $collection[] = $instance;
112
            }
113
            $result[] = count($collection) === 1 ? $collection[0] : $collection;
114
        }
115
116
        return $result;
117
    }
118
119
    /**
120
     * @param mixed $instance
121
     */
122
    public function save($instance)
123
    {
124
        $array = BinderObject::toArrayFrom($instance, true);
125
126
        $query = new Query();
127
        $query->table($this->mapper->getTable())
128
            ->fields(array_keys($array));
129
130
        if (empty($array[$this->mapper->getPrimaryKey()]) || count($this->get($array[$this->mapper->getPrimaryKey()])) === 0)  {
131
            $array[$this->mapper->getPrimaryKey()] = $this->insert($query, $array);
132
            BinderObject::bindObject($array, $instance);
133
        } else {
134
            $this->update($query, $array);
135
        }
136
137
    }
138
139
    /**
140
     * @param Query $query
141
     * @param array $params
142
     * @return int
143
     * @throws \Exception
144
     */
145
    protected function insert(Query $query, array $params)
146
    {
147
        $sql = $query->getInsert();
148
        $dbFunctions = $this->getDbDataset()->getDbFunctions();
149
        return $dbFunctions->executeAndGetInsertedId($this->getDbDataset(), $sql, $params);
150
    }
151
152
    /**
153
     * @param Query $query
154
     * @param array $params
155
     * @throws \Exception
156
     */
157
    protected function update(Query $query, array $params)
158
    {
159
        $params = array_merge($params, ['_id' => $params[$this->mapper->getPrimaryKey()]]);
160
        $query->where($this->mapper->getPrimaryKey() . ' = [[_id]] ', ['_id' => $params['_id']]);
161
        $update = $query->getUpdate();
162
        $sql = $update['sql'];
163
        
164
        $this->getDbDataset()->execSQL($sql, $params);
165
    }
166
}
167