Passed
Pull Request — master (#33)
by Mark van den
16:57 queued 06:50
created

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