Passed
Push — master ( 1908fb...8df30e )
by Rougin
03:01
created

QueryRepository::affected()   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
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\MapperInterface
23
     */
24
    protected $mapper;
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 42
    public function __construct(QueryInterface $query, ResultInterface $result)
43
    {
44 42
        $this->result = $result;
45
46 42
        $this->query = $query;
47
48 42
        $this->original = $query;
49 42
    }
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 3
    public function first()
67
    {
68 3
        return $this->map($this->execute()->first());
69
    }
70
71
    /**
72
     * Returns all items from the result.
73
     *
74
     * @return mixed
75
     */
76 15
    public function items()
77
    {
78 15
        $items = array();
79
80 15
        $result = $this->execute()->items();
81
82 15
        foreach ($result as $item)
83
        {
84 15
            $items[] = $this->map($item);
85 10
        }
86
87 15
        return $items;
88
    }
89
90
    /**
91
     * Sets the mapper instance.
92
     *
93
     * @param  \Rougin\Windstorm\MapperInterface $mapper
94
     * @return self
95
     */
96 27
    public function mapper(MapperInterface $mapper)
97
    {
98 27
        $this->mapper = $mapper;
99
100 27
        return $this;
101
    }
102
103
    /**
104
     * Mutates the specified query instance.
105
     *
106
     * @param  \Rougin\Windstorm\MutatorInterface $mutator
107
     * @return self
108
     */
109 39
    public function mutate(MutatorInterface $mutator)
110
    {
111 39
        $this->query = $mutator->set($this->query);
112
113 39
        return $this;
114
    }
115
116
    /**
117
     * Executes the result against query instance.
118
     *
119
     * @return \Rougin\Windstorm\ResultInterface
120
     */
121 39
    protected function execute()
122
    {
123 39
        return $this->result->execute($this->query);
124
    }
125
126
    /**
127
     * Maps the result data into a mapper instance.
128
     *
129
     * @param  mixed $data
130
     * @return mixed
131
     */
132 18
    protected function map($data)
133
    {
134 18
        if ($this->mapper === null)
135 12
        {
136 6
            return (array) $data;
137
        }
138
139 12
        return $this->mapper->map($data);
140
    }
141
}
142