Completed
Push — master ( 4f4c57...90ef53 )
by Abdelrahman
02:43
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\Contracts\Hashing\Hasher;
21
use Illuminate\Contracts\Foundation\Application;
22
use Rinvex\Fort\Contracts\UserRepositoryContract;
23
use Rinvex\Fort\Contracts\AuthenticatableContract;
24
use Rinvex\Repository\Repositories\EloquentRepository;
25
26
class UserRepository extends EloquentRepository implements UserRepositoryContract
27
{
28
    use HasRoles;
29
30
    /**
31
     * The hasher implementation.
32
     *
33
     * @var \Illuminate\Contracts\Hashing\Hasher
34
     */
35
    protected $hasher;
36
37
    /**
38
     * create a new user repository instance.
39
     *
40
     * @param \Illuminate\Contracts\Foundation\Application $app
41
     *
42
     * @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...
43
     */
44
    public function __construct(Application $app, Hasher $hasher)
45
    {
46
        $this->setContainer($app)
47
             ->setHasher($hasher)
48
             ->setRepositoryId('rinvex.fort.user')
49
             ->setModel($app['config']['rinvex.fort.models.user']);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function findByToken($identifier, $token)
56
    {
57
        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...
58
                    ->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...
59
                    ->findFirst();
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function findByCredentials(array $credentials)
66
    {
67
        if (empty($credentials)) {
68
            return;
69
        }
70
71
        // First we will add each credential element to the query as a where clause.
72
        // Then we can execute the query and, if we found a user, return it in a
73
        // Eloquent User "model" that will be utilized by the Guard instances.
74
        $model = $this;
75
76
        foreach ($credentials as $key => $value) {
77
            if (! Str::contains($key, 'password')) {
78
                $model = $model->where($key, $value);
79
            }
80
        }
81
82
        return $model->findFirst();
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function create(array $attributes = [])
89
    {
90
        $social = array_pull($attributes, 'social', false);
91
92
        // Fire the register start event
93
        $this->getContainer('events')->fire('rinvex.fort.register'.$social.'.start', [$attributes]);
94
95
        // Prepare registration data
96
        $attributes['password'] = bcrypt(! $social ? $attributes['password'] : str_random());
97
        $attributes['active']   = ! config('rinvex.fort.registration.moderated');
98
99
        // Create new user
100
        $user = parent::create($attributes);
101
102
        // Fire the register success event
103
        $this->getContainer('events')->fire('rinvex.fort.register'.$social.'.success', [$user]);
104
105
        // Send verification if required
106
        if (! $social && config('rinvex.fort.verification.required')) {
107
            return app('rinvex.fort.verifier')->broker()->sendVerificationLink(['email' => $attributes['email']]);
108
        }
109
110
        // Registration completed successfully
111
        return ! $social ? static::AUTH_REGISTERED : $user;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function updateRememberToken(AuthenticatableContract $user, $token)
118
    {
119
        $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...
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function validateCredentials(AuthenticatableContract $user, array $credentials)
126
    {
127
        $plain = $credentials['password'];
128
129
        return $this->getHasher()->check($plain, $user->getAuthPassword());
130
    }
131
132
    /**
133
     * Gets the hasher implementation.
134
     *
135
     * @return \Illuminate\Contracts\Hashing\Hasher
136
     */
137
    public function getHasher()
138
    {
139
        return $this->hasher;
140
    }
141
142
    /**
143
     * Sets the hasher implementation.
144
     *
145
     * @param \Illuminate\Contracts\Hashing\Hasher $hasher
146
     *
147
     * @return $this
148
     */
149
    public function setHasher(Hasher $hasher)
150
    {
151
        $this->hasher = $hasher;
152
153
        return $this;
154
    }
155
}
156