Completed
Pull Request — master (#2)
by Joao
05:22
created

Oci8Iterator::__destruct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
namespace ByJG\AnyDataset\Dataset;
4
5
use ByJG\AnyDataset\Exception\IteratorException;
6
7
class Oci8Iterator extends GenericIterator
8
{
9
10
    const RECORD_BUFFER = 50;
11
12
    private $rowBuffer;
13
    protected $currentRow = 0;
14
    protected $moveNextRow = 0;
15
16
    /**
17
     * @var resource Cursor
18
     */
19
    private $cursor;
20
21
    /**
22
     *
23
     * @param resource $cursor
24
     */
25
    public function __construct($cursor)
26
    {
27
        $this->cursor = $cursor;
28
        $this->rowBuffer = array();
29
    }
30
31
    /**
32
     * @access public
33
     * @return int
34
     */
35
    public function count()
36
    {
37
        return -1;
38
    }
39
40
    /**
41
     * @access public
42
     * @return bool
43
     */
44
    public function hasNext()
45
    {
46
        if (count($this->rowBuffer) >= Oci8Iterator::RECORD_BUFFER) {
47
            return true;
48
        }
49
50
        if (is_null($this->cursor)) {
51
            return (count($this->rowBuffer) > 0);
52
        }
53
54
        $rowArray = oci_fetch_array($this->cursor, OCI_ASSOC + OCI_RETURN_NULLS);
55
        if (!empty($rowArray)) {
56
            $rowArray = array_change_key_case($rowArray, CASE_LOWER);
57
            $singleRow = new Row($rowArray);
58
59
            $this->currentRow++;
60
61
            // Enfileira o registo
62
            array_push($this->rowBuffer, $singleRow);
63
            // Traz novos até encher o Buffer
64
            if (count($this->rowBuffer) < DbIterator::RECORD_BUFFER) {
65
                $this->hasNext();
66
            }
67
            return true;
68
        }
69
70
        oci_free_statement($this->cursor);
71
        $this->cursor = null;
72
        return (count($this->rowBuffer) > 0);
73
    }
74
75
    public function __destruct()
76
    {
77
        if (!is_null($this->cursor)) {
78
            oci_free_statement($this->cursor);
79
            $this->cursor = null;
80
        }
81
    }
82
83
    /**
84
     * @return mixed
85
     * @throws IteratorException
86
     */
87 View Code Duplication
    public function moveNext()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        if (!$this->hasNext()) {
90
            throw new IteratorException("No more records. Did you used hasNext() before moveNext()?");
91
        } else {
92
            $sr = array_shift($this->rowBuffer);
93
            $this->moveNextRow++;
94
            return $sr;
95
        }
96
    }
97
98
    public function key()
99
    {
100
        return $this->moveNextRow;
101
    }
102
}
103