Passed
Push — master ( f96b92...f35c3a )
by Morten Poul
03:26
created

HasPivotEvents::newBelongsToMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 8
dl 0
loc 11
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Signifly\PivotEvents;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
9
trait HasPivotEvents
10
{
11
    protected $pivotChanges = [];
12
13
    public function setPivotChanges(string $type, string $relation, array $ids = []): void
14
    {
15
        collect($ids)->each(function ($attributes, $id) use ($type, $relation) {
16
            data_set($this->pivotChanges, "{$type}.{$relation}.{$id}", $attributes);
17
        });
18
    }
19
20
    public function getPivotChanges($type = null): Collection
21
    {
22
        if ($type) {
23
            return collect(data_get($this->pivotChanges, $type));
24
        }
25
26
        return collect($this->pivotChanges);
27
    }
28
29
    public function getPivotChangeIds($type, $relation): Collection
30
    {
31
        return collect($this->getPivotChanges("{$type}.{$relation}"))->keys();
32
    }
33
34
    public function resetPivotChanges(): void
35
    {
36
        $this->pivotChanges = [];
37
    }
38
39
    public static function pivotAttaching($callback)
40
    {
41
        static::registerModelEvent('pivotAttaching', $callback);
42
    }
43
44
    public static function pivotAttached($callback)
45
    {
46
        static::registerModelEvent('pivotAttached', $callback);
47
    }
48
49
    public static function pivotDetaching($callback)
50
    {
51
        static::registerModelEvent('pivotDetaching', $callback);
52
    }
53
54
    public static function pivotDetached($callback)
55
    {
56
        static::registerModelEvent('pivotDetached', $callback);
57
    }
58
59
    public static function pivotUpdating($callback)
60
    {
61
        static::registerModelEvent('pivotUpdating', $callback);
62
    }
63
64
    public static function pivotUpdated($callback)
65
    {
66
        static::registerModelEvent('pivotUpdated', $callback);
67
    }
68
69
    public function firePivotAttachingEvent($halt = true)
70
    {
71
        return $this->fireModelEvent('pivotAttaching', $halt);
0 ignored issues
show
Bug introduced by
It seems like fireModelEvent() 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

71
        return $this->/** @scrutinizer ignore-call */ fireModelEvent('pivotAttaching', $halt);
Loading history...
72
    }
73
74
    public function firePivotAttachedEvent($halt = false)
75
    {
76
        return $this->fireModelEvent('pivotAttached', $halt);
77
    }
78
79
    public function firePivotDetachingEvent($halt = true)
80
    {
81
        return $this->fireModelEvent('pivotDetaching', $halt);
82
    }
83
84
    public function firePivotDetachedEvent($halt = false)
85
    {
86
        return $this->fireModelEvent('pivotDetached', $halt);
87
    }
88
89
    public function firePivotUpdatingEvent($halt = true)
90
    {
91
        return $this->fireModelEvent('pivotUpdating', $halt);
92
    }
93
94
    public function firePivotUpdatedEvent($halt = false)
95
    {
96
        return $this->fireModelEvent('pivotUpdated', $halt);
97
    }
98
99
    /**
100
     * Get the observable event names.
101
     *
102
     * @return array
103
     */
104
    public function getObservableEvents()
105
    {
106
        return array_merge(
107
            parent::getObservableEvents(),
108
            [
109
                'pivotAttaching', 'pivotAttached',
110
                'pivotDetaching', 'pivotDetached',
111
                'pivotUpdating', 'pivotUpdated',
112
            ]
113
        );
114
    }
115
116
    /**
117
     * Instantiate a new BelongsToMany relationship.
118
     *
119
     * @param  \Illuminate\Database\Eloquent\Builder  $query
120
     * @param  \Illuminate\Database\Eloquent\Model  $parent
121
     * @param  string  $table
122
     * @param  string  $foreignPivotKey
123
     * @param  string  $relatedPivotKey
124
     * @param  string  $parentKey
125
     * @param  string  $relatedKey
126
     * @param  string  $relationName
127
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
128
     */
129
    protected function newBelongsToMany(
130
        Builder $query,
131
        Model $parent,
132
        $table,
133
        $foreignPivotKey,
134
        $relatedPivotKey,
135
        $parentKey,
136
        $relatedKey,
137
        $relationName = null
138
    ) {
139
        return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName);
140
    }
141
}
142