Passed
Push — master ( 1a4dd8...786e6d )
by Carlos
06:24
created

HasPivotEvents::pivotAttaching()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/laravel-follow
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\LaravelFollow\Traits;
13
14
use Illuminate\Database\Eloquent\Model;
15
use Illuminate\Database\Eloquent\Builder;
16
use Overtrue\LaravelFollow\BelongsToMany;
17
18
trait HasPivotEvents
19
{
20
    protected $pivotChanges = [];
21
22
    public function setPivotChanges(string $type, string $relation, array $ids = [])
23
    {
24
        collect($ids)->each(function ($attributes, $id) use ($type, $relation) {
25
            data_set($this->pivotChanges, "{$type}.{$relation}.{$id}", $attributes);
26
        });
27
    }
28
29
    public function getPivotChanges($type = null)
30
    {
31
        if ($type) {
32
            return collect(data_get($this->pivotChanges, $type));
33
        }
34
35
        return collect($this->pivotChanges);
36
    }
37
38
    public function getPivotChangeIds($type, $relation)
39
    {
40
        return collect($this->getPivotChanges("{$type}.{$relation}"))->keys();
41
    }
42
43
    public static function pivotAttaching($callback)
44
    {
45
        static::registerModelEvent('pivotAttaching', $callback);
46
    }
47
48
    public static function pivotAttached($callback)
49
    {
50
        static::registerModelEvent('pivotAttached', $callback);
51
    }
52
53
    public static function pivotDetaching($callback)
54
    {
55
        static::registerModelEvent('pivotDetaching', $callback);
56
    }
57
58
    public static function pivotDetached($callback)
59
    {
60
        static::registerModelEvent('pivotDetached', $callback);
61
    }
62
63
    public static function pivotUpdating($callback)
64
    {
65
        static::registerModelEvent('pivotUpdating', $callback);
66
    }
67
68
    public static function pivotUpdated($callback)
69
    {
70
        static::registerModelEvent('pivotUpdated', $callback);
71
    }
72
73
    public function firePivotAttachingEvent($halt = true)
74
    {
75
        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

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