Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Test Setup Failed
Pull Request — development (#68)
by José
06:06
created

AuthenticateController::authenticate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace GiveBlood\Units\Authentication\Http\Controllers\Auth;
4
5
use JWTAuth;
6
use Tymon\JWTAuth\Exceptions\JWTException;
7
use GiveBlood\Support\Http\Controllers\Controller;
8
use Illuminate\Http\Request;
9
use Carbon\Carbon;
10
use GiveBlood\Support\Http\Requests\RegisterUserRequest;
11
use GiveBlood\Mail\UserCreated;
12
use Illuminate\Support\Facades\Mail;
13
use GiveBlood\Modules\Users\User;
14
15
class AuthenticateController extends Controller
16
{
17
      /**
18
       * Authenticate the user
19
       *
20
       * @param  Request $request
21
       * @return \Illuminate\Http\JsonResponse
22
       */
23
    public function authenticate(Request $request)
24
    {
25
26
        try {
27
28
            // grab credentials from the request
29
            // attempt to verify the credentials and create a token for the user
30
            if (!$token = JWTAuth::attempt(
31
                $request->only('email', 'password'), [
32
                'exp' => Carbon::now()->addWeek()->timestamp,
33
                ]
34
            )
35
            ) {
36
                return response()->json([ 'error' => 'invalid_credentials' ], 401);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
37
            }
38
        } catch (JWTException $e) {
39
            // something went wrong whilst attempting to encode the token
40
            return response()->json([ 'error' => 'could_not_create_token' ], 500);
41
        }
42
43
        // all good so return the token
44
        return response()->json(
45
            [
46
            'access_token' => $token,
47
            'token_type' => 'Bearer'
48
            ], 200
49
        );
50
    }
51
52
    /**
53
     * Register a new User
54
     *
55
     * @param  RegisterUserRequest $request
56
     * @return \Illuminate\Http\JsonResponse
57
     */
58
    public function register(RegisterUserRequest $request)
59
    {
60
        $user = User::create(
61
            [
62
            'first_name' => $request->first_name,
63
            'last_name' => $request->last_name,
64
            'email' => $request->email,
65
            'username' => $request->username,
66
            'phone' => $request->phone,
67
            'country_code' => $request->country_code,
68
            'bio' => $request->bio,
69
            'blood_type_id' => $request->blood_type_id,
70
            'birthdate' => $request->birthdate,
71
            'password' => bcrypt($request->password),
72
            ]
73
        );
74
75
        // Send mail to user
76
        Mail::to($user->email)->send(new UserCreated($user));
77
78
79
80
        $token = JWTAuth::attempt($request->only('email', 'password'));
81
82
        // all good so return the token
83
        return response()->json(
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
84
            [
85
            'access_token' => $token,
86
            'token_type' => 'Bearer'
87
            ], 201
88
        );
89
    }
90
91
    /**
92
     * Invalidate and log out the user
93
     *
94
     * @param  Request $request
95
     * @return \Illuminate\Http\JsonResponse
96
     */
97
    public function logout(Request $request)
98
    {
99
        $this->validate($request, [ 'token' => 'required' ]);
100
101
        try {
102
            JWTAuth::invalidate($request->input('token'));
103
            return response()->json(
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
104
                [
105
                'success' => true
106
                ]
107
            );
108
        } catch (JWTException $e) {
109
            // Something went wrong whilst attemping to encode the token
110
            return response()->json([ 'success' => false, 'error' => 'Failed to logout, please try again.', 500 ]);
111
        }
112
    }
113
114
}
115