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

Completed
Pull Request — development (#48)
by José
04:01
created

CampaignController::update()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 48
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 48
ccs 0
cts 23
cp 0
rs 9.125
cc 3
eloc 26
nc 3
nop 2
crap 12
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);
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' ];
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...
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));
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...
53
54
        return response()->json(
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(
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,
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...
109
              // '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...
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(
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