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 Failed
Push — development ( 3add70...a51bef )
by José
06:20
created

CampaignController::destroy()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 32
rs 8.8571
cc 3
eloc 16
nc 3
nop 1
1
<?php
2
3
namespace DoeSangue\Http\Controllers\API\V1;
4
5
use DoeSangue\Http\Requests\UpdateCampaignRequest;
6
use DoeSangue\Http\Requests\CreateCampaignRequest;
7
use DoeSangue\Http\Controllers\Controller;
8
use Illuminate\Support\Facades\Mail;
9
use DoeSangue\Models\Donor;
10
use DoeSangue\Models\Campaign;
11
use Carbon\Carbon;
12
use Tymon\JWTAuth\Facades\JWTAuth;
13
14
class CampaignController extends Controller
15
{
16
    public function __construct()
17
    {
18
        $this->middleware('jwt.auth', [ 'except' => [ 'index', 'show' ] ]);
19
    }
20
21
    public function index()
22
    {
23
        $campaigns = Campaign::with('owner')->with('comments')->paginate('12');
24
25
        return response()->json($campaigns, 200);
26
    }
27
28
    public function store(CreateCampaignRequest $request)
29
    {
30
31
        $user = JWTAuth::parseToken()->authenticate();
32
33
        $campaign = new Campaign();
34
        $campaign->title = $request[ 'title' ];
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<DoeSangue\Models\Campaign>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
35
        $campaign->description = $request['description'];
0 ignored issues
show
Documentation introduced by
The property description does not exist on object<DoeSangue\Models\Campaign>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
36
      /*   $imageName = sprintf('%s.%s', hash_file('sha256', $request->file->getPathname()), $request->file->getExtension());
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
37
         if ($request->has('image') && $filesystem->has("uploads/{$imageName}")) {
38
             $image->storePubliclyAs($imageName, 'uploads/');
39
         }
40
        $campaign->image = $imageName;*/
41
      /*  if ($request->has('image')) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
42
            $imageName = 'campaign' . '_'. hash_file('sha256', $request->file('image')->getClientOriginalExtension());
43
            $campaign->image = $imageName;
44
        }*/
45
        //$campaign->image = $request['image'];
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...
46
        $campaign->expires = $request[ 'expires' ];
0 ignored issues
show
Documentation introduced by
The property expires does not exist on object<DoeSangue\Models\Campaign>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
47
        //        $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...
48
        // use auth guard instead of $request['user_id'].
49
        $campaign->user_id = $user->id;
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<DoeSangue\Models\Campaign>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
50
        $campaign->created_at = Carbon::now();
0 ignored issues
show
Documentation introduced by
The property created_at does not exist on object<DoeSangue\Models\Campaign>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
        $campaign->save();
52
53
        // Send mail to users about the new campaign.
54
        //       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...
55
56
        return response()->json(
57
            [
58
            'status_code' => 201,
59
            'message' => 'Campaign added!'
60
            ], 201
61
        );
62
63
    }
64
65
    public function show($id)
66
    {
67
        $campaign = Campaign::find($id);
68
69
        if (!$campaign) {
70
            return response()->json(
71
                [
72
                    'error_code' => 404,
73
                    'error_message' => 'Campaign not found!'
74
                ], 404
75
            );
76
        }
77
78
        return response()->json(
79
            [
80
            'title' => $campaign->title,
81
            'owner' => [
82
              'first_name' => $campaign->owner->first_name,
83
              'last_name' => $campaign->owner->last_name,
84
              'username' => $campaign->owner->username
85
            ],
86
            'dates' => [
87
            'start_at' => $campaign->created_at->format('Y-m-d h:m:s'),
88
            'finish_at' => $campaign->expires
89
            ]
90
            ], 200
91
        );
92
93
    }
94
95
    public function update(UpdateCampaignRequest $request, $id)
96
    {
97
        $campaign = Campaign::find($id);
98
99
        $user = JWTAuth::parseToken()->authenticate();
100
101
        if ($user->id !== $campaign->user_id) {
102
            return response()->json(
103
                [
104
                'message' => 'You haven\'t permission to update this entry'
105
                ], 401
106
            );
107
        }
108
109
        $campaign->title = $request[ 'title' ];
110
        $campaign->expires = $request[ 'expires' ];
111
        $campaign->description = $request[ 'description' ];
112
        $campaign->updated_at = Carbon::now();
113
114
        // Notify error in not found
115
        if (!$campaign) {
116
            return response()->json(
117
                [
118
                  'error_code' => '404',
119
                  'message' => 'Campaign not found!'
120
                ], 404
121
            );
122
        }
123
124
        // If validation pass: update the entry.
125
        $campaign->save();
126
127
        return response()->json(
128
            [
129
            'title' => $campaign->title,
130
            'owner' => [
131
              'first_name' => $campaign->owner->first_name,
132
              'last_name' => $campaign->owner->last_name,
133
              'email' => $campaign->owner->email,
134
              'username' => $campaign->owner->username
135
            ],
136
            'dates' => [
137
            'start_at' => $campaign->created_at->format('d-m-Y h:m:s'),
138
            'finish_at' => $campaign->expires->format('d-m-Y h:m:s')
139
            ]
140
            ], 200
141
        );
142
    }
143
144
    public function destroy($id)
145
    {
146
        $campaign = Campaign::find($id);
147
148
        $user = JWTAuth::parseToken()->authenticate();
149
150
        if ($user->id !== $campaign->user_id) {
151
            return response()->json(
152
                [
153
                'message' => 'You haven\'t permission to delete this entry'
154
                ], 401
155
            );
156
        }
157
158
        // Notify error in not found
159
        if (!$campaign) {
160
            return response()->json(
161
                [
162
                  'error_code' => 404,
163
                  'message' => 'Campaign not found!'
164
                ], 404
165
            );
166
        }
167
168
        $campaign->delete();
169
170
        return response()->json(
171
            [
172
            'message' => 'Campaign deleted'
173
            ], 204
174
        );
175
    }
176
177
}
178