GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 7169be...a6690c )
by Danger
02:22
created

WorkspaceController   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 518
Duplicated Lines 8.88 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 44
c 6
b 2
f 0
lcom 1
cbo 0
dl 46
loc 518
rs 8.3396

17 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 22 1
A getSessionId() 0 4 1
B getWorkspaceList() 0 27 2
A createWorkspace() 8 61 2
B putWorkspace() 0 37 1
B getWorkspace() 0 31 3
A deleteWorkspace() 0 14 1
A share() 0 23 1
B join() 4 38 3
B postPart() 7 37 3
B getPart() 0 40 4
A getPositionInArray() 0 10 3
B putPart() 6 58 7
A deletePart() 5 19 2
A getPoint() 0 15 4
B checkin() 16 44 5
A deleteCheckin() 0 16 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like WorkspaceController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WorkspaceController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace BitPrepared\Bundle\D1b0Workspace\Controller\V1;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
use Silex\Application;
8
use Silex\Api\ControllerProviderInterface;
9
use RedBeanPHP\Facade as R;
10
11
class WorkspaceController implements ControllerProviderInterface
12
{
13
14
    public $POINT_FOR_USING_A_CONQUERED_BADGE = 200;
15
    public $POINT_FOR_USING_A_BADGE = 100;
16
    public $POINT_DEFAULT = 50;
17
18
    private $app;
19
20
    public function connect(Application $app)
21
    {
22
        $this->app = $app;
23
        $factory = $app['controllers_factory'];
24
        # il mount point e' precedente e non serve prima
25
        $this->app['db'];
26
        R::fancyDebug(TRUE);
27
        $factory->get('/', array($this, 'getWorkspaceList'));
28
        $factory->post('/', array($this, 'createWorkspace'));
29
        $factory->get('/{id}', array($this, 'getWorkspace'));
30
        $factory->put('/{id}', array($this, 'putWorkspace'));
31
        $factory->delete('/{id}', array($this, 'deleteWorkspace'));
32
        $factory->get('/{id}/share', array($this, 'share'));
33
        $factory->post('/join', array($this, 'join'));
34
        $factory->post('/{id}/part', array($this, 'postPart'));
35
        $factory->get('/{id}/part/{part_id}', array($this, 'getPart'));
36
        $factory->put('/{id}/part/{part_id}', array($this, 'putPart'));
37
        $factory->delete('/{id}/part/{part_id}', array($this, 'deletePart'));
38
        $factory->post('/{id}/part/{part_id}/checkin', array($this, 'checkin'));
39
        $factory->delete('/{id}/part/{part_id}/checkin', array($this, 'deleteCheckin'));
40
        return $factory;
41
    }
42
    public function getSessionId() {
43
        $user_id = $this->app['session']->get('user')['id'];
44
        return $user_id;
45
    }
46
    public function getWorkspaceList(Request $request)
47
    {
48
        $user_id = $this->getSessionId();
49
        $workspaces = R::getAll("SELECT ws.id,
50
                                          ws.title,
51
                                          ws.description,
52
                                          ws.environment,
53
                                          ws.completed
54
                                          FROM userworkspace AS uws
55
                                          LEFT JOIN workspace AS ws
56
                                          ON uws.workspace = ws.id
57
                                          WHERE uws.user = ?",[$user_id]);
58
        $list = [];
59
        foreach ($workspaces as $ws) {
60
            array_push($list, [
61
                "id"=>$ws['id'],
62
                "title"=>$ws['title'],
63
                "description"=>$ws['description'],
64
                "environment"=>$ws['environment'],
65
                "point"=>0, //TODO fare una view con i point già calcolati per il ws
66
                "completed"=>$ws['completed'],
67
            ]);
68
        }
69
        $headers = [];
70
        return JsonResponse::create($list, 200, $headers)->setSharedMaxAge(300);
71
72
    }
73
    public function createWorkspace(Request $request)
74
    {
75
        $user_id = $this->getSessionId();
76
        $data = json_decode($request->getContent(), true);
77
        //TODO validate json_decode
78
        $title = $data['title'];
79
        $description = $data['description'];
80
        $environment = $data['environment'];
81
82
        $patrol = $data['team']['patrol'];
83
        $unit = $data['team']['unit'];
84
        $group = $data['team']['group'];
85
86
        //save the workspace get id
87
        $ws = R::dispense("workspace");
88
            $ws->title = $title;
89
            $ws->description = $description;
90
            $ws->environment = $environment;
91
            $ws->completed = false;
92
            $ws->inserttime = date('Y-m-d H:i:s');
93
            $ws->lastupdatetime = date('Y-m-d H:i:s');
94
        $id = R::store($ws);
95
96
        //save the team
97
        $team = R::dispense("team");
98
            $team->workspace = $id;
99
            $team->patrol = $patrol;
100
            $team->unit = $unit;
101
            $team->group = $group;
102
        $team_id = R::store($team);
0 ignored issues
show
Unused Code introduced by
$team_id 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...
103
104
        //create a phantom part to add badge
105
        $part = R::dispense("part");
106
            $part->workspace = $id;
107
            $part->user = $user_id;
108
            $part->inserttime = date('Y-m-d H:i:s');
109
            $part->lastupdatetime = date('Y-m-d H:i:s');
110
            $part->totalpoint = 0;
111
        $part_id = R::store($part);
112
113
        //add the badge to the project
114 View Code Duplication
        foreach ($data['badges'] as $badge_id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
            //TODO insert those badge as first hidden post
116
            $pb = R::dispense("partbadge");
117
                $pb->badge = $badge_id;
118
                $pb->part = $part_id;
119
                $pb->inserttime = date('Y-m-d H:i:s');
120
            $tmp = R::store($pb);
0 ignored issues
show
Unused Code introduced by
$tmp 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...
121
        }
122
123
        //add the workspace created to the user as owner
124
        $usw = R::dispense("userworkspace");
125
            $usw->user = $user_id;
126
            $usw->workspace = $id;
127
            $usw->inserttime = date('Y-m-d H:i:s');
128
        R::store($usw);
129
130
        $res = ["id" => $id];
131
        $headers = [];
132
        return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300);
133
    }
134
135
    public function putWorkspace($id,Request $request)
136
    {
137
        $user_id = $this->getSessionId();
0 ignored issues
show
Unused Code introduced by
$user_id 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...
138
        $data = json_decode($request->getContent(), true);
139
        //TODO validate json_decode
140
141
        $title = $data['title'];
142
        $description = $data['description'];
143
        $environment = $data['environment'];
144
145
        $patrol = $data['team']['patrol'];
146
        $unit = $data['team']['unit'];
147
        $group = $data['team']['group'];
148
149
        $wp = R::load("workspace",$id);
0 ignored issues
show
Unused Code introduced by
$wp 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...
150
            $ws->title = $title;
0 ignored issues
show
Bug introduced by
The variable $ws does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
151
            $ws->description = $description;
152
            $ws->environment = $environment;
153
            $ws->completed = false;
154
            $ws->lastupdatetime = date('Y-m-d H:i:s');
155
        $id = R::store($ws);
156
157
        //save the team
158
        $team = R::findOne("team","workspace = ?",[$id]);
159
            $team->patrol = $patrol;
160
            $team->unit = $unit;
161
            $team->group = $group;
162
        $team_id = R::store($team);
0 ignored issues
show
Unused Code introduced by
$team_id 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...
163
164
        //TODO WE DELIBERATLY IGNORE ANY CHANGE IN BADGES AND PARTS, THEY MUST NOT BE EDITED HERE!!!! AND IF YOU DID WE DONT CARE!
165
166
167
        $response = new Response();
168
        $response->headers->set('Content-Type', 'text/html');
169
        $response->setStatusCode(Response::HTTP_NO_CONTENT);
170
        $response->setSharedMaxAge(300);
171
    }
172
173
    public function getWorkspace($id, Request $request) {
174
        $user_id = $this->getSessionId();
0 ignored issues
show
Unused Code introduced by
$user_id 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...
175
        //TODO controllare che l'utente abbia diritto a vedere questo workspace
176
177
        $workspace = R::findOne("workspace", "id = ?", [$id]);
178
        $part = R::findAll("part", "workspace = ?", [$id]);
179
180
        $badges = R::findAll("workspacebadge", "workspace = ?", [$id]);
181
182
        $l_part = [];
183
        foreach ($part as $p) {
184
            array_push($l_part, intval($p['id']));
185
        }
186
        $l_badges = [];
187
        foreach ($badges as $b) {
188
            array_push($l_badges, intval($b['badge']));
189
        }
190
191
        $res = [
192
            'id'=> $workspace['id'],
193
            'title'=> $workspace['title'],
194
            'description'=> $workspace['description'],
195
            'environment'=> $workspace['environment'],
196
            'environment'=> $workspace['environment'],
197
            'completed'=> $workspace['completed'],
198
            'parts'=>$l_part,
199
            'badges'=>$l_badges
200
        ];
201
        $headers = [];
202
        return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300);
203
    }
204
205
    public function deleteWorkspace($id,Request $request)
206
    {
207
        $user_id = $this->getSessionId();
0 ignored issues
show
Unused Code introduced by
$user_id 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...
208
209
        $wp = R::load("workspace",$id);
0 ignored issues
show
Unused Code introduced by
$wp 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...
210
        R::trash($ws); //TODO add soft delete!!!! we can not manage this one in production
0 ignored issues
show
Bug introduced by
The variable $ws does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
211
212
        //TODO soft delete all the part!!
213
214
        $response = new Response();
215
        $response->headers->set('Content-Type', 'text/html');
216
        $response->setStatusCode(Response::HTTP_NO_CONTENT);
217
        $response->setSharedMaxAge(300);
218
    }
219
220
    public function share($id, Request $request) {
221
        $generatedKey = hash("sha256", (mt_rand(10000, 99999).time().$id));
222
        //TODO verificare documentazione realtiva sulla reale entropia generata da questo sistema
223
        $user_id = $this->getSessionId();
224
        $share = R::dispense("share");
225
            $share->user = $user_id;
226
            $share->workspace = $id;
227
            $share->key = $generatedKey;
228
            $share->inserttime = date('Y-m-d H:i:s');
229
        $share_id = R::store($share);
230
231
        $date = new \DateTime();
232
        date_add($date, date_interval_create_from_date_string('15 minutes'));
233
234
        $res = [
235
            "id"=>$share_id,
236
            "key"=>$generatedKey,
237
            "expire"=>$date->format('Y-m-d H:i:s')
238
        ];
239
240
        $headers = [];
241
        return JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300);
242
    }
243
244
    public function join(Request $request) {
245
246
247
        $headers = [];
248
        $response = JsonResponse::create(["message"=>"No key found"], 400, $headers)->setSharedMaxAge(300);
249
250
        //TODO verificare documentazione realtiva sulla reale entropia generata da questo sistema
251
        $user_id = $this->getSessionId();
252
        $data = json_decode($request->getContent(), true);
253
254
        $key = $data['key'];
255
256
        $share = R::findOne("share","key = ?",[$key]);
257
        echo $share->inserttime;
258
        if($share !== NULL){
259
            $date = new \DateTime();
260
            date_sub($date, date_interval_create_from_date_string('15 minutes'));
261
262
            $wp_id=$share['workspace'];
263
264
            $dateOld = new \DateTime($share->inserttime);
265
            if($dateOld > $date){
266
                $usw = R::dispense("userworkspace");
267
                    $usw->user = $user_id;
268
                    $usw->workspace = $wp_id;
269
                    $usw->inserttime = date('Y-m-d H:i:s');
270
                R::store($usw);
271
                $headers = [];
272
                $response = JsonResponse::create(["id"=>$wp_id], 200, $headers)->setSharedMaxAge(300);
273
274 View Code Duplication
            }else{
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
275
                $headers = [];
276
                $response = JsonResponse::create(["message"=>"Key no more valid"], 498, $headers)->setSharedMaxAge(300);
277
            }
278
        }
279
280
        return $response;
281
    }
282
283
    public function postPart($id, Request $request) {
284
        $user_id = $this->getSessionId();
285
286
        $data = json_decode($request->getContent(), true);
287
288
        $part = R::dispense("part");
289
            $part->workspace = $id;
290
            $part->user = $user_id;
291
            $part->inserttime = date('Y-m-d H:i:s');
292
            $part->lastupdatetime = date('Y-m-d H:i:s');
293
            $part->totalpoint = 0;
294
        $part_id = R::store($part);
295
296
        foreach($data['part'] as $r){ //TODO va fixato nelle api
297
            $resource = R::dispense("resource");
298
                $resource->part = $part_id;
299
                $resource->inserttime = date('Y-m-d H:i:s');
300
                $resource->updatetime = date('Y-m-d H:i:s');
301
                $resource->type = $r->type;
302
                $resource->ref = $r->ref;
303
                $resource->hash = $r->hash;
304
                $resource->totalpoint = 0;
305
            $resource_id = R::store($resource);
0 ignored issues
show
Unused Code introduced by
$resource_id 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...
306
        }
307
308 View Code Duplication
        foreach($data['badges'] as $badge_id){ //TODO va fixato nelle api
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
309
            $pb = R::dispense("partbadge");
310
                $pb->badge = $badge_id;
311
                $pb->part = $part_id;
312
                $pb->inserttime = date('Y-m-d H:i:s');
313
            $tmp = R::store($pb);
0 ignored issues
show
Unused Code introduced by
$tmp 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...
314
        }
315
316
        $res = ["id"=>$part_id];
317
        $headers = [];
318
        return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300);
319
    }
320
321
    public function getPart($id,$part_id, Request $request) {
322
        $user_id = $this->getSessionId();
0 ignored issues
show
Unused Code introduced by
$user_id 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...
323
324
        $data = json_decode($request->getContent(), true);
0 ignored issues
show
Unused Code introduced by
$data 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...
325
326
        $part = R::findOne("part","id = ?",[$part_id]);
327
328
        $resource = R::findAll("resource","part = ?",[$part_id]);
329
330
        $partecipants = R::findAll("cero","part = ?",[$part_id]);
331
332
        $badges = R::findAll("partbadge","part = ?",[$part_id]);
333
334
        $res= [
335
            "id"=>$part->id,
336
            "creation"=>$part->inserttime,
337
            "points"=>$part->points,
338
            "checked"=>$part->checked,
339
            "badges"=>[],
340
            "part"=>[],
341
            "partecipants"=>[]
342
        ];
343
344
        foreach($badges as $b){
345
            array_push($res['badges'],$b->id);
346
        }
347
        foreach($resource as $r){
348
            array_push($res['part'],[
349
                "type"=>$r->type,
350
                "hash"=>$r->hash,
351
                "ref"=>$r->ref
352
            ]);
353
        }
354
        foreach($partecipants as $p){
355
            array_push($res['partecipants'],$p->user);//TODO forse va usato l'id del c'ero e non l'id dell'utente
356
        }
357
358
        $headers = [];
359
        return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300);
360
    }
361
362
    private function getPositionInArray($array,$id){
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
363
        $count =0;
364
        foreach($array as $a){
365
            if($a->id === $id){
366
                return $count;
367
            }
368
            $count = $count + 1;
369
        }
370
        return -1;
371
    }
372
373
    public function putPart($id,$part_id, Request $request) {
374
        $user_id = $this->getSessionId();
375
376
        $data = json_decode($request->getContent(), true);
377
378
        $part = R::load("part",$part_id);
379
            $part->workspace = $id;
380
            $part->user = $user_id;
381
            $part->lastupdatetime = date('Y-m-d H:i:s');
382
            $part->totalpoint = 0;
383
        $part_id = R::store($part);
384
385
        $delete_res=R::findAll("resource","WHERE part = ?",[$part_id]);
386
387
        foreach($data['part'] as $r){ //TODO va fixato nelle api
388
            $resource = R::findOne("resource","WHERE hash =?",[$r->hash]);//TODO BISOGNA FARE IL DIFF TRA QUELLE PRESENTI E QUELLE NON PRESENTI
389
                $resource->part = $part_id;
390
                $resource->updatetime = date('Y-m-d H:i:s');
391
                $resource->type = $r->type;
392
                $resource->ref = $r->ref;
393
                $resource->hash = $r->hash;
394
                $resource->totalpoint = 0;
395
            $resource_id = R::store($resource);
396
            $rem_id=getPositionInArray($delete_res,$resource_id);
397
            if($rem_id != 0)
398
                array_splice($delete_res,$rem_id,1); //RIMUOVO GLI ELEMENTI CHE HO MODIFICATO
399
        }
400
401
        foreach($delete_res as $d){
402
            //RIMUOVO REALMENTE DAL DB LE COSE CHE HO LASCIATO FUORI DALLA PUT (PRESENTI NEL DB MA NON NELLA NUOVA VERSIONE ODIO LE PUT)
403
            $resource = R::load("resource",[$d->id]);
404
            $resource->deleted=true;
405
            R::store($resource);
406
        }
407
408
        $delete_badge=R::findAll("partbadge","WHERE part = ?",[$part_id]);
409
410
        foreach($data['badges'] as $badge_id){
411
            $pb = R::load("partbadge",$badge_id);
412
                $pb->badge = $badge_id;
413
                $pb->part = $part_id;
414
            $tmp = R::store($pb);
415
            $rem_id=getPositionInArray($delete_badge,$tmp);
416
            if($rem_id != 0)
417
                array_splice($delete_badge,$rem_id,1); //RIMUOVO GLI ELEMENTI CHE HO MODIFICATO
418
        }
419
420 View Code Duplication
        foreach($delete_badge as $d){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
421
            //RIMUOVO REALMENTE DAL DB LE COSE CHE HO LASCIATO FUORI DALLA PUT (PRESENTI NEL DB MA NON NELLA NUOVA VERSIONE ODIO LE PUT)
422
            $badge = R::load("partbadge",[$d->id]);//FORSE RILOADARLI NON È NECESSARIO
423
            $badge->deleted=true;
424
            R::store($badge);
425
        }
426
427
        $res = ["id"=>$part_id];
428
        $headers = [];
429
        return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300);
430
    }
431
432
    public function deletePart($id,$part_id, Request $request) {
433
        $user_id = $this->getSessionId();
0 ignored issues
show
Unused Code introduced by
$user_id 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...
434
        $part = R::load("part",$part_id);
435
        R::trash($part);
436
437
        //TODO soft delete all the part badge!!
438
439
        $delete_badge=R::findAll("partbadge","WHERE part = ?",[$part_id]);
440 View Code Duplication
        foreach($delete_badge as $d){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
441
            $badge = R::load("partbadge",[$d->id]);//FORSE RILOADARLI NON È NECESSARIO BASTA FARE $d->deleted=true; e store($d)
442
                $badge->deleted=true;
443
            R::store($badge);
444
        }
445
446
        $response = new Response();
447
        $response->headers->set('Content-Type', 'text/html');
448
        $response->setStatusCode(Response::HTTP_NO_CONTENT);
449
        $response->setSharedMaxAge(300);
450
    }
451
452
    private function getPoint($badge_id,$badges){
453
        foreach($badges as $b){
454
            if($b->id === $badge_id){
455
                if($b->completed === True){
456
                    echo "CASO 1;<BR />";
457
                    return $this->$POINT_FOR_USING_A_CONQUERED_BADGE;
0 ignored issues
show
Bug introduced by
The variable $POINT_FOR_USING_A_CONQUERED_BADGE does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
458
                }else{
459
                    echo "CASO 2;<BR />";
460
                    return $this->POINT_FOR_USING_A_BADGE;
461
                }
462
            }
463
        }
464
        echo "CASO 3;<BR />";
465
        return $this->POINT_DEFAULT;
466
    }
467
    public function checkin($id,$part_id, Request $request) {
468
        $user_id = $this->getSessionId();
469
470
        $badges = R::findAll("partbadge","part = ?",[$part_id]);
471
        $u_badges = R::findAll("userbadge","user = ?",[$user_id]);
472
473
        $point_earned = 0;
474
        foreach($badges as $b){ //SE CI SONO DEI BADGE
475
            $point = $this->getPoint($b->id,$u_badges);
476
            if($point != $this->POINT_DEFAULT){ //SE SEI IN CAMMINO PER QUEI BADGE O SE LI POSSIEDI GIÀ
477
                echo "PUNTI:".$point;
478
                $point_earned = $point_earned + $point;
479
                $pb = R::dispense("cero");
480
                    $pb->user = $user_id;
481
                    $pb->part = $part_id;
482
                    $pb->badge = $b->id;
483
                    $pb->inserttime = date('Y-m-d H:i:s');
484
                    $pb->points = $point;
485
                $tmp = R::store($pb);
0 ignored issues
show
Unused Code introduced by
$tmp 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...
486
487 View Code Duplication
                if($point === $this->POINT_FOR_USING_A_BADGE){ //SE SEI IN CAMMINO MA NON LI HAI ANCORA RAGGIUNTI
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
488
                    $ubc = R::dispense("userbadgeclove");
489
                        $ubc->user = $user_id;
490
                        $ubc->badge = $b->id;
491
                        $ubc->part = $part_id;
492
                        $ubc->inserttime = date('Y-m-d H:i:s');
493
                    $tmp = R::store($ubc);
0 ignored issues
show
Unused Code introduced by
$tmp 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...
494
                }
495
            }
496
        }
497
498 View Code Duplication
        if($point_earned <= 0){ //SE NON CI SONO BADGE O SE TU NON SEI IN CAMMINO PER NESSUNO DI LORO
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
499
            $pb = R::dispense("cero");
500
                $pb->user = $user_id;
501
                $pb->part = $part_id;
502
                $pb->inserttime = date('Y-m-d H:i:s');
503
                $pb->points = $this->POINT_DEFAULT;
504
            $tmp = R::store($pb);
0 ignored issues
show
Unused Code introduced by
$tmp 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...
505
        }
506
        $res = ["points"=>$point_earned];
507
        $headers = [];
508
        return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300);
509
510
    }
511
512
    public function deleteCheckin($id,$part_id, Request $request) {
513
        $user_id = $this->getSessionId();
514
515
        $u_badges = R::findAll("userbadge","user = ? AND part = ?",[$user_id,$part_id]);
516
        R::trashAll($u_badges);
517
518
        $cero = R::findAll("cero","user = ? AND part = ?",[$user_id,$part_id]);
519
        R::trashAll($cero);
520
521
        $response = new Response();
522
        $response->headers->set('Content-Type', 'text/html');
523
        $response->setStatusCode(Response::HTTP_NO_CONTENT);
524
        $response->setSharedMaxAge(300);
525
        return $response;
526
527
    }
528
}
529