Completed
Pull Request — master (#62)
by
unknown
02:14
created

UserHasTeams::getCurrentTeamAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Events\UserJoinedTeam;
14
use Mpociot\Teamwork\Events\UserLeftTeam;
15
use Mpociot\Teamwork\Exceptions\UserNotInTeamException;
16
17
trait UserHasTeams
18
{
19
    /**
20
     * Many-to-Many relations with the user model.
21
     *
22
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
23
     */
24
    public function teams()
25
    {
26
        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...
27
    }
28
29
    /**
30
     * Accessor for the currentTeam method.
31
     *
32
     * @return Mpociot\Teamwork\TeamworkTeam
33
     */
34
    public function getCurrentTeamAttribute()
35
    {
36
        return $this->currentTeam();
37
    }
38
39
    /**
40
     * Get the team that user is currently viewing.
41
     *
42
     * @return Many Mpociot\Teamwork\TeamworkTeam
43
     */
44
    public function currentTeam()
45
    {
46
        return $this->teams->find($this->current_team_id);
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...
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...
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    public function ownedTeams()
53
    {
54
        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...
55
    }
56
57
    /**
58
     * One-to-Many relation with the invite model
59
     * @return mixed
60
     */
61
    public function invites()
62
    {
63
        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...
64
    }
65
66
    /**
67
     * Boot the user model
68
     * Attach event listener to remove the many-to-many records when trying to delete
69
     * Will NOT delete any records if the user model uses soft deletes.
70
     *
71
     * @return void|bool
72
     */
73
    public static function bootUserHasTeams()
74
    {
75
        static::deleting( function ( Model $user )
76
        {
77
            if ( !method_exists( Config::get( 'teamwork.user_model' ), 'bootSoftDeletes' ) )
78
            {
79
                $user->teams()->sync( [ ] );
80
            }
81
            return true;
82
        } );
83
    }
84
85
86
    /**
87
     * Returns if the user owns a team
88
     *
89
     * @return bool
90
     */
91
    public function isOwner()
92
    {
93
        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...
94
    }
95
96
    /**
97
     * Wrapper method for "isOwner"
98
     *
99
     * @return bool
100
     */
101
    public function isTeamOwner()
102
    {
103
        return $this->isOwner();
104
    }
105
106
    /**
107
     * @param $team
108
     * @return mixed
109
     */
110
    protected function retrieveTeamId( $team )
111
    {
112
        if ( is_object( $team ) )
113
        {
114
            $team = $team->getKey();
115
        }
116
        if ( is_array( $team ) && isset( $team[ "id" ] ) )
117
        {
118
            $team = $team[ "id" ];
119
        }
120
        return $team;
121
    }
122
123
124
    /**
125
     * Returns if the user owns the given team
126
     *
127
     * @param mixed $team
128
     * @return bool
129
     */
130
    public function isOwnerOfTeam( $team )
131
    {
132
        $team        = $this->retrieveTeamId( $team );
133
        $teamModel   = Config::get( 'teamwork.team_model' );
134
        $teamKeyName = ( new $teamModel() )->getKeyName();
135
        return ( ( new $teamModel )
136
            ->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...
137
            ->where( $teamKeyName, "=", $team )->first()
138
        ) ? true : false;
139
    }
140
141
    /**
142
     * Alias to eloquent many-to-many relation's attach() method.
143
     *
144
     * @param mixed $team
145
     * @param array $pivotData
146
     * @return $this
147
     */
148
    public function attachTeam( $team, $pivotData = [] )
149
    {
150
        $team        = $this->retrieveTeamId( $team );
151
        /**
152
         * If the user has no current team,
153
         * use the attached one
154
         */
155
        if( is_null( $this->current_team_id ) )
156
        {
157
            $this->current_team_id = $team;
158
            $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...
159
160
            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...
161
                $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...
162
            }
163
164
        }
165
        
166
        // Reload relation
167
        $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...
168
169
        if( !$this->teams->contains( $team ) )
170
        {
171
            $this->teams()->attach( $team, $pivotData );
172
173
            event(new UserJoinedTeam($this, $team));
174
175
            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...
176
                $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...
177
            }
178
        }
179
        return $this;
180
    }
181
182
    /**
183
     * Alias to eloquent many-to-many relation's detach() method.
184
     *
185
     * @param mixed $team
186
     * @return $this
187
     */
188
    public function detachTeam( $team )
189
    {
190
        $team        = $this->retrieveTeamId( $team );
191
        $this->teams()->detach( $team );
192
193
        event(new UserLeftTeam($this, $team));
194
195
        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...
196
            $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...
197
        }
198
        
199
        /**
200
         * If the user has no more teams,
201
         * unset the current_team_id
202
         */
203
        if( $this->teams()->count() === 0 || $this->current_team_id === $team )
204
        {
205
            $this->current_team_id = null;
206
            $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...
207
208
            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...
209
                $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...
210
            }
211
212
        }
213
        return $this;
214
    }
215
216
    /**
217
     * Attach multiple teams to a user
218
     *
219
     * @param mixed $teams
220
     * @return $this
221
     */
222
    public function attachTeams( $teams )
223
    {
224
        foreach ( $teams as $team )
225
        {
226
            $this->attachTeam( $team );
227
        }
228
        return $this;
229
    }
230
231
    /**
232
     * Detach multiple teams from a user
233
     *
234
     * @param mixed $teams
235
     * @return $this
236
     */
237
    public function detachTeams( $teams )
238
    {
239
        foreach ( $teams as $team )
240
        {
241
            $this->detachTeam( $team );
242
        }
243
        return $this;
244
    }
245
246
    /**
247
     * Switch the current team of the user
248
     *
249
     * @param object|array|integer $team
250
     * @return $this
251
     * @throws ModelNotFoundException
252
     * @throws UserNotInTeamException
253
     */
254
    public function switchTeam( $team )
255
    {
256
        if( $team !== 0 && $team !== null )
257
        {
258
            $team        = $this->retrieveTeamId( $team );
259
            $teamModel   = Config::get( 'teamwork.team_model' );
260
            $teamObject  = ( new $teamModel() )->find( $team );
261
            if( !$teamObject )
262
            {
263
                $exception = new ModelNotFoundException();
264
                $exception->setModel( $teamModel );
265
                throw $exception;
266
            }
267
            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...
268
            {
269
                $exception = new UserNotInTeamException();
270
                $exception->setTeam( $teamObject->name );
271
                throw $exception;
272
            }
273
        }
274
        $this->current_team_id = $team;
275
        $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...
276
277
        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...
278
            $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...
279
        }
280
        
281
        return $this;
282
    }
283
}
284