HasMergedRelationships   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mergedRelation() 0 3 1
A mergedRelationWithModel() 0 9 2
A newMergedRelation() 0 3 1
1
<?php
2
3
namespace Staudenmeir\LaravelMergedRelations\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6
use Illuminate\Database\Eloquent\Model;
7
use Staudenmeir\LaravelMergedRelations\Eloquent\Relations\MergedRelation;
8
9
trait HasMergedRelationships
10
{
11
    /**
12
     * Define a merged relationship.
13
     *
14
     * @param string $view
15
     * @param string|null $localKey
16
     * @return \Staudenmeir\LaravelMergedRelations\Eloquent\Relations\MergedRelation
17
     */
18
    public function mergedRelation($view, $localKey = null)
19
    {
20
        return $this->mergedRelationWithModel(static::class, $view, $localKey);
21
    }
22
23
    /**
24
     * Define a merged relationship with model.
25
     *
26
     * @param string $related
27
     * @param string $view
28
     * @param string|null $localKey
29
     * @return \Staudenmeir\LaravelMergedRelations\Eloquent\Relations\MergedRelation
30
     */
31
    public function mergedRelationWithModel($related, $view, $localKey = null)
32
    {
33
        $instance = $this->newRelatedInstance($related)->setTable($view);
0 ignored issues
show
Bug introduced by
It seems like newRelatedInstance() 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

33
        $instance = $this->/** @scrutinizer ignore-call */ newRelatedInstance($related)->setTable($view);
Loading history...
34
35
        $query = (new Builder($instance->getConnection()->query()))->setModel($instance);
36
37
        $localKey = $localKey ?: $this->getKeyName();
0 ignored issues
show
Bug introduced by
It seems like getKeyName() 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

37
        $localKey = $localKey ?: $this->/** @scrutinizer ignore-call */ getKeyName();
Loading history...
38
39
        return $this->newMergedRelation($query, $this, $view.'.laravel_foreign_key', $localKey);
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelMerge...\HasMergedRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelMerge...ps::newMergedRelation(). ( Ignorable by Annotation )

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

39
        return $this->newMergedRelation($query, /** @scrutinizer ignore-type */ $this, $view.'.laravel_foreign_key', $localKey);
Loading history...
40
    }
41
42
    /**
43
     * Instantiate a new MergedRelation relationship.
44
     *
45
     * @param \Illuminate\Database\Eloquent\Builder $query
46
     * @param \Illuminate\Database\Eloquent\Model $parent
47
     * @param string $foreignKey
48
     * @param string $localKey
49
     * @return \Staudenmeir\LaravelMergedRelations\Eloquent\Relations\MergedRelation
50
     */
51
    protected function newMergedRelation(EloquentBuilder $query, Model $parent, $foreignKey, $localKey)
52
    {
53
        return new MergedRelation($query, $parent, $foreignKey, $localKey);
54
    }
55
}
56