UserRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace App\Repositories;
4
5
use App\User;
6
use Auth;
7
8
class UserRepository extends Repository
9
{
10
    /**
11
     * @var RolesRepository
12
     */
13
    private $roles;
14
15
    /**
16
     * @var
17
     */
18
    private $profile;
19
20
    /**
21
     * UserRepository constructor.
22
     * @param RolesRepository $rolesRepository
23
     * @param ProfileRepository $profileRepository
24
     */
25
    public function __construct(
26
        RolesRepository $rolesRepository = null,
27
        ProfileRepository $profileRepository = null
28
    )
29
    {
30
        $this->roles = $rolesRepository;
31
        $this->profile = $profileRepository;
32
    }
33
34
    /**
35
     * @return User
36
     */
37
    public function getModel()
38
    {
39
        return new User();
40
    }
41
42
    /**
43
     * @return User
44
     */
45
    static public function staticGetModel()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
46
    {
47
        return new User();
48
    }
49
50
    /**
51
     * @param array $data
52
     * @param int $confirmed
53
     * @return User
54
     */
55
    public function createSimpleUser(array $data, $confirmed = 0)
56
    {
57
        $user = self::getModel()
58
            ->create([
59
                'email' => $data['email'],
60
                'password' => $this->hashPassword($data['password']),
61
                'role_id' => $this->getSimpleUser()->id,
62
                'confirmation_code' => str_random(30),
63
                'confirmed' => $confirmed
64
            ]);
65
66
        $this->profile->getModel()->create([
67
            'user_id' => $user->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\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...
68
            'firstname' => $data['firstname'],
69
            'lastname' => $data['lastname'],
70
            'phone' => isset($data['phone']) ? $data['phone'] : ''
71
        ]);
72
73
        return $user;
74
    }
75
76
    /**
77
     * @param array $data
78
     * @return \App\User
79
     */
80
    public function createAdminUser(array $data)
81
    {
82
        return self::getModel()
83
            ->create([
84
                'email' => $data['email'],
85
                'password' => bcrypt($data['password']),
86
                'role_id' => $this->getAdminRole()->id,
87
                'confirmed' => 1
88
            ]);
89
    }
90
91
    /**
92
     * @return mixed
93
     */
94
    public function getSimpleUser()
95
    {
96
        return $this->roles->getSimpleUserRole();
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102
    public function getAdminRole()
103
    {
104
        return $this->roles->getAdminRole();
105
    }
106
107
    /**
108
     * Get user by confirmation code.
109
     *
110
     * @param $code
111
     * @return mixed
112
     */
113
    public function getByConfirmationCode($code)
114
    {
115
        return self::getModel()
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<App\User>? 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...
116
            ->where('confirmation_code', $code)
117
            ->first();
118
    }
119
120
    /**
121
     * @param $user
122
     */
123
    public function confirmate($user)
124
    {
125
        $user->confirmed = (int)true;
126
        $user->save();
127
    }
128
129
    /**
130
     * Get user by email.
131
     *
132
     * @param $email
133
     * @return mixed
134
     */
135
    public function getByEmail($email)
136
    {
137
        return $this->getModel()
0 ignored issues
show
Documentation Bug introduced by
The method whereEmail does not exist on object<App\User>? 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...
138
            ->whereEmail($email)
139
            ->first();
140
    }
141
142
    /**
143
     * @param array $data
144
     */
145
    public function update_user(array $data)
146
    {
147
        Auth::user()->update([
148
            'name' => $data['fname'],
149
            'email' => $data['email']
150
        ]);
151
152
        Auth::user()->profile->update([
153
            'firstname' => $data['fname'],
154
            'lastname' => $data['lname'],
155
            'phone' => $data['phone']
156
        ]);
157
    }
158
159
    /**
160
     * @param $password
161
     * @return string
162
     */
163
    public function hashPassword($password)
164
    {
165
        return bcrypt($password);
166
    }
167
168
    /**
169
     * @param $password
170
     */
171
    public function updatePassword($password)
172
    {
173
        Auth::user()->update([
174
            'password' => $this->hashPassword($password),
175
        ]);
176
    }
177
178
    /**
179
     * Check by email if user exists.
180
     *
181
     * @param $email
182
     * @return bool
183
     */
184
    public function checkIfUserExists($email)
185
    {
186
        return (bool)$this->getByEmail($email);
187
    }
188
}