SubQuery::getQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BfwSql\Queries\Parts;
4
5
use \Exception;
6
use \BfwSql\Queries\AbstractQuery;
7
8
class SubQuery extends AbstractPart
9
{
10
    /**
11
     * @const ERR_QUERY_NOT_OBJECT_OR_STRING Exception code if the query is
12
     * not an object or a string.
13
     */
14
    const ERR_QUERY_NOT_OBJECT_OR_STRING = 2513001;
15
    
16
    /**
17
     * @const ERR_QUERY_OBJECT_BAD_INSTANCE Exception code in the case of
18
     * the query is an object but with not the expected class.
19
     */
20
    const ERR_QUERY_OBJECT_BAD_INSTANCE = 2513002;
21
    
22
    /**
23
     * @var string $query The sub-query
24
     */
25
    protected $query = '';
26
    
27
    /**
28
     * @var string $shortcut The shortcut to use into the request
29
     */
30
    protected $shortcut = '';
31
    
32
    /**
33
     * Define the shortcut and the sql query.
34
     * If the sub-query is an object, call method to obtain the string query
35
     * 
36
     * @param \BfwSql\Queries\AbstractQuery $querySystem
37
     * @param string $shortcut The shortcut to use into the request
38
     * @param string|\BfwSql\Queries\AbstractQuery $query The sub-query
39
     */
40
    public function __construct(
41
        AbstractQuery $querySystem,
42
        string $shortcut,
43
        $query
44
    ) {
45
        parent::__construct($querySystem);
46
        
47
        $this->shortcut = $shortcut;
48
        $this->query    = $this->obtainAssembledQuery($query);
49
    }
50
    
51
    /**
52
     * Getter accessor to property query
53
     * 
54
     * @return string
55
     */
56
    public function getQuery(): string
57
    {
58
        return $this->query;
59
    }
60
61
    /**
62
     * Getter accessor to property shortcut
63
     * 
64
     * @return string
65
     */
66
    public function getShortcut(): string
67
    {
68
        return $this->shortcut;
69
    }
70
    
71
    /**
72
     * Obtain the sql request (string type) from the arguments
73
     * If it's already a string, just return the string.
74
     * Else, if it's an AbstractQuery, use the method assemble() to obtain
75
     * the string sql request.
76
     * 
77
     * @param string|\BfwSql\Queries\AbstractQuery $query The sub-query
78
     * 
79
     * @return string
80
     * 
81
     * @throws \Exception If the sub-query has not the correct type or if it's
82
     * not the correct class instance.
83
     */
84
    protected function obtainAssembledQuery($query): string
85
    {
86
        if (is_string($query)) {
87
            return $query;
88
        }
89
        
90
        if (!is_object($query)) {
91
            throw new Exception(
92
                'A sub-query should be a string or an AbstractQuery object',
93
                self::ERR_QUERY_NOT_OBJECT_OR_STRING
94
            );
95
        }
96
        
97
        if ($query instanceof \BfwSql\Queries\AbstractQuery === false) {
98
            throw new Exception(
99
                'If a sub-query is an object, '
100
                .'it should be an instance of AbstractQuery object',
101
                self::ERR_QUERY_OBJECT_BAD_INSTANCE
102
            );
103
        }
104
        
105
        return $query->assemble();
106
    }
107
    
108
    /**
109
     * Generate the sql query to use for to this sub-query
110
     * 
111
     * @return string
112
     */
113
    public function generate(): string
114
    {
115
        return $this->querySystem
116
            ->getQuerySgbd()
117
            ->subQuery($this->query, $this->shortcut)
118
        ;
119
    }
120
}
121