Completed
Push — master ( dc40d0...a009a1 )
by Marcel
05:50
created

UserHasTeams::detachTeams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php namespace Mpociot\Teamwork\Traits;
2
3
/**
4
 * This file is part of Teamwork,
5
 *
6
 * @license MIT
7
 * @package Teamwork
8
 */
9
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Database\Eloquent\ModelNotFoundException;
12
use Illuminate\Support\Facades\Config;
13
use Mpociot\Teamwork\Exceptions\UserNotInTeamException;
14
15
trait UserHasTeams
16
{
17
    /**
18
     * Many-to-Many relations with the user model.
19
     *
20
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
21
     */
22
    public function teams()
23
    {
24
        return $this->belongsToMany( Config::get( 'teamwork.team_model' ),Config::get( 'teamwork.team_user_table' ), 'user_id', 'team_id' )->withTimestamps();
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() 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...
25
    }
26
27
    /**
28
     * has-one relation with the current selected team model.
29
     *
30
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
31
     */
32
    public function currentTeam()
33
    {
34
        return $this->hasOne( Config::get( 'teamwork.team_model' ), 'id', 'current_team_id' );
0 ignored issues
show
Bug introduced by
It seems like hasOne() 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...
35
    }
36
37
    /**
38
     * @return mixed
39
     */
40
    public function ownedTeams()
41
    {
42
        return $this->teams()->where( "owner_id", "=", $this->getKey() );
0 ignored issues
show
Bug introduced by
It seems like getKey() 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...
43
    }
44
45
    /**
46
     * One-to-Many relation with the invite model
47
     * @return mixed
48
     */
49
    public function invites()
50
    {
51
        return $this->hasMany( Config::get('teamwork.invite_model'), 'email', 'email' );
0 ignored issues
show
Bug introduced by
It seems like hasMany() 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...
52
    }
53
54
    /**
55
     * Boot the user model
56
     * Attach event listener to remove the many-to-many records when trying to delete
57
     * Will NOT delete any records if the user model uses soft deletes.
58
     *
59
     * @return void|bool
60
     */
61
    public static function bootUserHasTeams()
62
    {
63
        static::deleting( function ( Model $user )
64
        {
65
            if ( !method_exists( Config::get( 'teamwork.user_model' ), 'bootSoftDeletes' ) )
66
            {
67
                $user->teams()->sync( [ ] );
68
            }
69
            return true;
70
        } );
71
    }
72
73
74
    /**
75
     * Returns if the user owns a team
76
     *
77
     * @return bool
78
     */
79
    public function isOwner()
80
    {
81
        return ( $this->teams()->where( "owner_id", "=", $this->getKey() )->first() ) ? true : false;
0 ignored issues
show
Bug introduced by
It seems like getKey() 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...
82
    }
83
84
    /**
85
     * Wrapper method for "isOwner"
86
     *
87
     * @return bool
88
     */
89
    public function isTeamOwner()
90
    {
91
        return $this->isOwner();
92
    }
93
94
    /**
95
     * @param $team
96
     * @return mixed
97
     */
98
    protected function retrieveTeamId( $team )
99
    {
100
        if ( is_object( $team ) )
101
        {
102
            $team = $team->getKey();
103
        }
104
        if ( is_array( $team ) && isset( $team[ "id" ] ) )
105
        {
106
            $team = $team[ "id" ];
107
        }
108
        return $team;
109
    }
110
111
112
    /**
113
     * Returns if the user owns the given team
114
     *
115
     * @param mixed $team
116
     * @return bool
117
     */
118
    public function isOwnerOfTeam( $team )
119
    {
120
        $team        = $this->retrieveTeamId( $team );
121
        $teamModel   = Config::get( 'teamwork.team_model' );
122
        $teamKeyName = ( new $teamModel() )->getKeyName();
123
        return ( ( new $teamModel )
124
            ->where( "owner_id", "=", $this->getKey() )
0 ignored issues
show
Bug introduced by
It seems like getKey() 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...
125
            ->where( $teamKeyName, "=", $team )->first()
126
        ) ? true : false;
127
    }
128
129
    /**
130
     * Alias to eloquent many-to-many relation's attach() method.
131
     *
132
     * @param mixed $team
133
     * @return $this
134
     */
135
    public function attachTeam( $team )
136
    {
137
        $team        = $this->retrieveTeamId( $team );
138
        /**
139
         * If the user has no current team,
140
         * use the attached one
141
         */
142
        if( is_null( $this->current_team_id ) )
143
        {
144
            $this->current_team_id = $team;
0 ignored issues
show
Bug introduced by
The property current_team_id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
145
            $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() 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...
146
147
            if( $this->relationLoaded('currentTeam') ) {
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() 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...
148
                $this->load('currentTeam');
0 ignored issues
show
Bug introduced by
It seems like load() 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...
149
            }
150
151
        }
152
        
153
        // Reload relation
154
        $this->load('teams');
0 ignored issues
show
Bug introduced by
It seems like load() 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...
155
156
        if( !$this->teams->contains( $team ) )
0 ignored issues
show
Bug introduced by
The property teams does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
157
        {
158
            $this->teams()->attach( $team );
159
160
            if( $this->relationLoaded('teams') ) {
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() 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...
161
                $this->load('teams');
0 ignored issues
show
Bug introduced by
It seems like load() 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...
162
            }
163
        }
164
        return $this;
165
    }
166
167
    /**
168
     * Alias to eloquent many-to-many relation's detach() method.
169
     *
170
     * @param mixed $team
171
     * @return $this
172
     */
173
    public function detachTeam( $team )
174
    {
175
        $team        = $this->retrieveTeamId( $team );
176
        $this->teams()->detach( $team );
177
178
        if( $this->relationLoaded('teams') ) {
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() 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...
179
            $this->load('teams');
0 ignored issues
show
Bug introduced by
It seems like load() 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...
180
        }
181
        
182
        /**
183
         * If the user has no more teams,
184
         * unset the current_team_id
185
         */
186
        if( $this->teams()->count() === 0 || $this->current_team_id === $team )
187
        {
188
            $this->current_team_id = null;
189
            $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() 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...
190
191
            if( $this->relationLoaded('currentTeam') ) {
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() 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...
192
                $this->load('currentTeam');
0 ignored issues
show
Bug introduced by
It seems like load() 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...
193
            }
194
195
        }
196
        return $this;
197
    }
198
199
    /**
200
     * Attach multiple teams to a user
201
     *
202
     * @param mixed $teams
203
     * @return $this
204
     */
205
    public function attachTeams( $teams )
206
    {
207
        foreach ( $teams as $team )
208
        {
209
            $this->attachTeam( $team );
210
        }
211
        return $this;
212
    }
213
214
    /**
215
     * Detach multiple teams from a user
216
     *
217
     * @param mixed $teams
218
     * @return $this
219
     */
220
    public function detachTeams( $teams )
221
    {
222
        foreach ( $teams as $team )
223
        {
224
            $this->detachTeam( $team );
225
        }
226
        return $this;
227
    }
228
229
    /**
230
     * Switch the current team of the user
231
     *
232
     * @param object|array|integer $team
233
     * @return $this
234
     * @throws ModelNotFoundException
235
     * @throws UserNotInTeamException
236
     */
237
    public function switchTeam( $team )
238
    {
239
        if( $team !== 0 && $team !== null )
240
        {
241
            $team        = $this->retrieveTeamId( $team );
242
            $teamModel   = Config::get( 'teamwork.team_model' );
243
            $teamObject  = ( new $teamModel() )->find( $team );
244
            if( !$teamObject )
245
            {
246
                $exception = new ModelNotFoundException();
247
                $exception->setModel( $teamModel );
248
                throw $exception;
249
            }
250
            if( !$teamObject->users->contains( $this->getKey() ) )
0 ignored issues
show
Bug introduced by
It seems like getKey() 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...
251
            {
252
                $exception = new UserNotInTeamException();
253
                $exception->setTeam( $teamObject->name );
254
                throw $exception;
255
            }
256
        }
257
        $this->current_team_id = $team;
258
        $this->save();
0 ignored issues
show
Bug introduced by
It seems like save() 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...
259
260
        if( $this->relationLoaded('currentTeam') ) {
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() 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...
261
            $this->load('currentTeam');
0 ignored issues
show
Bug introduced by
It seems like load() 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...
262
        }
263
        
264
        return $this;
265
    }
266
}
267