Completed
Push — master ( 17909b...b957b5 )
by Mikaël
18:52 queued 08:52
created

HasTranslatableRelationships::belongsToMany()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.4746
c 0
b 0
f 0
cc 7
nc 16
nop 7
1
<?php
2
3
namespace BBSLab\NovaTranslation\Models\Concerns;
4
5
use BBSLab\NovaTranslation\Models\Relations\BelongsToMany;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Concerns\HasRelationships;
8
use Illuminate\Database\Eloquent\Model;
9
10
trait HasTranslatableRelationships
11
{
12
    use HasRelationships;
13
14
    /**
15
     * Define a many-to-many relationship.
16
     *
17
     * @param  string  $related
18
     * @param  string|null  $table
19
     * @param  string|null  $foreignPivotKey
20
     * @param  string|null  $relatedPivotKey
21
     * @param  string|null  $parentKey
22
     * @param  string|null  $relatedKey
23
     * @param  string|null  $relation
24
     * @return \BBSLab\NovaTranslation\Models\Relations\BelongsToMany
25
     */
26
    public function belongsToMany($related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null,
27
        $parentKey = null, $relatedKey = null, $relation = null)
28
    {
29
        // If no relationship name was passed, we will pull backtraces to get the
30
        // name of the calling function. We will use that function name as the
31
        // title of this relation since that is a great convention to apply.
32
        if (is_null($relation)) {
33
            $relation = $this->guessBelongsToManyRelation();
34
        }
35
36
        // First, we'll need to determine the foreign key and "other key" for the
37
        // relationship. Once we have determined the keys we'll make the query
38
        // instances as well as the relationship instances we need for this.
39
        $instance = $this->newRelatedInstance($related);
40
41
        $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey();
0 ignored issues
show
Bug introduced by
It seems like getForeignKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
42
43
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
44
45
        // If no table name was provided, we can guess it by concatenating the two
46
        // models using underscores in alphabetical order. The two model names
47
        // are transformed to snake case from their default CamelCase also.
48
        if (is_null($table)) {
49
            $table = $this->joiningTable($related, $instance);
50
        }
51
52
        return $this->newBelongsToMany(
53
            $instance->newQuery(), $this, $table, $foreignPivotKey,
54
            $relatedPivotKey, $parentKey ?: $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
55
            $relatedKey ?: $instance->getKeyName(), $relation
56
        );
57
    }
58
59
    /**
60
     * Instantiate a new BelongsToMany relationship.
61
     *
62
     * @param  \Illuminate\Database\Eloquent\Builder  $query
63
     * @param  \Illuminate\Database\Eloquent\Model  $parent
64
     * @param  string  $table
65
     * @param  string  $foreignPivotKey
66
     * @param  string  $relatedPivotKey
67
     * @param  string  $parentKey
68
     * @param  string  $relatedKey
69
     * @param  string|null  $relationName
70
     * @return \BBSLab\NovaTranslation\Models\Relations\BelongsToMany
71
     */
72
    protected function newBelongsToMany(Builder $query, Model $parent, $table, $foreignPivotKey, $relatedPivotKey,
73
        $parentKey, $relatedKey, $relationName = null)
74
    {
75
        return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName);
76
    }
77
}
78