Completed
Push — master ( d6fc27...dc9f7f )
by Hung
19s
created

ResultSetAdapter   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 131
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAffectedRows() 0 4 1
A getNumRows() 0 4 1
A getFields() 0 4 1
A isDml() 0 4 1
A store() 0 5 1
A toRow() 0 4 1
A freeResult() 0 4 1
A rewind() 0 5 1
A valid() 0 4 1
A fetch() 0 14 3
A fetchAll() 0 14 3
1
<?php
2
3
namespace Foolz\SphinxQL\Drivers\Mysqli;
4
5
class ResultSetAdapter implements \Foolz\SphinxQL\Drivers\ResultSetAdapterInterface
6
{
7
    /**
8
     * @var Connection|null
9
     */
10
    protected $connection = null;
11
12
    /**
13
     * @var \mysqli_result|null
14
     */
15
    protected $result = null;
16
17
    /**
18
     * @var bool
19
     */
20
    protected $valid = true;
21
22
    /**
23
     * @param Connection $connection
24
     * @param null|\mysqli_result $result
25
     */
26
    public function __construct(Connection $connection, $result = null)
27
    {
28
        $this->connection = $connection;
29
        $this->result = $result;
30
    }
31
32
    /**
33
     * @return mixed
34
     * @throws \Foolz\SphinxQL\Exception\ConnectionException
35
     */
36
    public function getAffectedRows()
37
    {
38
        return $this->connection->getConnection()->affected_rows;
39
    }
40
41
    /**
42
     * @return int
43
     */
44
    public function getNumRows()
45
    {
46
        return $this->result->num_rows;
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function getFields()
53
    {
54
        return $this->result->fetch_fields();
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    public function isDml()
61
    {
62
        return !($this->result instanceof \mysqli_result);
63
    }
64
65
    /**
66
     * @return mixed
67
     */
68
    public function store()
69
    {
70
        $this->result->data_seek(0);
71
        return $this->result->fetch_all(MYSQLI_NUM);
72
    }
73
74
    /**
75
     * @param int $num
76
     */
77
    public function toRow($num)
78
    {
79
        $this->result->data_seek($num);
80
    }
81
82
    public function freeResult()
83
    {
84
        $this->result->free_result();
85
    }
86
87
    public function rewind()
88
    {
89
        $this->valid = true;
90
        $this->result->data_seek(0);
91
    }
92
93
    public function valid()
94
    {
95
        return $this->valid;
96
    }
97
98
    /**
99
     * @param self::FETCH_ASSOC|self::FETCH_NUM $fetch_type
0 ignored issues
show
Documentation introduced by
The doc-type self::FETCH_ASSOC|self::FETCH_NUM could not be parsed: Unknown type name "self::FETCH_ASSOC" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
100
     * @return array|null
101
     */
102
    public function fetch($fetch_type)
103
    {
104
        if ($fetch_type == self::FETCH_ASSOC) {
105
            $row = $this->result->fetch_assoc();
106
        } else {
107
            $row = $this->result->fetch_row();
108
        }
109
110
        if (!$row) {
111
            $this->valid = false;
112
        }
113
114
        return $row;
115
    }
116
117
    /**
118
     * @param self::FETCH_ASSOC|self::FETCH_NUM $fetch_type
0 ignored issues
show
Documentation introduced by
The doc-type self::FETCH_ASSOC|self::FETCH_NUM could not be parsed: Unknown type name "self::FETCH_ASSOC" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
119
     * @return array
120
     */
121
    public function fetchAll($fetch_type)
122
    {
123
        if ($fetch_type == self::FETCH_ASSOC) {
124
            $row = $this->result->fetch_all(MYSQLI_ASSOC);
125
        } else {
126
            $row = $this->result->fetch_all(MYSQLI_NUM);
127
        }
128
129
        if (empty($row)) {
130
            $this->valid = false;
131
        }
132
133
        return $row;
134
    }
135
}
136