QueryRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Rougin\Windstorm;
4
5
use Rougin\Windstorm\MixedInterface;
6
use Rougin\Windstorm\QueryInterface;
7
use Rougin\Windstorm\ResultInterface;
8
9
/**
10
 * Query Repository
11
 *
12
 * @package Windstorm
13
 * @author  Rougin Gutib <[email protected]>
14
 */
15
class QueryRepository
16
{
17
    /**
18
     * @var \Rougin\Windstorm\ResultInterface
19
     */
20
    protected $result;
21
22
    /**
23
     * @var \Rougin\Windstorm\MappingInterface|null
24
     */
25
    protected $mapping = null;
26
27
    /**
28
     * @var \Rougin\Windstorm\MixedInterface
29
     */
30
    protected $mixed = null;
31
32
    /**
33
     * @var \Rougin\Windstorm\QueryInterface
34
     */
35
    protected $query;
36
37
    /**
38
     * Initializes the repository instance.
39
     *
40
     * @param \Rougin\Windstorm\QueryInterface  $query
41
     * @param \Rougin\Windstorm\ResultInterface $result
42
     */
43 54
    public function __construct(QueryInterface $query, ResultInterface $result)
44
    {
45 54
        $this->result = $result;
46
47 54
        $this->query = $query;
48 54
    }
49
50
    /**
51
     * Returns a number of affected rows.
52
     *
53
     * @return integer
54
     */
55 21
    public function affected()
56
    {
57 21
        return $this->execute($this->query)->affected();
58
    }
59
60
    /**
61
     * Executes the result against query instance.
62
     *
63
     * @param  \Rougin\Windstorm\QueryInterface $query
64
     * @return \Rougin\Windstorm\ResultInterface
65
     */
66 48
    public function execute(QueryInterface $query)
67
    {
68 48
        return $this->result->execute($query);
69
    }
70
71
    /**
72
     * Returns the first row from result.
73
     *
74
     * @return mixed
75
     */
76 6
    public function first()
77
    {
78 6
        if ($this->mixed !== null)
79 4
        {
80
            return current($this->items());
81
        }
82
83 6
        $item = $this->execute($this->query)->first();
84
85 6
        if ($this->mapping && $item)
86 4
        {
87 3
            return $this->mapping->map($item);
88
        }
89
90 3
        return $item;
91
    }
92
93
    /**
94
     * Returns the last inserted ID.
95
     *
96
     * @return integer
97
     */
98
    public function inserted()
99
    {
100
        return $this->execute($this->query)->inserted();
101
    }
102
103
    /**
104
     * Returns all items from the result.
105
     *
106
     * @return mixed
107
     */
108 21
    public function items()
109
    {
110 21
        if ($this->mixed !== null)
111 14
        {
112
            $result = $this->combine();
113
        }
114
        else
115
        {
116 21
            $result = $this->execute($this->query)->items();
117
        }
118
119 21
        if ($this->mapping === null || ! $result)
120 14
        {
121 6
            return $result;
122
        }
123
124 15
        foreach ($result as $key => $item)
125
        {
126 15
            $result[$key] = $this->mapping->map($item);
127 10
        }
128
129 15
        return $result;
130
    }
131
132
    /**
133
     * Sets the mapping instance.
134
     *
135
     * @param  \Rougin\Windstorm\MappingInterface|null $mapping
136
     * @return self
137
     */
138 39
    public function map(MappingInterface $mapping = null)
139
    {
140 39
        $this->mapping = $mapping;
141
142 39
        return $this;
143
    }
144
145
    /**
146
     * Returns the query instance.
147
     *
148
     * @return \Rougin\Windstorm\QueryRepository
149
     */
150 3
    public function query()
151
    {
152 3
        return clone $this->query;
153
    }
154
155
    /**
156
     * Mutates the specified query instance.
157
     *
158
     * @param  \Rougin\Windstorm\MutatorInterface $mutator
159
     * @return self
160
     */
161 48
    public function set(MutatorInterface $mutator)
162
    {
163 48
        $query = $mutator->set($this->query);
164
165 48
        if ($query instanceof MixedInterface)
166 32
        {
167
            $this->mixed = $query;
168
169
            return $this;
170
        }
171
172 48
        $this->query = $query;
173
174 48
        return $this;
175
    }
176
177
    public function combine()
178
    {
179
        $parent = $this->execute($this->mixed)->items();
180
181
        $ids = array();
182
183
        foreach ($parent as $item)
184
        {
185
            $ids[] = (integer) $item[$this->mixed->primary()];
186
        }
187
188
        if (count($ids) === 0)
189
        {
190
            return $parent;
191
        }
192
193
        foreach ($this->mixed->all() as $field => $child)
194
        {
195
            $children = $child->andWhere($child->column())->in($ids);
196
197
            $children = $this->execute($children)->items();
198
199
            foreach ($parent as $index => $item)
200
            {
201
                $parent[$index][$field] = array();
202
203
                foreach ($children as $key => $value)
204
                {
205
                    if ($item[$this->mixed->primary()] === $value[$child->foreign()])
206
                    {
207
                        $parent[$index][$field][] = $value;
208
209
                        // unset($children[$key]);
210
                    }
211
                }
212
            }
213
        }
214
215
        return $parent;
216
    }
217
}
218