Completed
Push — master ( c0d96b...f1fd23 )
by Maxime
07:29
created

User::getFirstRedirect()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 8.5125
cc 6
eloc 9
nc 5
nop 1
crap 6
1
<?php namespace Distilleries\Expendable\Models;
2
3
use Distilleries\Expendable\Contracts\LockableContract;
4
use Distilleries\Expendable\Helpers\UserUtils;
5
use Distilleries\Expendable\Mails\ResetPasswordNotification;
6
use Distilleries\PermissionUtil\Contracts\PermissionUtilContract;
7
use Illuminate\Auth\Authenticatable;
8
use Illuminate\Auth\Passwords\CanResetPassword;
9
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
10
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
11
use Illuminate\Foundation\Auth\Access\Authorizable;
12
use Illuminate\Notifications\Notifiable;
13
14
class User extends BaseModel implements AuthenticatableContract, CanResetPasswordContract, PermissionUtilContract, LockableContract {
15
16
    use Authenticatable, Authorizable, CanResetPassword, \Distilleries\Expendable\Models\StatusTrait, LockableTrait;
17
    use Notifiable;
18
    protected $tabPermission = [];
19
    protected $fillable = [
20
        'email',
21
        'password',
22
        'status',
23
        'role_id'
24
    ];
25
26
    /**
27
     * The attributes excluded from the model's JSON form.
28
     *
29
     * @var array
30
     */
31 296
    protected $hidden = ['password', 'remember_token'];
32
33 296
    public function role()
34
    {
35
        return $this->belongsTo('Distilleries\Expendable\Models\Role');
36 396
    }
37
38 396
    public static function boot()
39 396
    {
40 297
        parent::boot();
41
        self::observe(new \Distilleries\Expendable\Observers\PasswordObserver);
42
    }
43
44 276
45
46
    public function hasAccess($key)
47 276
    {
48 207
49 272
        if (!empty($this->role->overide_permission))
0 ignored issues
show
Documentation introduced by
The property role does not exist on object<Distilleries\Expendable\Models\User>. 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...
50
        {
51
            return true;
52 4
        }
53
54
        return UserUtils::hasAccess($key);
55 20
    }
56
57 20
    public function getFirstRedirect($left)
58
    {
59
        foreach ($left as $item)
60 20
        {
61 15
62 16
            if (!empty($item['action']) && $this->hasAccess($item['action']))
63
            {
64 8
                return preg_replace('/\/index/i', '', action($item['action']));
65 6
66 8
            } else if (!empty($item['submenu']))
67
            {
68 8
                $redirect = $this->getFirstRedirect($item['submenu']);
69 6
70 5
                if (!empty($redirect))
71
                {
72
                    return $redirect;
73 3
                }
74
75 3
            }
76
77 4
        }
78
79
        return '';
80
    }
81
82
83
    /**
84
     * Send the password reset notification.
85
     *
86
     * @param  string  $token
87
     * @return void
88
     */
89
    public function sendPasswordResetNotification($token)
90
    {
91
        $this->notify(new ResetPasswordNotification($token));
92
    }
93
94
}
95