Completed
Push — master ( 178a80...06b4a8 )
by Manel
05:47
created

Department::setHeadAttribute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
cc 3
eloc 7
nc 4
nop 1
crap 12
1
<?php
2
3
namespace Scool\EnrollmentMobile\Models;
4
5
use Acacha\Names\Traits\Nameable;
6
use Illuminate\Database\Eloquent\Model;
7
//use Scool\Curriculum\Traits\HasManyFamilies;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
8
//use Scool\Curriculum\Traits\HasStudies;
9
use Scool\EnrollmentMobile\Traits\HasManyFamilies;
10
use Scool\EnrollmentMobile\Traits\HasStudies;
11
use Scool\Foundation\User;
12
13
class Department extends Model
14
{
15
    use HasManyFamilies, Nameable, HasStudies , Nameable;
16
    /**
17
     * The attributes that are mass assignable.
18
     *
19
     * @var array
20
     */
21
    protected $fillable = ['name'];
22
    /**
23
     * Get the subdepartments for the department.
24
     */
25
    public function subdepartments()
26
    {
27
        return $this->hasMany(Department::class,'parent');
28
    }
29
    /**
30
     * Get the parent department.
31
     */
32
    public function parent()
33
    {
34
        return $this->belongsTo(Department::class,'parent');
35
    }
36
    /**
37
     * The heads that belong to the department.
38
     */
39
    public function heads()
40
    {
41
        return $this->belongsToMany(User::class,'department_head', 'department_id','user_id')
42
            ->withPivot('main');
43
    }
44
    /**
45
     * The principal head that belong to the department.
46
     */
47
    public function head()
48
    {
49
        return $this->heads()->wherePivot('main', 1)->first();
50
    }
51
    /**
52
     * Set the principal head.
53
     *
54
     * @param  User  $user
55
     * @return void
56
     */
57
    public function setHeadAttribute(User $user)
58
    {
59
        foreach ($this->heads as $head) {
0 ignored issues
show
Documentation introduced by
The property heads does not exist on object<Scool\EnrollmentMobile\Models\Department>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
60
            $this->heads()->updateExistingPivot($head->id, ['main' => false]);
61
        }
62
        if ( ! $this->heads->contains($user) ) {
0 ignored issues
show
Documentation introduced by
The property heads does not exist on object<Scool\EnrollmentMobile\Models\Department>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
63
            $this->heads()->save($user, ['main' => true]);
64
        } else {
65
            $this->heads()->updateExistingPivot($user->id, ['main' => true]);
66
        }
67
    }
68
}
69