Passed
Push — master ( f0e277...a14668 )
by Rougin
03:58
created

QueryRepository::affected()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\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
    public function __construct(QueryInterface $query, ResultInterface $result)
43
    {
44
        $this->result = $result;
45
46
        $this->query = $query;
47
48
        $this->original = $query;
49
    }
50
51
    /**
52
     * Returns a number of affected rows.
53
     *
54
     * @return integer
55
     */
56
    public function affected()
57
    {
58
        return $this->execute()->affected();
59
    }
60
61
    /**
62
     * Returns the first row from result.
63
     *
64
     * @return mixed
65
     */
66
    public function first()
67
    {
68
        return $this->map($this->execute()->first());
69
    }
70
71
    /**
72
     * Returns all items from the result.
73
     *
74
     * @return mixed
75
     */
76
    public function items()
77
    {
78
        $items = array();
79
80
        $result = $this->execute()->items();
81
82
        foreach ($result as $item)
83
        {
84
            $items[] = $this->map($item);
85
        }
86
87
        return $items;
88
    }
89
90
    /**
91
     * Sets the mapper instance.
92
     *
93
     * @param  \Rougin\Windstorm\MapperInterface $mapper
94
     * @return self
95
     */
96
    public function mapper(MapperInterface $mapper)
97
    {
98
        $this->mapper = $mapper;
99
100
        return $this;
101
    }
102
103
    /**
104
     * Mutates the specified query instance.
105
     *
106
     * @param  \Rougin\Windstorm\MutatorInterface $mutator
107
     * @return self
108
     */
109
    public function mutate(MutatorInterface $mutator)
110
    {
111
        $this->query = $mutator->set($this->query);
112
113
        return $this;
114
    }
115
116
    /**
117
     * Executes the result against query instance.
118
     *
119
     * @return \Rougin\Windstorm\ResultInterface
120
     */
121
    protected function execute()
122
    {
123
        return $this->result->execute($this->query);
124
    }
125
126
    /**
127
     * Maps the result data into a mapper instance.
128
     *
129
     * @param  array  $data
130
     * @return mixed
131
     */
132
    protected function map(array $data)
133
    {
134
        return $this->mapper->map($data);
135
    }
136
}
137