Completed
Push — master ( 304183...e1273d )
by Ryuichi
16:29
created

Result   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Test Coverage

Coverage 33.9%

Importance

Changes 0
Metric Value
dl 0
loc 213
ccs 20
cts 59
cp 0.339
rs 10
c 0
b 0
f 0
wmc 25

15 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 6 2
A seek() 0 11 3
A key() 0 3 1
A offsetSet() 0 3 1
A offsetUnSet() 0 3 1
A current() 0 7 3
A count() 0 14 3
A valid() 0 3 1
A offsetExists() 0 7 2
A toEntity() 0 6 1
A toArray() 0 7 1
A rewind() 0 6 2
A __construct() 0 5 1
A __destruct() 0 4 1
A offsetGet() 0 7 2
1
<?php
2
namespace WebStream\Database;
3
4
use WebStream\DI\Injector;
0 ignored issues
show
Bug introduced by
The type WebStream\DI\Injector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use WebStream\Exception\Extend\CollectionException;
0 ignored issues
show
Bug introduced by
The type WebStream\Exception\Extend\CollectionException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
/**
8
 * Result
9
 * @author Ryuichi TANAKA.
10
 * @since 2013/12/14
11
 * @version 0.7
12
 */
13
class Result implements \Iterator, \SeekableIterator, \ArrayAccess, \Countable
14
{
15
    use Injector;
16
17
    /**
18
     * @var Doctrine\DBAL\Statement ステートメント
0 ignored issues
show
Bug introduced by
The type WebStream\Database\Doctrine\DBAL\Statement was not found. Did you mean Doctrine\DBAL\Statement? If so, make sure to prefix the type with \.
Loading history...
19
     */
20
    private $stmt;
21
22
    /**
23
     * @var array<mixed> 列データ
24
     */
25
    private $row;
26
27
    /**
28
     * @var array<mixed> キャッシュ化列データ
29
     */
30
    private $rowCache;
31
32
    /**
33
     * @var int インデックス位置
34
     */
35
    private $position;
36
37
    /**
38
     * コンストラクタ
39
     * @param Doctrine\DBAL\Driver\Statement ステートメントオブジェクト
40
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment ステートメントオブジェクト at position 0 could not be parsed: Unknown type name 'ステートメントオブジェクト' at position 0 in ステートメントオブジェクト.
Loading history...
41 27
    public function __construct(\Doctrine\DBAL\Driver\Statement $stmt)
42
    {
43 27
        $this->stmt = $stmt;
0 ignored issues
show
Documentation Bug introduced by
It seems like $stmt of type Doctrine\DBAL\Driver\Statement is incompatible with the declared type WebStream\Database\Doctrine\DBAL\Statement of property $stmt.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44 27
        $this->position = 0;
45 27
        $this->rowCache = [];
46
    }
47
48
    /**
49
     * デストラクタ
50
     */
51 27
    public function __destruct()
52
    {
53 27
        $this->stmt = null;
54 27
        $this->rowCache = null;
55
    }
56
57
    /**
58
     * Implements Countable#count
59
     * @return integer 結果件数
60
     */
61
    public function count()
62
    {
63
        $count = 0;
64
        if ($this->stmt === null) {
65
            $count = count($this->rowCache);
66
        } else {
67
            $count = $this->stmt->rowCount();
68
            if ($count === 0) {
69
                $this->toArray();
70
                $count = count($this->rowCache);
71
            }
72
        }
73
74
        return $count;
75
    }
76
77
    /**
78
     * Implements SeekableIterator#seek
79
     * カーソル位置を移動する
80
     * @param mixed オフセット
81
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment オフセット at position 0 could not be parsed: Unknown type name 'オフセット' at position 0 in オフセット.
Loading history...
82
    public function seek($offset)
83
    {
84
        if ($this->stmt !== null) {
85
            // Mysql does not support scrollable cursor.
86
            // but if statement to array, it is accessable.
87
            $this->toArray();
88
        }
89
        if (array_key_exists($offset, $this->rowCache)) {
90
            return $this->rowCache[$offset];
91
        } else {
92
            throw new \OutOfBoundsException("Current cursor is out of range: " . $offset);
93
        }
94
    }
95
96
    /**
97
     * Implements Iterator#current
98
     * 現在の要素を返却する
99
     * @return array<string> 列データ
100
     */
101
    public function current()
102
    {
103
        if ($this->stmt === null) {
104
            return array_key_exists($this->position, $this->rowCache) ? $this->rowCache[$this->position] : null;
105
        }
106
107
        return $this->row;
108
    }
109
110
    /**
111
     * Implements Iterator#key
112
     * 現在の要素のキーを返却する
113
     * @return integer キー
114
     */
115
    public function key()
116
    {
117
        return $this->position;
118
    }
119
120
    /**
121
     * Implements Iterator#next
122
     * 次の要素に進む
123
     */
124
    public function next()
125
    {
126
        if ($this->stmt !== null) {
127
            $this->row = $this->stmt->fetch(\PDO::FETCH_ASSOC);
128
        }
129
        $this->position++;
130
    }
131
132
    /**
133
     * Implements Iterator#rewind
134
     * イテレータを先頭に巻き戻す
135
     */
136
    public function rewind()
137
    {
138
        if ($this->stmt !== null) {
139
            $this->row = $this->stmt->fetch(\PDO::FETCH_ASSOC, \PDO::FETCH_ORI_FIRST);
140
        }
141
        $this->position = 0;
142
    }
143
144
    /**
145
     * Implements Iterator#valid
146
     * 現在位置が有効かどうかを調べる
147
     * @return boolean 有効かどうか
148
     */
149
    public function valid()
150
    {
151
        return $this->row !== false;
152
    }
153
154
    /**
155
     * Implements ArrayAccess#offsetExists
156
     * オフセットの位置に値が存在するかどうか返却する
157
     * @return boolean 値が存在するかどうか
158
     */
159
    public function offsetExists($offset)
160
    {
161
        if ($this->stmt !== null) {
162
            $this->toArray();
163
        }
164
165
        return array_key_exists($offset, $this->rowCache);
166
    }
167
168
    /**
169
     * Implements ArrayAccess#offsetGet
170
     * オフセットの位置の値を返却する
171
     * @return mixed 値
172
     */
173 6
    public function offsetGet($offset)
174
    {
175 6
        if ($this->stmt !== null) {
176 6
            $this->toArray();
177
        }
178
179 6
        return $this->rowCache[$offset];
180
    }
181
182
    /**
183
     * Implements ArrayAccess#offsetSet
184
     * オフセットの位置に値を設定する
185
     * @param mixed オフセット
186
     * @param mixed 値
187
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment オフセット at position 0 could not be parsed: Unknown type name 'オフセット' at position 0 in オフセット.
Loading history...
188
    public function offsetSet($offset, $value)
189
    {
190
        throw new CollectionException("Database results are read only.");
191
    }
192
193
    /**
194
     * Implements ArrayAccess#offsetUnSet
195
     * オフセットの設定を解除する
196
     * @param mixed オフセット
197
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment オフセット at position 0 could not be parsed: Unknown type name 'オフセット' at position 0 in オフセット.
Loading history...
198
    public function offsetUnSet($offset)
199
    {
200
        throw new CollectionException("Database results are read only.");
201
    }
202
203
    /**
204
     * 検索結果を全て配列として返却する
205
     * @return array<string> 検索結果
206
     */
207 15
    public function toArray()
208
    {
209 15
        $this->rowCache = $this->stmt->fetchAll(\PDO::FETCH_ASSOC);
210 15
        $this->logger->debug("All results to array and cached.");
211 15
        $this->stmt = null;
212
213 15
        return $this->rowCache;
214
    }
215
216
    /**
217
     * 検索結果をエンティティとして返却する
218
     * @return ResultEntity 検索結果
219
     */
220 12
    public function toEntity($classpath)
221
    {
222 12
        $resultEntity = new ResultEntity($this->stmt->getWrappedStatement(), $classpath);
223 12
        $resultEntity->inject('logger', $this->logger)->initialize();
224
225 12
        return $resultEntity;
226
    }
227
}
228