Subquery   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A setQuery() 0 4 1
A getQuery() 0 4 1
A getValue() 0 12 2
A getAlias() 0 4 1
A setAlias() 0 6 1
A __set() 0 4 1
A __get() 0 4 1
A __call() 0 4 1
1
<?php
2
3
namespace Sofa\Eloquence;
4
5
use Illuminate\Database\Query\Expression;
6
use Illuminate\Database\Query\Builder as QueryBuilder;
7
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
8
9
class Subquery extends Expression
10
{
11
    /**
12
     * Query builder instance.
13
     *
14
     * @var \Illuminate\Database\Query\Builder
15
     */
16
    protected $query;
17
18
    /**
19
     * Alias for the subquery.
20
     *
21
     * @var string
22
     */
23
    protected $alias;
24
25
    /**
26
     * Create new subquery instance.
27
     *
28
     * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
29
     * @param string $alias
30
     */
31
    public function __construct($query, $alias = null)
32
    {
33
        if ($query instanceof EloquentBuilder) {
34
            $query = $query->getQuery();
35
        }
36
37
        $this->setQuery($query);
38
39
        $this->alias = $alias;
40
    }
41
42
    /**
43
     * Set underlying query builder.
44
     *
45
     * @param \Illuminate\Database\Query\Builder $query
46
     */
47
    public function setQuery(QueryBuilder $query)
48
    {
49
        $this->query = $query;
50
    }
51
52
    /**
53
     * Get underlying query builder.
54
     *
55
     * @return \Illuminate\Database\Query\Builder
56
     */
57
    public function getQuery()
58
    {
59
        return $this->query;
60
    }
61
62
    /**
63
     * Evaluate query as string.
64
     *
65
     * @return string
66
     */
67
    public function getValue()
68
    {
69
        $sql = '('.$this->query->toSql().')';
70
71
        if ($this->alias) {
72
            $alias = $this->query->getGrammar()->wrapTable($this->alias);
73
74
            $sql .= ' as '.$alias;
75
        }
76
77
        return $sql;
78
    }
79
80
    /**
81
     * Get subquery alias.
82
     *
83
     * @return string
84
     */
85
    public function getAlias()
86
    {
87
        return $this->alias;
88
    }
89
90
    /**
91
     * Set subquery alias.
92
     *
93
     * @param  string $alias
94
     * @return $this
95
     */
96
    public function setAlias($alias)
97
    {
98
        $this->alias = $alias;
99
100
        return $this;
101
    }
102
103
    /**
104
     * Pass property calls to the underlying builder.
105
     *
106
     * @param  string $property
107
     * @param  mixed  $value
108
     * @return mixed
109
     */
110
    public function __set($property, $value)
111
    {
112
        return $this->query->{$property} = $value;
113
    }
114
115
    /**
116
     * Pass property calls to the underlying builder.
117
     *
118
     * @param  string $property
119
     * @return mixed
120
     */
121
    public function __get($property)
122
    {
123
        return $this->query->{$property};
124
    }
125
126
    /**
127
     * Pass method calls to the underlying builder.
128
     *
129
     * @param  string $method
130
     * @param  array  $params
131
     * @return mixed
132
     */
133
    public function __call($method, $params)
134
    {
135
        return call_user_func_array([$this->query, $method], $params);
136
    }
137
}
138