Completed
Push — master ( bf4c92...3d0f9a )
by Alex
04:55
created

AuthController::createUserApiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\User;
6
use Validator;
7
use App\Http\Controllers\Controller;
8
use Illuminate\Foundation\Auth\ThrottlesLogins;
9
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
10
use Chrisbjr\ApiGuard\Models\ApiKey;
11
12
class AuthController extends Controller
13
{
14
    /*
15
    |--------------------------------------------------------------------------
16
    | Registration & Login Controller
17
    |--------------------------------------------------------------------------
18
    |
19
    | This controller handles the registration of new users, as well as the
20
    | authentication of existing users. By default, this controller uses
21
    | a simple trait to add these behaviors. Why don't you explore it?
22
    |
23
    */
24
25
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
26
27
    /**
28
     * Where to redirect users after login / registration.
29
     *
30
     * @var string
31
     */
32
    protected $redirectTo = '/home';
33
34
    /**
35
     * Create a new authentication controller instance.
36
     *
37
     * @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...
38
     */
39
    public function __construct()
40
    {
41
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
42
    }
43
44
    /**
45
     * Get a validator for an incoming registration request.
46
     *
47
     * @param  array  $data
48
     * @return \Illuminate\Contracts\Validation\Validator
49
     */
50
    protected function validator(array $data)
51
    {
52
        return Validator::make($data, [
53
            'name' => 'required|max:255',
54
            'email' => 'required|email|max:255|unique:users',
55
            'password' => 'required|confirmed|min:6',
56
        ]);
57
    }
58
59
    /**
60
     * Create a new user instance after a valid registration.
61
     *
62
     * @param  array  $data
63
     * @return User
64
     */
65
    protected function create(array $data)
66
    {
67
        $user =  User::create([
68
            'name' => $data['name'],
69
            'email' => $data['email'],
70
            'password' => bcrypt($data['password']),
71
        ]);
72
73
        $user->apiKey = $this->createUserApiKey($user);
0 ignored issues
show
Documentation introduced by
The property apiKey does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
74
75
        return $user;
76
    }
77
78
    private function createUserApiKey(User $user)
79
    {
80
        $apiKey = ApiKey::make($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...
81
        $apiKey->save();
82
        return $apiKey->key;
83
     }
84
}
85