Passed
Push — master ( 4ee5c7...1ee112 )
by Jonas
03:35
created

HasEagerLimit::newMorphMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 5
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Staudenmeir\EloquentEagerLimit;
4
5
use Illuminate\Database\Connection;
6
use Illuminate\Database\Eloquent\Builder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Staudenmeir\EloquentEagerLimit\Builder. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Illuminate\Database\Eloquent\Model;
8
use RuntimeException;
9
use Staudenmeir\EloquentEagerLimit\Grammars\MySqlGrammar;
10
use Staudenmeir\EloquentEagerLimit\Grammars\PostgresGrammar;
11
use Staudenmeir\EloquentEagerLimit\Grammars\SQLiteGrammar;
12
use Staudenmeir\EloquentEagerLimit\Grammars\SqlServerGrammar;
13
use Staudenmeir\EloquentEagerLimit\Relations\BelongsToMany;
14
use Staudenmeir\EloquentEagerLimit\Relations\HasMany;
15
use Staudenmeir\EloquentEagerLimit\Relations\HasManyThrough;
16
use Staudenmeir\EloquentEagerLimit\Relations\HasOne;
17
use Staudenmeir\EloquentEagerLimit\Relations\HasOneThrough;
18
use Staudenmeir\EloquentEagerLimit\Relations\MorphMany;
19
use Staudenmeir\EloquentEagerLimit\Relations\MorphOne;
20
use Staudenmeir\EloquentEagerLimit\Relations\MorphToMany;
21
22
trait HasEagerLimit
23
{
24
    /**
25
     * Get a new query builder instance for the connection.
26
     *
27
     * @return \Illuminate\Database\Query\Builder
28
     */
29 74
    protected function newBaseQueryBuilder()
30
    {
31 74
        $connection = $this->getConnection();
0 ignored issues
show
Bug introduced by
It seems like getConnection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        /** @scrutinizer ignore-call */ 
32
        $connection = $this->getConnection();
Loading history...
32
33 74
        $grammar = $connection->withTablePrefix($this->getQueryGrammar($connection));
34
35 74
        return new \Staudenmeir\EloquentEagerLimit\Builder(
36 74
            $connection, $grammar, $connection->getPostProcessor()
37
        );
38
    }
39
40
    /**
41
     * Get the query grammar.
42
     *
43
     * @param  \Illuminate\Database\Connection  $connection
44
     * @return \Illuminate\Database\Query\Grammars\Grammar
45
     */
46 74
    protected function getQueryGrammar(Connection $connection) {
47 74
        $driver = $connection->getDriverName();
48
49 74
        switch ($driver) {
50 74
            case 'mysql':
51 37
                return new MySqlGrammar;
52 37
            case 'pgsql':
53 37
                return new PostgresGrammar;
54
            case 'sqlite':
55
                return new SQLiteGrammar;
56
            case 'sqlsrv':
57
                return new SqlServerGrammar;
58
        }
59
60
        throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore
61
    }
62
63
    /**
64
     * Instantiate a new HasOne relationship.
65
     *
66
     * @param  \Illuminate\Database\Eloquent\Builder  $query
67
     * @param  \Illuminate\Database\Eloquent\Model  $parent
68
     * @param  string  $foreignKey
69
     * @param  string  $localKey
70
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
71
     */
72 12
    protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
73
    {
74 12
        return new HasOne($query, $parent, $foreignKey, $localKey);
75
    }
76
77
    /**
78
     * Instantiate a new HasOneThrough relationship.
79
     *
80
     * @param  \Illuminate\Database\Eloquent\Builder  $query
81
     * @param  \Illuminate\Database\Eloquent\Model  $farParent
82
     * @param  \Illuminate\Database\Eloquent\Model  $throughParent
83
     * @param  string  $firstKey
84
     * @param  string  $secondKey
85
     * @param  string  $localKey
86
     * @param  string  $secondLocalKey
87
     * @return \Illuminate\Database\Eloquent\Relations\HasOneThrough
88
     */
89 8
    protected function newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
90
    {
91 8
        return new HasOneThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
92
    }
93
94
    /**
95
     * Instantiate a new MorphOne relationship.
96
     *
97
     * @param  \Illuminate\Database\Eloquent\Builder  $query
98
     * @param  \Illuminate\Database\Eloquent\Model  $parent
99
     * @param  string  $type
100
     * @param  string  $id
101
     * @param  string  $localKey
102
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
103
     */
104 8
    protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey)
105
    {
106 8
        return new MorphOne($query, $parent, $type, $id, $localKey);
107
    }
108
109
    /**
110
     * Instantiate a new HasMany relationship.
111
     *
112
     * @param  \Illuminate\Database\Eloquent\Builder  $query
113
     * @param  \Illuminate\Database\Eloquent\Model  $parent
114
     * @param  string  $foreignKey
115
     * @param  string  $localKey
116
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
117
     */
118 8
    protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
119
    {
120 8
        return new HasMany($query, $parent, $foreignKey, $localKey);
121
    }
122
123
    /**
124
     * Instantiate a new HasManyThrough relationship.
125
     *
126
     * @param  \Illuminate\Database\Eloquent\Builder  $query
127
     * @param  \Illuminate\Database\Eloquent\Model  $farParent
128
     * @param  \Illuminate\Database\Eloquent\Model  $throughParent
129
     * @param  string  $firstKey
130
     * @param  string  $secondKey
131
     * @param  string  $localKey
132
     * @param  string  $secondLocalKey
133
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
134
     */
135 8
    protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
136
    {
137 8
        return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
138
    }
139
140
    /**
141
     * Instantiate a new MorphMany relationship.
142
     *
143
     * @param  \Illuminate\Database\Eloquent\Builder  $query
144
     * @param  \Illuminate\Database\Eloquent\Model  $parent
145
     * @param  string  $type
146
     * @param  string  $id
147
     * @param  string  $localKey
148
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
149
     */
150 8
    protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey)
151
    {
152 8
        return new MorphMany($query, $parent, $type, $id, $localKey);
153
    }
154
155
    /**
156
     * Instantiate a new BelongsToMany relationship.
157
     *
158
     * @param  \Illuminate\Database\Eloquent\Builder  $query
159
     * @param  \Illuminate\Database\Eloquent\Model  $parent
160
     * @param  string  $table
161
     * @param  string  $foreignPivotKey
162
     * @param  string  $relatedPivotKey
163
     * @param  string  $parentKey
164
     * @param  string  $relatedKey
165
     * @param  string  $relationName
166
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
167
     */
168 8
    protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey,
169
                                        $parentKey, $relatedKey, $relationName = null)
170
    {
171 8
        return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName);
172
    }
173
174
    /**
175
     * Instantiate a new MorphToMany relationship.
176
     *
177
     * @param  \Illuminate\Database\Eloquent\Builder  $query
178
     * @param  \Illuminate\Database\Eloquent\Model  $parent
179
     * @param  string  $name
180
     * @param  string  $table
181
     * @param  string  $foreignPivotKey
182
     * @param  string  $relatedPivotKey
183
     * @param  string  $parentKey
184
     * @param  string  $relatedKey
185
     * @param  string  $relationName
186
     * @param  bool  $inverse
187
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
188
     */
189 8
    protected function newMorphToMany(Builder $query, Model $parent, $name, $table, $foreignPivotKey,
190
                                      $relatedPivotKey, $parentKey, $relatedKey,
191
                                      $relationName = null, $inverse = false)
192
    {
193 8
        return new MorphToMany($query, $parent, $name, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey,
194
            $relationName, $inverse);
195
    }
196
}
197