ApiLoginRegisterController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
eloc 24
c 2
b 0
f 1
dl 0
loc 51
ccs 24
cts 26
cp 0.9231
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUser() 0 5 1
A __construct() 0 3 1
A login() 0 11 2
A register() 0 16 2
1
<?php
2
3
namespace MedianetDev\LaravelAuthApi\Http\Controllers;
4
5
use Illuminate\Support\Facades\Auth;
6
use MedianetDev\LaravelAuthApi\Http\Helpers\ApiResponse;
7
use MedianetDev\LaravelAuthApi\Http\Requests\ApiUserLoginRequest as LoginRequest;
8
use MedianetDev\LaravelAuthApi\Http\Requests\ApiUserRegisterRequest as RegisterRequest;
9
10
class ApiLoginRegisterController extends Controller
11
{
12
    /**
13
     * Instantiate a new controller instance.
14
     *
15
     * @return void
16
     */
17 4
    public function __construct()
18
    {
19 4
        $this->middleware('auth:apiauth')->only('getUser');
20 4
    }
21
22
    public $successStatus = 200;
23
24 1
    public function register(RegisterRequest $request)
25
    {
26 1
        $user = config('laravel-auth-api.user_model_fqn');
27 1
        $input = $request->only(
28 1
            array_merge(['name', 'email', 'password'], array_keys(config('laravel-auth-api.extra_columns')))
29
        );
30 1
        $input['password'] = bcrypt($input['password']);
31 1
        $user = (new $user)->create($input);
32 1
        if (config('laravel-auth-api.auto_send_verify_email')) {
33
            $user->sendEmailVerificationNotification();
34
        }
35 1
        $success = [];
36 1
        $success['access_token'] = $user->createToken('AppName')->accessToken;
37 1
        $success['user'] = $user->toArray();
38
39 1
        return  ApiResponse::send($success, 1, 201, 'Account created successfully');
40
    }
41
42 1
    public function login(LoginRequest $request)
43
    {
44 1
        if (Auth::guard('apiauthweb')->attempt(['email' => $request->email, 'password' => $request->password])) {
45 1
            $user = Auth::guard('apiauthweb')->user();
46 1
            $success = [];
47 1
            $success['access_token'] = $user->createToken('AppName')->accessToken;
0 ignored issues
show
Bug introduced by
The method createToken() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            $success['access_token'] = $user->/** @scrutinizer ignore-call */ createToken('AppName')->accessToken;
Loading history...
48 1
            $success['user'] = $user->toArray();
49
50 1
            return ApiResponse::send($success, 1, 200);
51
        } else {
52
            return ApiResponse::send(['error' => 'Unauthorised'], 0, 401, 'Password or incorrect identity');
53
        }
54
    }
55
56 1
    public function getUser()
57
    {
58 1
        $user = Auth::guard('apiauth')->user();
59
60 1
        return ApiResponse::send($user, 1, 200);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type Illuminate\Contracts\Auth\Authenticatable; however, parameter $data of MedianetDev\LaravelAuthA...ers\ApiResponse::send() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        return ApiResponse::send(/** @scrutinizer ignore-type */ $user, 1, 200);
Loading history...
61
    }
62
}
63