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

Passed
Branch laravel-55 (e4caf7)
by José
06:15
created

CampaignController::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace DoeSangue\Http\Controllers\API\V1\User;
4
5
use Illuminate\Http\Request;
6
use DoeSangue\Http\Controllers\Controller;
7
8
use DoeSangue\Models\Campaign;
9
10
class CampaignController extends Controller
11
{
12
13
    /**
14
     * Get all campaigns from current logged-in user.
15
     *
16
     * @return void
17
     */
18
    public function all()
19
    {
20
21
        $user = JWTAuth::parseToken()->authenticate();
22
23
         // If the token is invalid
24
        if (!$user) {
25
            return response()->json([ 'invalid_user' ], 401);
26
        }
27
28
        $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...
29
    }
30
31
    /**
32
     * Create a new campaign
33
     *
34
     * @param  CreateCampaignRequest $request
35
     * @return void
36
     */
37
    public function store(CreateCampaignRequest $request)
38
    {
39
40
        $user = JWTAuth::parseToken()->authenticate();
41
42
        $campaign = new Campaign();
43
        $campaign->title = $request[ 'title' ];
44
        $campaign->description = $request['description'];
45
        $campaign->expires = $request[ 'expires' ];
46
        //        $campaign->user_id = $request[ 'user_id' ];
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
        // use auth guard instead of $request['user_id'].
48
        $campaign->user_id = $user->id;
49
        $campaign->created_at = Carbon::now();
50
        $campaign->save();
51
52
        // Send mail to users about the new campaign.
53
        //       Mail::to($campaign->owner->email)->send(new CampaignPublished($campaign));
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
54
55
        return response()->json(
56
            [
57
            'status_code' => 201,
58
            'message' => 'Campaign added!'
59
            ], 201
60
        );
61
62
    }
63
64
    /**
65
     * Update details of a campaign
66
     *
67
     * @param  UpdateCampaignRequest $request
68
     * @param  integer               $id
69
     * @return void
70
     */
71
    public function update(UpdateCampaignRequest $request, $id)
72
    {
73
        $campaign = Campaign::find($id);
74
75
        $user = JWTAuth::parseToken()->authenticate();
76
77
        if ($user->id !== $campaign->user_id) {
78
            return response()->json(
79
                [
80
                'message' => 'You haven\'t permission to update this entry'
81
                ], 401
82
            );
83
        }
84
85
        $campaign->title = $request[ 'title' ];
86
        $campaign->expires = $request[ 'expires' ];
87
        $campaign->description = $request[ 'description' ];
88
        $campaign->updated_at = Carbon::now();
89
90
        // Notify error in not found
91
        if (!$campaign) {
92
            return response()->json(
93
                [
94
                  'error_code' => '404',
95
                  'message' => 'Campaign not found!'
96
                ], 404
97
            );
98
        }
99
100
        // If validation pass: update the entry.
101
        $campaign->save();
102
103
        return response()->json(
104
            [
105
            'title' => $campaign->title,
106
            'owner' => [
107
              'first_name' => $campaign->owner->first_name,
108
              'last_name' => $campaign->owner->last_name,
109
             // 'email' => $campaign->owner->email,
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
110
             // 'username' => $campaign->owner->username
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
111
            ],
112
            'dates' => [
113
            'start_at' => $campaign->created_at->format('d-m-Y h:m:s'),
114
            'finish_at' => $campaign->expires->format('d-m-Y h:m:s')
115
            ]
116
            ], 200
117
        );
118
    }
119
120
    /**
121
     * Delete the campaign from platform.
122
     *
123
     * @param  integer $id
124
     * @return void
125
     */
126
    public function destroy($id)
127
    {
128
        $campaign = Campaign::find($id);
129
130
        $user = JWTAuth::parseToken()->authenticate();
131
132
        if ($user->id !== $campaign->user_id) {
133
            return response()->json(
134
                [
135
                'message' => 'You haven\'t permission to delete this entry'
136
                ], 401
137
            );
138
        }
139
140
        // Notify error in not found
141
        if (!$campaign) {
142
            return response()->json(
143
                [
144
                  'error_code' => 404,
145
                  'message' => 'Campaign not found!'
146
                ], 404
147
            );
148
        }
149
150
        $campaign->delete();
151
152
        return response()->json(
153
            [
154
            'message' => 'Campaign deleted'
155
            ], 204
156
        );
157
    }
158
159
}
160