Passed
Push — master ( 09af0a...9826c5 )
by Jonas
06:00
created

IsJsonRelation::pivotRelation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Staudenmeir\EloquentJsonRelations\Relations;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\Pivot;
9
use RuntimeException;
10
use Staudenmeir\EloquentJsonRelations\Grammars\MySqlGrammar;
11
use Staudenmeir\EloquentJsonRelations\Grammars\PostgresGrammar;
12
use Staudenmeir\EloquentJsonRelations\Grammars\SqlServerGrammar;
13
14
trait IsJsonRelation
15
{
16
    /**
17
     * The base path of the foreign key.
18
     *
19
     * @var string
20
     */
21
    protected $path;
22
23
    /**
24
     * The optional object key of the foreign key.
25
     *
26
     * @var string
27
     */
28
    protected $key;
29
30
    /**
31
     * Create a new JSON relationship instance.
32
     *
33
     * @return void
34
     */
35 106
    public function __construct()
36
    {
37 106
        $args = func_get_args();
38
39 106
        $foreignKey = explode('[]->', $args[2]);
40
41 106
        $this->path = $foreignKey[0];
42 106
        $this->key = $foreignKey[1] ?? null;
43
44 106
        parent::__construct(...$args);
45
    }
46
47
    /**
48
     * Hydrate the pivot relationship on the models.
49
     *
50
     * @param \Illuminate\Database\Eloquent\Collection $models
51
     * @param \Illuminate\Database\Eloquent\Model $parent
52
     * @return void
53
     */
54 35
    protected function hydratePivotRelation(Collection $models, Model $parent)
55
    {
56 35
        foreach ($models as $i => $model) {
57 35
            $clone = clone $model;
58
59 35
            $models[$i] = $clone->setRelation('pivot', $this->pivotRelation($clone, $parent));
60
        }
61
    }
62
63
    /**
64
     * Get the pivot relationship from the query.
65
     *
66
     * @param \Illuminate\Database\Eloquent\Model $model
67
     * @param \Illuminate\Database\Eloquent\Model $parent
68
     * @return \Illuminate\Database\Eloquent\Model
69
     */
70 35
    protected function pivotRelation(Model $model, Model $parent)
71
    {
72 35
        $attributes = $this->pivotAttributes($model, $parent);
73
74 35
        return Pivot::fromAttributes($model, $attributes, null, true);
75
    }
76
77
    /**
78
     * Get the pivot attributes from a model.
79
     *
80
     * @param \Illuminate\Database\Eloquent\Model $model
81
     * @param \Illuminate\Database\Eloquent\Model $parent
82
     * @return array
83
     */
84
    abstract protected function pivotAttributes(Model $model, Model $parent);
85
86
    /**
87
     * Execute the query and get the first related model.
88
     *
89
     * @param array $columns
90
     * @return mixed
91
     */
92 5
    public function first($columns = ['*'])
93
    {
94 5
        return $this->take(1)->get($columns)->first();
0 ignored issues
show
Bug introduced by
It seems like take() 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

94
        return $this->/** @scrutinizer ignore-call */ take(1)->get($columns)->first();
Loading history...
95
    }
96
97
    /**
98
     * Get the fully qualified path of the relationship.
99
     *
100
     * @return string
101
     */
102 15
    public function getQualifiedPath()
103
    {
104 15
        return $this->parent->qualifyColumn($this->path);
105
    }
106
107
    /**
108
     * Get the JSON grammar.
109
     *
110
     * @param \Illuminate\Database\Eloquent\Builder $query
111
     * @return \Staudenmeir\EloquentJsonRelations\Grammars\JsonGrammar
112
     */
113 20
    protected function getJsonGrammar(Builder $query)
114
    {
115 20
        $driver = $query->getConnection()->getDriverName();
116
117 20
        switch ($driver) {
118 20
            case 'mysql':
119 8
                return $query->getConnection()->withTablePrefix(new MySqlGrammar());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->getConnec...rammars\MySqlGrammar()) also could return the type Illuminate\Database\Conn...tabase\Eloquent\Builder which is incompatible with the documented return type Staudenmeir\EloquentJson...ns\Grammars\JsonGrammar.
Loading history...
120 12
            case 'pgsql':
121 8
                return $query->getConnection()->withTablePrefix(new PostgresGrammar());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->getConnec...mars\PostgresGrammar()) also could return the type Illuminate\Database\Conn...tabase\Eloquent\Builder which is incompatible with the documented return type Staudenmeir\EloquentJson...ns\Grammars\JsonGrammar.
Loading history...
122 4
            case 'sqlsrv':
123 4
                return $query->getConnection()->withTablePrefix(new SqlServerGrammar());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->getConnec...ars\SqlServerGrammar()) also could return the type Illuminate\Database\Conn...tabase\Eloquent\Builder which is incompatible with the documented return type Staudenmeir\EloquentJson...ns\Grammars\JsonGrammar.
Loading history...
124
        }
125
126
        throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore
127
    }
128
}
129