Completed
Push — master ( b739db...92d607 )
by Abdelrahman
02:42
created

UserRepository::menus()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 1
nop 2
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Repositories;
17
18
use Illuminate\Support\Str;
19
use Rinvex\Fort\Traits\HasRoles;
20
use Illuminate\Database\Eloquent\Model;
21
use Illuminate\Contracts\Hashing\Hasher;
22
use Illuminate\Contracts\Foundation\Application;
23
use Rinvex\Fort\Contracts\UserRepositoryContract;
24
use Rinvex\Fort\Contracts\AuthenticatableContract;
25
use Rinvex\Repository\Repositories\EloquentRepository;
26
27
class UserRepository extends EloquentRepository implements UserRepositoryContract
28
{
29
    use HasRoles;
30
31
    /**
32
     * The hasher implementation.
33
     *
34
     * @var \Illuminate\Contracts\Hashing\Hasher
35
     */
36
    protected $hasher;
37
38
    /**
39
     * create a new user repository instance.
40
     *
41
     * @param \Illuminate\Contracts\Foundation\Application $app
42
     *
43
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
44
     */
45
    public function __construct(Application $app, Hasher $hasher)
46
    {
47
        $this->setContainer($app)
48
             ->setHasher($hasher)
49
             ->setRepositoryId('rinvex.fort.user')
50
             ->setModel($app['config']['rinvex.fort.models.user']);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function findByToken($identifier, $token)
57
    {
58
        return $this->where($this->getAuthIdentifierName(), $identifier)
0 ignored issues
show
Documentation Bug introduced by
The method getAuthIdentifierName does not exist on object<Rinvex\Fort\Repositories\UserRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
59
                    ->where($this->getRememberTokenName(), $token)
0 ignored issues
show
Documentation Bug introduced by
The method getRememberTokenName does not exist on object<Rinvex\Fort\Repositories\UserRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
60
                    ->findFirst();
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function findByCredentials(array $credentials)
67
    {
68
        if (empty($credentials)) {
69
            return;
70
        }
71
72
        // First we will add each credential element to the query as a where clause.
73
        // Then we can execute the query and, if we found a user, return it in a
74
        // Eloquent User "model" that will be utilized by the Guard instances.
75
        $model = $this;
76
77
        foreach ($credentials as $key => $value) {
78
            if (! Str::contains($key, 'password')) {
79
                $model = $model->where($key, $value);
80
            }
81
        }
82
83
        return $model->findFirst();
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function updateRememberToken(AuthenticatableContract $user, $token)
90
    {
91
        $this->update($user, [$this->getRememberTokenName() => $token]);
0 ignored issues
show
Documentation Bug introduced by
The method getRememberTokenName does not exist on object<Rinvex\Fort\Repositories\UserRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function validateCredentials(AuthenticatableContract $user, array $credentials)
98
    {
99
        $plain = $credentials['password'];
100
101
        return $this->getHasher()->check($plain, $user->getAuthPassword());
102
    }
103
104
    /**
105
     * Gets the hasher implementation.
106
     *
107
     * @return \Illuminate\Contracts\Hashing\Hasher
108
     */
109
    public function getHasher()
110
    {
111
        return $this->hasher;
112
    }
113
114
    /**
115
     * Sets the hasher implementation.
116
     *
117
     * @param \Illuminate\Contracts\Hashing\Hasher $hasher
118
     *
119
     * @return $this
120
     */
121
    public function setHasher(Hasher $hasher)
122
    {
123
        $this->hasher = $hasher;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Get the menus for the given user.
130
     *
131
     * @param int  $modelId
132
     * @param bool $root
133
     *
134
     * @return mixed
135
     */
136
    public function menus($modelId, $root = true)
137
    {
138
        return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($modelId, $root) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
139
            if (! is_int($modelId) || ! $model = $this->with(['abilities'])->find($modelId)) {
140
                return [];
141
            }
142
143
            $roleAbilities = null;
144
            $callback      = function ($item) {
145
                if (strpos($item, '.index') === false) {
146
                    return str_replace(strrchr($item, '.'), '.index', $item);
147
                } else {
148
                    return 'root';
149
                }
150
            };
151
152
            if (! $model instanceof Role) {
0 ignored issues
show
Bug introduced by
The class Rinvex\Fort\Repositories\Role does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
153
                $roleAbilities = $model->roles()->with(['abilities'])->get()->pluck('abilities')->flatten()->pluck('slug');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
154
            }
155
156
            $userAbilities = $model->abilities->pluck('slug')->merge($roleAbilities)->unique()->groupBy($callback)->toArray();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
157
158
            return $root ? $userAbilities['root'] : $userAbilities;
159
        });
160
    }
161
}
162