SubqueryMagicScope::addWhereNotInSubquery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace MaksimM\SubqueryMagic\Scopes;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\Relation;
8
use Illuminate\Database\Eloquent\Scope;
9
use Illuminate\Support\Facades\DB;
10
11
class SubqueryMagicScope implements Scope
12
{
13
    /**
14
     * All of the extensions to be added to the builder.
15
     *
16
     * @var array
17
     */
18
    protected $extensions = [
19
        'LeftJoinSubquery',
20
        'JoinSubquery',
21
        'RightJoinSubquery',
22
        'WhereInSubquery',
23
        'WhereNotInSubquery',
24
        'OrWhereInSubquery',
25
        'OrWhereNotInSubquery',
26
        'FromSubquery',
27
    ];
28
29
    /**
30
     * Apply the scope to a given Eloquent query builder.
31
     *
32
     * @param \Illuminate\Database\Eloquent\Builder|Relation $builder
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $builder a bit more specific; maybe use Builder.
Loading history...
33
     * @param \Illuminate\Database\Eloquent\Model            $model
34
     */
35
    public function apply(Builder $builder, Model $model)
36
    {
37
    }
38
39
    /**
40
     * Extend the query builder with the needed functions.
41
     *
42
     * @param \Illuminate\Database\Eloquent\Builder|Relation $builder
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $builder a bit more specific; maybe use Builder.
Loading history...
43
     */
44
    public function extend(Builder $builder)
45
    {
46
        foreach ($this->extensions as $extension) {
47
            $this->{"add{$extension}"}($builder);
48
        }
49
    }
50
51
    /**
52
     * @param \Illuminate\Database\Eloquent\Builder|Relation $builder
53
     * @param array                                          $bindings
54
     * @param string                                         $type
55
     */
56
    private function addBindings($builder, $bindings, $type = 'where')
57
    {
58
        $builder->addBinding($bindings, $type);
59
    }
60
61
    /**
62
     * Add extension to the builder.
63
     *
64
     * @param \Illuminate\Database\Eloquent\Builder|Relation $builder
65
     */
66 View Code Duplication
    protected function addLeftJoinSubquery($builder)
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...
67
    {
68
        $builder->macro('leftJoinSubquery', function ($builder, $subquery, $alias, \Closure $on) {
69
            /*
70
             * @var $builder \Illuminate\Database\Eloquent\Builder|Relation
71
             * @var $subquery \Illuminate\Database\Eloquent\Builder|Relation
72
             */
73
            $builder->leftJoin(DB::raw('('.$subquery->toSql().') '.$builder->getQuery()->getGrammar()->wrap($alias)), $on);
74
            //merge bindings from subquery
75
            $this->addBindings($builder, $subquery->getBindings(), 'join');
76
77
            return $builder;
78
        });
79
    }
80
81
    /**
82
     * Add extension to the builder.
83
     *
84
     * @param \Illuminate\Database\Eloquent\Builder|Relation $builder
85
     */
86 View Code Duplication
    protected function addRightJoinSubquery($builder)
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...
87
    {
88
        $builder->macro('rightJoinSubquery', function ($builder, $subquery, $alias, \Closure $on) {
89
            /*
90
             * @var $builder \Illuminate\Database\Eloquent\Builder|Relation
91
             * @var $subquery \Illuminate\Database\Eloquent\Builder|Relation
92
             */
93
            $builder->rightJoin(DB::raw('('.$subquery->toSql().') '.$builder->getQuery()->getGrammar()->wrap($alias)), $on);
94
            //merge bindings from subquery
95
            $this->addBindings($builder, $subquery->getBindings(), 'join');
96
97
            return $builder;
98
        });
99
    }
100
101
    /**
102
     * Add extension to the builder.
103
     *
104
     * @param \Illuminate\Database\Eloquent\Builder|Relation $builder
105
     */
106 View Code Duplication
    protected function addJoinSubquery($builder)
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...
107
    {
108
        $builder->macro('joinSubquery', function ($builder, $subquery, $alias, \Closure $on) {
109
            /*
110
             * @var $builder \Illuminate\Database\Eloquent\Builder|Relation
111
             * @var $subquery \Illuminate\Database\Eloquent\Builder|Relation
112
             */
113
            $builder->join(DB::raw('('.$subquery->toSql().') '.$builder->getQuery()->getGrammar()->wrap($alias)), $on);
114
            //merge bindings from subquery
115
            $this->addBindings($builder, $subquery->getBindings(), 'join');
116
117
            return $builder;
118
        });
119
    }
120
121
    /**
122
     * Add extension to the builder.
123
     *
124
     * @param \Illuminate\Database\Eloquent\Builder|Relation $builder
125
     */
126 View Code Duplication
    protected function addWhereInSubquery($builder)
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...
127
    {
128
        $builder->macro('whereInSubquery', function ($builder, $field, $subquery) {
129
            /*
130
             * @var $builder \Illuminate\Database\Eloquent\Builder|Relation
131
             * @var $subquery \Illuminate\Database\Eloquent\Builder|Relation
132
             */
133
            $builder->whereRaw($builder->getQuery()->getGrammar()->wrap($field).' IN ('.$subquery->toSql().')');
134
            //merge bindings from subquery
135
            $this->addBindings($builder, $subquery->getBindings(), 'where');
136
137
            return $builder;
138
        });
139
    }
140
141
    /**
142
     * Add extension to the builder.
143
     *
144
     * @param \Illuminate\Database\Eloquent\Builder|Relation $builder
145
     */
146 View Code Duplication
    protected function addWhereNotInSubquery($builder)
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...
147
    {
148
        $builder->macro('whereNotInSubquery', function ($builder, $field, $subquery) {
149
            /*
150
             * @var $builder \Illuminate\Database\Eloquent\Builder|Relation
151
             * @var $subquery \Illuminate\Database\Eloquent\Builder|Relation
152
             */
153
            $builder->whereRaw($builder->getQuery()->getGrammar()->wrap($field).' NOT IN ('.$subquery->toSql().')');
154
            //merge bindings from subquery
155
            $this->addBindings($builder, $subquery->getBindings(), 'where');
156
157
            return $builder;
158
        });
159
    }
160
161
    /**
162
     * Add extension to the builder.
163
     *
164
     * @param \Illuminate\Database\Eloquent\Builder|Relation $builder
165
     */
166 View Code Duplication
    protected function addOrWhereInSubquery($builder)
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...
167
    {
168
        $builder->macro('orWhereInSubquery', function ($builder, $field, $subquery) {
169
            /*
170
             * @var $builder \Illuminate\Database\Eloquent\Builder|Relation
171
             * @var $subquery \Illuminate\Database\Eloquent\Builder|Relation
172
             */
173
            $builder->orWhereRaw($builder->getQuery()->getGrammar()->wrap($field).' IN ('.$subquery->toSql().')');
174
            //merge bindings from subquery
175
            $this->addBindings($builder, $subquery->getBindings(), 'where');
176
177
            return $builder;
178
        });
179
    }
180
181
    /**
182
     * Add extension to the builder.
183
     *
184
     * @param \Illuminate\Database\Eloquent\Builder|Relation $builder
185
     */
186 View Code Duplication
    protected function addOrWhereNotInSubquery($builder)
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...
187
    {
188
        $builder->macro('orWhereNotInSubquery', function ($builder, $field, $subquery) {
189
            /*
190
             * @var $builder \Illuminate\Database\Eloquent\Builder|Relation
191
             * @var $subquery \Illuminate\Database\Eloquent\Builder|Relation
192
             */
193
            $builder->orWhereRaw($builder->getQuery()->getGrammar()->wrap($field).' NOT IN ('.$subquery->toSql().')');
194
            //merge bindings from subquery
195
            $this->addBindings($builder, $subquery->getBindings(), 'where');
196
197
            return $builder;
198
        });
199
    }
200
201
    /**
202
     * Add extension to the builder.
203
     *
204
     * @param \Illuminate\Database\Eloquent\Builder|Relation $builder
205
     */
206 View Code Duplication
    protected function addFromSubquery($builder)
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...
207
    {
208
        $builder->macro('fromSubquery', function ($builder, $subquery, $alias) {
209
            /*
210
             * @var $builder \Illuminate\Database\Eloquent\Builder|Relation
211
             * @var $subquery \Illuminate\Database\Eloquent\Builder|Relation
212
             */
213
            $builder->from(DB::raw('('.$subquery->toSql().') '.$builder->getQuery()->getGrammar()->wrap($alias)));
214
            //merge bindings from subquery
215
            $builder->setBindings(array_merge($subquery->getBindings(), $builder->getBindings()));
216
217
            return $builder;
218
        });
219
    }
220
}
221