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

CampaignController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 150
ccs 0
cts 56
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 12 2
A store() 0 26 1
A update() 0 48 3
A destroy() 0 32 3
1
<?php
2
3
namespace DoeSangue\Http\Controllers\API\V1\User;
4
5
use DoeSangue\Http\Controllers\Controller;
6
7
use DoeSangue\Models\Campaign;
8
9
class CampaignController extends Controller
10
{
11
12
    /**
13
     * Get all campaigns from current logged-in user.
14
     *
15
     * @return \Illuminate\Http\JsonResponse|null
16
     */
17
    public function all()
18
    {
19
20
        $user = JWTAuth::parseToken()->authenticate();
21
22
          // If the token is invalid
23
        if (!$user) {
24
            return response()->json([ 'invalid_user' ], 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...
25
        }
26
27
        $campaigns = $user->campaigns()->get();
0 ignored issues
show
Unused Code introduced by
$campaigns is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
28
    }
29
30
    /**
31
     * Create a new campaign
32
     *
33
     * @param  CreateCampaignRequest $request
34
     * @return \Illuminate\Http\JsonResponse
35
     */
36
    public function store(CreateCampaignRequest $request)
37
    {
38
39
        $user = JWTAuth::parseToken()->authenticate();
40
41
        $campaign = new Campaign();
42
        $campaign->title = $request[ 'title' ];
43
        $campaign->description = $request[ 'description' ];
44
        $campaign->expires = $request[ 'expires' ];
45
        //        $campaign->user_id = $request[ 'user_id' ];
46
        // use auth guard instead of $request['user_id'].
47
        $campaign->user_id = $user->id;
48
        $campaign->created_at = Carbon::now();
49
        $campaign->save();
50
51
        // Send mail to users about the new campaign.
52
        //       Mail::to($campaign->owner->email)->send(new CampaignPublished($campaign));
53
54
        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...
55
            [
56
            'status_code' => 201,
57
            'message' => 'Campaign added!'
58
            ], 201
59
        );
60
61
    }
62
63
    /**
64
     * Update details of a campaign
65
     *
66
     * @param  UpdateCampaignRequest $request
67
     * @param  integer               $id
68
     * @return \Illuminate\Http\JsonResponse
69
     */
70
    public function update(UpdateCampaignRequest $request, $id)
71
    {
72
        $campaign = Campaign::find($id);
73
74
        $user = JWTAuth::parseToken()->authenticate();
75
76
        if ($user->id !== $campaign->user_id) {
77
            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...
78
                [
79
                'message' => 'You haven\'t permission to update this entry'
80
                ], 401
81
            );
82
        }
83
84
        $campaign->title = $request[ 'title' ];
85
        $campaign->expires = $request[ 'expires' ];
86
        $campaign->description = $request[ 'description' ];
87
        $campaign->updated_at = Carbon::now();
88
89
        // Notify error in not found
90
        if (!$campaign) {
91
            return response()->json(
92
                [
93
                  'error_code' => '404',
94
                  'message' => 'Campaign not found!'
95
                ], 404
96
            );
97
        }
98
99
        // If validation pass: update the entry.
100
        $campaign->save();
101
102
        return response()->json(
103
            [
104
            'title' => $campaign->title,
105
            'owner' => [
106
              'first_name' => $campaign->owner->first_name,
107
              'last_name' => $campaign->owner->last_name,
108
              // 'email' => $campaign->owner->email,
109
              // 'username' => $campaign->owner->username
110
            ],
111
            'dates' => [
112
            'start_at' => $campaign->created_at->format('d-m-Y h:m:s'),
113
            'finish_at' => $campaign->expires->format('d-m-Y h:m:s')
114
            ]
115
            ], 200
116
        );
117
    }
118
119
    /**
120
     * Delete the campaign from platform.
121
     *
122
     * @param  integer $id
123
     * @return \Illuminate\Http\JsonResponse
124
     */
125
    public function destroy($id)
126
    {
127
        $campaign = Campaign::find($id);
128
129
        $user = JWTAuth::parseToken()->authenticate();
130
131
        if ($user->id !== $campaign->user_id) {
132
            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...
133
                [
134
                'message' => 'You haven\'t permission to delete this entry'
135
                ], 401
136
            );
137
        }
138
139
        // Notify error in not found
140
        if (!$campaign) {
141
            return response()->json(
142
                [
143
                  'error_code' => 404,
144
                  'message' => 'Campaign not found!'
145
                ], 404
146
            );
147
        }
148
149
        $campaign->delete();
150
151
        return response()->json(
152
            [
153
            'message' => 'Campaign deleted'
154
            ], 204
155
        );
156
    }
157
158
}
159