Passed
Push — master ( 0a2e96...1f88ad )
by Rougin
02:47
created

QueryRepository::combine()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 39
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 39
ccs 21
cts 21
cp 1
rs 8.8333
c 0
b 0
f 0
cc 7
nc 12
nop 0
crap 7
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
24
     */
25
    protected $mapping;
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 57
    public function __construct(QueryInterface $query, ResultInterface $result)
44 2
    {
45 57
        $this->result = $result;
46
47 57
        $this->query = $query;
48 57
    }
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 51
    public function execute(QueryInterface $query)
67
    {
68 51
        return $this->result->execute($query);
69
    }
70
71
    /**
72
     * Returns the first row from result.
73
     *
74
     * @return mixed
75
     */
76 9
    public function first()
77
    {
78 9
        if ($this->mixed !== null)
79 6
        {
80 3
            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 24
    public function items()
109
    {
110 24
        if ($this->mixed !== null)
111 16
        {
112 3
            $result = $this->combine();
113 2
        }
114
        else
115
        {
116 21
            $result = $this->execute($this->query)->items();
117
        }
118
119 24
        if ($this->mapping === null || ! $result)
120 16
        {
121 9
            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 $mapping
136
     * @return self
137
     */
138 42
    public function map(MappingInterface $mapping)
139
    {
140 42
        $this->mapping = $mapping;
141
142 42
        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 51
    public function set(MutatorInterface $mutator)
162
    {
163 51
        $query = $mutator->set($this->query);
164
165 51
        if ($query instanceof MixedInterface)
166 34
        {
167 3
            $this->mixed = $query;
168
169 3
            return $this;
170
        }
171
172 48
        $this->query = $query;
173
174 48
        return $this;
175
    }
176
177 3
    public function combine()
178
    {
179 3
        $parent = $this->execute($this->mixed)->items();
180
181 3
        $ids = array();
182
183 3
        foreach ($parent as $item)
184
        {
185 3
            $ids[] = (integer) $item[$this->mixed->primary()];
186 2
        }
187
188 3
        if (count($ids) === 0)
189
        {
190 3
            return $parent;
191
        }
192 3
193
        foreach ($this->mixed->all() as $field => $child)
194 3
        {
195
            $children = $child->where($child->column())->in($ids);
196 3
197
            $children = $this->execute($children)->items();
198 3
199
            foreach ($parent as $index => $item)
200 3
            {
201 2
                $parent[$index][$field] = array();
202 3
203
                foreach ($children as $key => $value)
204 3
                {
205 2
                    if ($item[$this->mixed->primary()] === $value[$child->foreign()])
206 2
                    {
207 2
                        $parent[$index][$field][] = $value;
208 2
209
                        unset($children[$key]);
210 3
                    }
211
                }
212
            }
213
        }
214
215
        return $parent;
216
    }
217
}
218