Result::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Nip\Database;
4
5
use Nip\Database\Adapters\AbstractAdapter;
6
use Nip\Database\Adapters\MySQLi;
7
use Nip\Database\Query\AbstractQuery;
8
9
/**
10
 * Class Result
11
 * @package Nip\Database
12
 */
13
class Result
14
{
15
    /**
16
     * @var \mysqli_result
17
     */
18
    protected $resultSQL;
19
20
    /**
21
     * @var AbstractAdapter| MySQLi
22
     */
23
    protected $adapter;
24
25
    /**
26
     * @var AbstractQuery
27
     */
28
    protected $query;
29
30
    protected $results = [];
31
32
    /**
33
     * Result constructor.
34
     *
35
     * @param \mysqli_result  $resultSQL
36
     * @param AbstractAdapter $adapter
37
     */
38
    public function __construct($resultSQL, $adapter)
39
    {
40
        $this->resultSQL = $resultSQL;
41
        $this->adapter = $adapter;
42
    }
43
44
    public function __destruct()
45
    {
46
        if ($this->resultSQL && !is_bool($this->resultSQL)) {
47
            $this->getAdapter()->freeResults($this->resultSQL);
0 ignored issues
show
Bug introduced by
The method freeResults() does not exist on Nip\Database\Adapters\AbstractAdapter. Since it exists in all sub-types, consider adding an abstract or default implementation to Nip\Database\Adapters\AbstractAdapter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            $this->getAdapter()->/** @scrutinizer ignore-call */ freeResults($this->resultSQL);
Loading history...
48
        }
49
    }
50
51
    /**
52
     * @return AbstractAdapter|MySQLi
53
     */
54
    public function getAdapter()
55
    {
56
        return $this->adapter;
57
    }
58
59
    /**
60
     * @param AbstractAdapter|MySQLi $adapter
61
     */
62
    public function setAdapter($adapter)
63
    {
64
        $this->adapter = $adapter;
65
    }
66
67
    /**
68
     * Fetches all rows from current result set.
69
     *
70
     * @return array
71
     */
72
    public function fetchResults()
73
    {
74
        if (count($this->results) == 0) {
75
            while ($result = $this->fetchResult()) {
76
                $this->results[] = $result;
77
            }
78
        }
79
80
        return $this->results;
81
    }
82
83
    /**
84
     * Fetches row from current result set.
85
     *
86
     * @return bool|array
87
     */
88
    public function fetchResult()
89
    {
90
        if ($this->checkValid()) {
91
            try {
92
                return $this->getAdapter()->fetchAssoc($this->resultSQL);
0 ignored issues
show
Bug introduced by
The method fetchAssoc() does not exist on Nip\Database\Adapters\AbstractAdapter. Since it exists in all sub-types, consider adding an abstract or default implementation to Nip\Database\Adapters\AbstractAdapter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
                return $this->getAdapter()->/** @scrutinizer ignore-call */ fetchAssoc($this->resultSQL);
Loading history...
93
            } catch (Exception $e) {
94
                $e->log();
0 ignored issues
show
Bug introduced by
The method log() does not exist on Nip\Database\Exception. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
                $e->/** @scrutinizer ignore-call */ 
95
                    log();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
            }
96
        }
97
98
        return false;
99
    }
100
101
    /**
102
     * @return bool
103
     */
104
    public function checkValid()
105
    {
106
        if (!$this->isValid()) {
107
            trigger_error("Invalid result for query [" . $this->getQuery()->getString() . "]", E_USER_WARNING);
108
109
            return false;
110
        }
111
112
        return true;
113
    }
114
115
    /**
116
     * @return bool
117
     */
118
    public function isValid()
119
    {
120
        return $this->resultSQL !== false && $this->resultSQL !== null;
121
    }
122
123
    /**
124
     * @return AbstractQuery
125
     */
126
    public function getQuery()
127
    {
128
        return $this->query;
129
    }
130
131
    /**
132
     * @param AbstractQuery $query
133
     */
134
    public function setQuery($query)
135
    {
136
        $this->query = $query;
137
    }
138
139
    /**
140
     * @return bool|int
141
     */
142
    public function numRows()
143
    {
144
        if ($this->checkValid()) {
145
            return $this->getAdapter()->numRows($this->resultSQL);
0 ignored issues
show
Bug introduced by
The method numRows() does not exist on Nip\Database\Adapters\AbstractAdapter. Since it exists in all sub-types, consider adding an abstract or default implementation to Nip\Database\Adapters\AbstractAdapter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
            return $this->getAdapter()->/** @scrutinizer ignore-call */ numRows($this->resultSQL);
Loading history...
146
        }
147
148
        return false;
149
    }
150
}
151