Passed
Push — master ( f42cdc...9659cc )
by Rougin
04:09
created

QueryRepository::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Rougin\Windstorm;
4
5
use Rougin\Windstorm\QueryInterface;
6
use Rougin\Windstorm\ResultInterface;
7
8
/**
9
 * Query Repository
10
 *
11
 * @package Windstorm
12
 * @author  Rougin Gutib <[email protected]>
13
 */
14
class QueryRepository
15
{
16
    /**
17
     * @var \Rougin\Windstorm\ResultInterface
18
     */
19
    protected $result;
20
21
    /**
22
     * @var \Rougin\Windstorm\MappingInterface
23
     */
24
    protected $mapping;
25
26
    /**
27
     * @var \Rougin\Windstorm\QueryInterface
28
     */
29
    protected $original;
30
31
    /**
32
     * @var \Rougin\Windstorm\QueryInterface
33
     */
34
    protected $query;
35
36
    /**
37
     * Initializes the repository instance.
38
     *
39
     * @param \Rougin\Windstorm\QueryInterface  $query
40
     * @param \Rougin\Windstorm\ResultInterface $result
41
     */
42 63
    public function __construct(QueryInterface $query, ResultInterface $result)
43
    {
44 63
        $this->result = $result;
45
46 63
        $this->query = $query;
47
48 63
        $this->original = $query;
49 63
    }
50
51
    /**
52
     * Returns a number of affected rows.
53
     *
54
     * @return integer
55
     */
56 21
    public function affected()
57
    {
58 21
        return $this->execute()->affected();
59
    }
60
61
    /**
62
     * Returns the first row from result.
63
     *
64
     * @return mixed
65
     */
66 9
    public function first()
67
    {
68 9
        $item = $this->execute()->first();
69
70 9
        if ($this->mapping)
71 6
        {
72 6
            return $this->mapping->map($item);
73
        }
74
75 3
        return $item;
76
    }
77
78
    /**
79
     * Returns all items from the result.
80
     *
81
     * @return mixed
82
     */
83 27
    public function items()
84
    {
85 27
        $result = $this->execute()->items();
86
87 27
        if ($this->mapping === null)
88 18
        {
89 6
            return $result;
90
        }
91
92 21
        foreach ($result as $key => $item)
93
        {
94 21
            $result[$key] = $this->mapping->map($item);
95 14
        }
96
97 21
        return $result;
98
    }
99
100
    /**
101
     * Sets the mapping instance.
102
     *
103
     * @param  \Rougin\Windstorm\MappingInterface $mapping
104
     * @return self
105
     */
106 48
    public function map(MappingInterface $mapping)
107
    {
108 48
        $this->mapping = $mapping;
109
110 48
        return $this;
111
    }
112
113
    /**
114
     * Returns the query instance.
115
     *
116
     * @return \Rougin\Windstorm\QueryRepository
117
     */
118 3
    public function query()
119
    {
120 3
        return clone $this->query;
121
    }
122
123
    /**
124
     * Mutates the specified query instance.
125
     *
126
     * @param  \Rougin\Windstorm\MutatorInterface $mutator
127
     * @return self
128
     */
129 57
    public function set(MutatorInterface $mutator)
130
    {
131 57
        $this->query = $mutator->set($this->query);
132
133 57
        return $this;
134
    }
135
136
    /**
137
     * Executes the result against query instance.
138
     *
139
     * @return \Rougin\Windstorm\ResultInterface
140
     */
141 57
    protected function execute()
142
    {
143 57
        return $this->result->execute($this->query);
144
    }
145
}
146