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 ( e58147...7621e2 )
by
unknown
02:21
created

WorkspaceController   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 295
Duplicated Lines 5.08 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 25
c 4
b 2
f 0
lcom 1
cbo 0
dl 15
loc 295
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 16 1
A getSessionId() 0 4 1
B getWorkspaceList() 0 27 2
A createWorkspace() 8 62 2
B getWorkspace() 0 31 3
A share() 0 23 1
B postPart() 7 39 4
B getPart() 0 40 4
A getPoint() 0 11 4
B checkin() 0 28 3

How to fix   Duplicated Code   

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:

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->get('/{id}/share', array($this, 'share'));
31
        $factory->post('/{id}/part', array($this, 'postPart'));
32
        $factory->get('/{id}/part/$id_part', array($this, 'getPart'));
33
        $factory->patch('/{id}/part/$id_part/checkin', array($this, 'checkin'));
34
        return $factory;
35
    }
36
    public function getSessionId() {
37
        $user_id = $this->app['session']->get('user')['id'];
38
        return $user_id;
39
    }
40
    public function getWorkspaceList(Request $request)
41
    {
42
        $user_id = $this->getSessionId();
43
        $workspaces = R::getAll("SELECT ws.id,
44
                                          ws.title,
45
                                          ws.description,
46
                                          ws.environment,
47
                                          ws.completed
48
                                          FROM userworkspace AS uws
49
                                          LEFT JOIN workspace AS ws
50
                                          ON uws.workspace = ws.id
51
                                          WHERE uws.user = ?",[$user_id]);
52
        $list = [];
53
        foreach ($workspaces as $ws) {
54
            array_push($list, [
55
                "id"=>$ws['id'],
56
                "title"=>$ws['title'],
57
                "description"=>$ws['description'],
58
                "environment"=>$ws['environment'],
59
                "point"=>0, //TODO fare una view con i point già calcolati per il ws
60
                "completed"=>$ws['completed'],
61
            ]);
62
        }
63
        $headers = [];
64
        return JsonResponse::create($list, 200, $headers)->setSharedMaxAge(300);
65
66
    }
67
    public function createWorkspace(Request $request)
68
    {
69
        $user_id = $this->getSessionId();
70
        $counter = 0;
0 ignored issues
show
Unused Code introduced by
$counter 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...
71
        $data = json_decode($request->getContent(), true);
72
        //TODO validate json_decode
73
        $title = $data['title'];
74
        $description = $data['description'];
75
        $environment = $data['environment'];
76
77
        $patrol = $data['team']['patrol'];
78
        $unit = $data['team']['unit'];
79
        $group = $data['team']['group'];
80
81
        //save the workspace get id
82
        $ws = R::dispense("workspace");
83
            $ws->title = $title;
84
            $ws->description = $description;
85
            $ws->environment = $environment;
86
            $ws->completed = false;
87
            $ws->inserttime = date('Y-m-d H:i:s');
88
            $ws->lastupdatetime = date('Y-m-d H:i:s');
89
        $id = R::store($ws);
90
91
        //save the team
92
        $team = R::dispense("team");
93
            $team->workspace = $id;
94
            $team->patrol = $patrol;
95
            $team->unit = $unit;
96
            $team->group = $group;
97
        $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...
98
99
        //create a phantom part to add badge
100
        $part = R::dispense("part");
101
            $part->workspace = $id;
102
            $part->user = $user_id;
103
            $part->inserttime = date('Y-m-d H:i:s');
104
            $part->lastupdatetime = date('Y-m-d H:i:s');
105
            $part->totalpoint = 0;
106
        $part_id = R::store($part);
107
108
        //add the badge to the project
109 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...
110
            //TODO insert those badge as first hidden post
111
            $pb = R::dispense("partbadge");
112
                $pb->badge = $badge_id;
113
                $pb->part = $part_id;
114
                $pb->inserttime = date('Y-m-d H:i:s');
115
            $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...
116
        }
117
118
        //add the workspace created to the user as owner
119
        $usw = R::dispense("userworkspace");
120
            $usw->user = $user_id;
121
            $usw->workspace = $id;
122
            $usw->inserttime = date('Y-m-d H:i:s');
123
        R::store($usw);
124
125
        $res = ["id" => $id];
126
        $headers = [];
127
        return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300);
128
    }
129
130
    public function getWorkspace($id, Request $request) {
131
        $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...
132
        //TODO controllare che l'utente abbia diritto a vedere questo workspace
133
134
        $workspace = R::findOne("workspace", "id = ?", [$id]);
135
        $part = R::findAll("part", "workspace = ?", [$id]);
136
137
        $badges = R::findAll("workspacebadge", "workspace = ?", [$id]);
138
139
        $l_part = [];
140
        foreach ($part as $p) {
141
            array_push($l_part, intval($p['id']));
142
        }
143
        $l_badges = [];
144
        foreach ($badges as $b) {
145
            array_push($l_badges, intval($b['badge']));
146
        }
147
148
        $res = [
149
            'id'=> $workspace['id'],
150
            'title'=> $workspace['title'],
151
            'description'=> $workspace['description'],
152
            'environment'=> $workspace['environment'],
153
            'environment'=> $workspace['environment'],
154
            'completed'=> $workspace['completed'],
155
            'parts'=>$l_part,
156
            'badges'=>$l_badges
157
        ];
158
        $headers = [];
159
        return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300);
160
    }
161
162
    public function share($id, Request $request) {
163
        $generatedKey = hash("sha256", (mt_rand(10000, 99999).time().$id));
164
        //TODO verificare documentazione realtiva sulla reale entropia generata da questo sistema
165
        $user_id = $this->getSessionId();
166
        $share = R::dispense("share");
167
            $share->user = $user_id;
168
            $share->workspace = $id;
169
            $share->key = $generatedKey;
170
            $share->inserttime = date('Y-m-d H:i:s');
171
        $share_id = R::store($share);
172
173
        $date = new \DateTime();
174
        date_add($date, date_interval_create_from_date_string('15 minutes'));
175
176
        $res = [
177
            "id"=>$share_id,
178
            "key"=>$generatedKey,
179
            "expire"=>$date->format('Y-m-d H:i:s')
180
        ];
181
182
        $headers = [];
183
        return JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300);
184
    }
185
186
    public function postPart($id, Request $request) {
187
        $user_id = $this->getSessionId();
188
189
        $data = json_decode($request->getContent(), true);
190
191
        $part = R::dispense("part");
192
            $part->workspace = $id;
193
            $part->user = $user_id;
194
            $part->inserttime = date('Y-m-d H:i:s');
195
            $part->lastupdatetime = date('Y-m-d H:i:s');
196
            $part->totalpoint = 0;
197
        $part_id = R::store($part);
198
199
        foreach($data['part'] as $r){ //TODO va fixato nelle api
200
            if($r->type != "badge"){
201
                $resource = R::dispense("resource");
202
                    $resource->part = $part_id;
203
                    $resource->inserttime = date('Y-m-d H:i:s');
204
                    $resource->updatetime = date('Y-m-d H:i:s');
205
                    $resource->type = $r->type;
206
                    $resource->ref = $r->ref;
207
                    $resource->hash = $r->hash;
208
                    $resource->totalpoint = 0;
209
                $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...
210
            }
211
        }
212
213 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...
214
            $pb = R::dispense("partbadge");
215
                $pb->badge = $badge_id;
216
                $pb->part = $part_id;
217
                $pb->inserttime = date('Y-m-d H:i:s');
218
            $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...
219
        }
220
221
        $res = ["id"=>$part_id];
222
        $headers = [];
223
        return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300);
224
    }
225
226
    public function getPart($id,$part_id,$request) {
227
        $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...
228
229
        $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...
230
231
        $part = R::finOne("part","id = ?",[$part_id]);
232
233
        $resource = R::findAll("resource","part = ?",[$part_id]);
234
235
        $partecipants = R::findAll("cero","part = ?",[$part_id]);
236
237
        $badges = R::findAll("partbadge","part = ?",[$part_id]);
238
239
        $res= [
240
            "id"=>$part->id,
241
            "creation"=>$part->inserttime,
242
            "points"=>$part->points,
243
            "checked"=>$part->checked,
244
            "badges"=>[],
245
            "part"=>[],
246
            "partecipants"=>[]
247
        ];
248
249
        foreach($badges as $b){
250
            array_push($res['badges'],$b->id);
251
        }
252
        foreach($resource as $r){
253
            array_push($res['part'],[
254
                "type"=>$r->type,
255
                "hash"=>$r->hash,
256
                "ref"=>$r->ref
257
            ]);
258
        }
259
        foreach($partecipants as $p){
260
            array_push($res['partecipants'],$p->user);//TODO forse va usato l'id del c'ero e non l'id dell'utente
261
        }
262
263
        $headers = [];
264
        return JsonResponse::create($res, 201, $headers)->setSharedMaxAge(300);
265
    }
266
    private function getPoint($badge_id,$badges){
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
267
        foreach($badges as $b){
268
            if($b->id === $badge_id){
269
                if($b->completed === True)
270
                    return POINT_FOR_USING_A_CONQUERED_BADGE;
271
                else
272
                    return POINT_FOR_USING_A_BADGE;
273
            }
274
        }
275
        return POINT_DEFAULT;
276
    }
277
    public function checkin($id,$part_id,$request) {
278
        $user_id = $this->getSessionId();
279
280
        $badges = R::findAll("partbadge","part = ?",[$part_id]);
281
        $u_badges = R::findAll("userbadge","user = ?",[$user_id]);
282
283
        foreach($badges as $b){
284
            $point = getPoint($b->id,$u_badges);
285
            $pb = R::dispense("cero");
286
                $pb->user = $user_id;
287
                $pb->part = $part_id;
288
                $pb->badge = $b->id;
289
                $pb->inserttime = date('Y-m-d H:i:s');
290
                $bp->points = $point;
0 ignored issues
show
Bug introduced by
The variable $bp 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...
291
            $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...
292
293
            if($point === POINT_FOR_USING_A_BADGE){
294
                $ubc = R::dispense("userbadgeclove");
295
                    $ubc->user = $user_id;
296
                    $ubc->badge = $b->id;
297
                    $ubc->part = $part_id;
298
                    $ubc->inserttime = date('Y-m-d H:i:s');
299
                $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...
300
            }
301
        }
302
303
304
    }
305
}
306