Select   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setReturnType() 0 4 1
A obtainPdoFetchType() 0 7 2
A getReturnType() 0 3 1
A fetchAll() 0 6 2
A fetchRow() 0 7 3
1
<?php
2
3
namespace BfwSql\Executers;
4
5
use \PDO;
6
7
class Select extends Common
8
{
9
    /**
10
     * @var string $returnType PHP Type used for return result
11
     */
12
    protected $returnType = '';
13
    
14
    /**
15
     * Getter accessor to returnType property
16
     * 
17
     * @return string
18
     */
19
    public function getReturnType(): string
20
    {
21
        return $this->returnType;
22
    }
23
    
24
    /**
25
     * Setter accessor to property returnType
26
     * 
27
     * @param string $returnType The new return type value
28
     * 
29
     * @return $this
30
     */
31
    public function setReturnType(string $returnType): self
32
    {
33
        $this->returnType = $returnType;
34
        return $this;
35
    }
36
    
37
    /**
38
     * Return the PDO constant for the returnType declared
39
     * 
40
     * @return integer
41
     */
42
    protected function obtainPdoFetchType(): int
43
    {
44
        if ($this->returnType === 'object') {
45
            return PDO::FETCH_OBJ;
46
        }
47
        
48
        return PDO::FETCH_ASSOC;
49
    }
50
    
51
    /**
52
     * Fetch one row of the result
53
     * 
54
     * @param bool $reexecute (default false) To reexecute the request, or just
55
     * have the next line.
56
     * 
57
     * @return mixed
58
     */
59
    public function fetchRow(bool $reexecute = false)
60
    {
61
        if ($this->lastRequestStatement === null || $reexecute === true) {
62
            $this->execute();
63
        }
64
        
65
        return $this->lastRequestStatement->fetch($this->obtainPdoFetchType());
66
    }
67
    
68
    /**
69
     * Fetch all rows returned by the request
70
     * 
71
     * @return \Generator
72
     */
73
    public function fetchAll(): \Generator
74
    {
75
        $request = $this->execute(); //throw an Exception if error
76
        
77
        while ($row = $request->fetch($this->obtainPdoFetchType())) {
78
            yield $row;
79
        }
80
    }
81
}
82