ResultAbstract   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 114
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 4 1
A isSelect() 0 4 1
A fetchAll() 0 7 1
A fetchRow() 0 7 1
A fetchCol() 0 11 4
A exceptionIfNotSelect() 0 9 2
A exceptionIfFetchedAlready() 0 9 2
realFetchAll() 0 1 ?
realFetchRow() 0 1 ?
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Db
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Db\Driver;
16
17
use Phossa2\Db\Message\Message;
18
use Phossa2\Shared\Base\ObjectAbstract;
19
use Phossa2\Db\Interfaces\ResultInterface;
20
use Phossa2\Db\Exception\RuntimeException;
21
22
/**
23
 * ResultAbstract
24
 *
25
 * @package Phossa2\Db
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     ObjectAbstract
28
 * @see     ResultInterface
29
 * @version 2.0.0
30
 * @since   2.0.0 added
31
 */
32
abstract class ResultAbstract extends ObjectAbstract implements ResultInterface
33
{
34
    /**
35
     * Fetched already
36
     *
37
     * @var    bool
38
     * @access protected
39
     */
40
    protected $fetched = false;
41
42
    /**
43
     * Desctructor
44
     *
45
     * @access public
46
     */
47
    public function __destruct()
48
    {
49
        $this->close();
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function isSelect()/*# : bool */
56
    {
57
        return 0 !== $this->fieldCount();
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function fetchAll()/*# : array */
64
    {
65
        $this->exceptionIfNotSelect();
66
        $this->exceptionIfFetchedAlready();
67
        $this->fetched = true;
68
        return $this->realFetchAll();
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function fetchRow(/*# int */ $rowCount = 1)/*# : array */
75
    {
76
        $this->exceptionIfNotSelect();
77
        $this->exceptionIfFetchedAlready();
78
        $this->fetched = true;
79
        return $this->realFetchRow($rowCount);
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85
    public function fetchCol($col = 0, $rowCount = 0)/*# : array */
86
    {
87
        $rows = $rowCount ? $this->fetchRow($rowCount) : $this->fetchAll();
88
        $cols = [];
89
        foreach ($rows as $row) {
90
            if (isset($row[$col])) {
91
                $cols[] = $row[$col];
92
            }
93
        }
94
        return $cols;
95
    }
96
97
    /**
98
     * Throw exception if not select query
99
     *
100
     * @throws RuntimeException if not a select query
101
     * @access protected
102
     */
103
    protected function exceptionIfNotSelect()
104
    {
105
        if (!$this->isSelect()) {
106
            throw new RuntimeException(
107
                Message::get(Message::DB_RESULT_NOT_SELECT),
108
                Message::DB_RESULT_NOT_SELECT
109
            );
110
        }
111
    }
112
113
    /**
114
     * Throw exception if fetched already
115
     *
116
     * @throws RuntimeException if fetched already
117
     * @access protected
118
     */
119
    protected function exceptionIfFetchedAlready()
120
    {
121
        if ($this->fetched) {
122
            throw new RuntimeException(
123
                Message::get(Message::DB_RESULT_FETCHED),
124
                Message::DB_RESULT_FETCHED
125
            );
126
        }
127
    }
128
129
    /**
130
     * Driver fetch all
131
     *
132
     * @return array
133
     * @access protected
134
     */
135
    abstract protected function realFetchAll()/*# : array */;
136
137
    /**
138
     * Driver fetch row
139
     *
140
     * @param  int $rowCount number of rows to fetch
141
     * @return array
142
     * @access protected
143
     */
144
    abstract protected function realFetchRow($rowCount)/*# : array */;
145
}
146