Completed
Push — FVSv2 ( 78f5f9...0e95d7 )
by Patrick
01:35
created

DepartmentAPI::createRoleForDepartment()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 3
dl 17
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
5
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
6
7
class DepartmentAPI extends Http\Rest\DataTableAPI
8
{
9
    use Processor;
10
11
    protected $isAdmin = null;
12
    protected $isLead = null;
13
14
    public function __construct()
15
    {
16
        parent::__construct('fvs', 'departments', 'departmentID');
0 ignored issues
show
Documentation introduced by
'departmentID' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
17
    }
18
19
    public function setup($app)
20
    {
21
        parent::setup($app);
22
        $app->get('/{dept}/roles[/]', array($this, 'getRolesForDepartment'));
23
        $app->post('/{dept}/roles[/]', array($this, 'createRoleForDepartment'));
24
        $app->patch('/{dept}/roles/{roleName}[/]', array($this, 'updateRoleForDepartment'));
25
        $app->get('/{dept}/shifts[/]', array($this, 'getShiftsForDepartment'));
26
        $app->post('/{dept}/shifts[/]', array($this, 'createShiftForDepartment'));
27
        $app->get('/{dept}/shifts/Actions/GenerateShiftSchedule', array($this, 'generateShiftSchedule'));
28
    }
29
30
    protected function isVolunteerAdmin($request)
31
    {
32
        $this->validateLoggedIn($request);
33
        if($this->isAdmin === null)
34
        {
35
            $this->isAdmin = $this->user->isInGroupNamed('VolunteerAdmins');
1 ignored issue
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
        }
37
        return $this->isAdmin;
38
    }
39
40
    protected function canCreate($request)
41
    {
42
        return $this->isVolunteerAdmin($request);
43
    }
44
45
    protected function canEditDept($request, $deptId, $dept = null)
46
    {
47
        if($this->isVolunteerAdmin($request))
48
        {
49
            return true;
50
        }
51
        if($dept !== null)
52
        {
53
            return $this->isUserDepartmentLead2($dept, $this->user);
54
        }
55
        return $this->isUserDepartmentLead($deptId, $this->user);
56
    }
57
58
    protected function canUpdate($request, $entity)
59
    {
60
        return $this->canEditDept($request, false);
61
    }
62
63
    protected function canDelete($request, $entity)
64
    {
65
        if($this->isVolunteerAdmin($request))
66
        {
67
            return true;
68
        }
69
        return false;
70
    }
71
72
    protected function processEntry($entry, $request)
73
    {
74
        $entry['available'] = true;
75
        $entry['isAdmin'] = $this->canEditDept($request, null, $entry);
76
        if(isset($entry['public']) && $entry['public'] === false)
77
        {
78
            if(!$this->isUserDepartmentLead2($entry, $this->user))
79
            {
80
                $entry['available'] = false;
81
                $entry['why'] = 'Not lead of department';
82
            }
83
            if(!$entry['available'] && !$entry['isAdmin'])
84
            {
85
                return null;
86
            }
87
        }
88
        return $entry;
89
    }
90
91
    public function getRolesForDepartment($request, $response, $args)
92
    {
93
        $deptId = $args['dept'];
94
        if($this->canEditDept($request, $deptId) === false)
95
        {
96
            return $response->withStatus(401);
97
        }
98
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles');
99
        $filter = new \Data\Filter("departmentID eq '$deptId'");
100
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
101
        $roles = $dataTable->read($filter, $odata->select, $odata->top,
102
                                  $odata->skip, $odata->orderby);
103
        if($roles === false)
104
        {
105
            $roles = array();
106
        }
107
        $count = count($roles);
108
        for($i = 0; $i < $count; $i++)
109
        {
110
            $roles[$i] = $this->processRole($roles[$i], $request);
111
        }
112
        return $response->withJson($roles);
113
    }
114
115
    public function getShiftsForDepartment($request, $response, $args)
116
    {
117
        $deptId = $args['dept'];
118
        if($this->canEditDept($request, $deptId) === false)
119
        {
120
            return $response->withStatus(401);
121
        }
122
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
123
        $filter = new \Data\Filter("departmentID eq '$deptId'");
124
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
125
        $shifts = $dataTable->read($filter, $odata->select, $odata->top,
126
                                  $odata->skip, $odata->orderby);
127
        if($shifts === false)
128
        {
129
            $shifts = array();
130
        }
131
        return $response->withJson($shifts);
132
    }
133
134 View Code Duplication
    public function createRoleForDepartment($request, $response, $args)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
135
    {
136
        $deptId = $args['dept'];
137
        if($this->canEditDept($request, $deptId) === false)
138
        {
139
            return $response->withStatus(401);
140
        }
141
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles');
142
        $obj = $request->getParsedBody();
143
        if($obj == NULL)
144
        {
145
            $obj = json_decode($request->getBody()->getContents(), true);
146
        }
147
        $obj['departmentID'] = $deptId;
148
        $ret = $dataTable->create($obj);
149
        return $response->withJson($ret);
150
    }
151
152 View Code Duplication
    public function createShiftForDepartment($request, $response, $args)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
153
    {
154
        $deptId = $args['dept'];
155
        if($this->canEditDept($request, $deptId) === false)
156
        {
157
            return $response->withStatus(401);
158
        }
159
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
160
        $obj = $request->getParsedBody();
161
        if($obj == NULL)
162
        {
163
            $obj = json_decode($request->getBody()->getContents(), true);
164
        }
165
        $obj['departmentID'] = $deptId;
166
        $ret = $dataTable->create($obj);
167
        return $response->withJson($ret);
168
    }
169
170
    public function updateRoleForDepartment($request, $response, $args)
171
    {
172
        $deptId = $args['dept'];
173
        $roleId = $args['roleName'];
174
        if($this->canEditDept($request, $deptId) === false)
175
        {
176
            return $response->withStatus(401);
177
        }
178
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles');
179
        $filter = new \Data\Filter("departmentID eq '$deptId' and short_name eq '$roleId'");
180
        $entry = $dataTable->read($filter);
181
        if(empty($entry))
182
        {
183
            return $response->withStatus(404);
184
        }
185
        if(count($entry) === 1 && isset($entry[0]))
186
        {
187
            $entry = $entry[0];
0 ignored issues
show
Unused Code introduced by
$entry 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...
188
        }
189
        $obj = $request->getParsedBody();
190
        if($obj === null)
191
        {
192
            $request->getBody()->rewind();
193
            $obj = $request->getBody()->getContents();
194
            $tmp = json_decode($obj, true);
195
            if($tmp !== null)
196
            {
197
                $obj = $tmp;
198
            }
199
        }
200
        $ret = $dataTable->update($filter, $obj);
201
        return $response->withJson($ret);
202
    }
203
204
    public function generateShiftSchedule($request, $response, $args)
205
    {
206
        $deptId = $args['dept'];
207
        if($this->canEditDept($request, $deptId) === false)
208
        {
209
            return $response->withStatus(401);
210
        }
211
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'departments');
212
        $depts = $dataTable->read(new \Data\Filter('departmentID eq '.$deptId));
213
        if(empty($depts))
214
        {
215
            return $response->withStatus(404);
216
        }
217
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
218
        $eventId = $request->getParam('eventID');
219
        $filter = new \Data\Filter('eventID eq '.$eventId.' and departmentID eq '.$deptId);
220
        $shifts = $dataTable->read($filter);
221
        if(empty($shifts))
222
        {
223
            return $response->withStatus(404);
224
        }
225
        switch($request->getParam('type'))
226
        {
227
            case 'simplePDF':
228
               return $this->generateSimplePDFSchedule($depts[0], $shifts, $response);
229
            case 'gridXLSX':
230
               return $this->generateGridSchedule($depts[0], $shifts, $response, 'XLSX');
231
            case 'gridPDF':
232
               return $this->generateGridSchedule($depts[0], $shifts, $response, 'PDF');
233
        }
234
        return $response->withJson($shifts);
235
    }
236
237
    public function generateSimplePDFSchedule($dept, $shifts, $response)
238
    {
239
        $pdf = new \Schedules\SimplePDF($dept, $shifts);
240
        $response = $response->withHeader('Content-Type', 'application/pdf');
241
        $response->getBody()->write($pdf->toPDFBuffer());
242
        return $response;
243
    }
244
245
    public function generateGridSchedule($dept, $shifts, $response, $type)
246
    {
247
        $ss = new \Schedules\GridSchedule($dept, $shifts);
248
        $data = $ss->getBuffer($type);
249
        $response = $response->withHeader('Content-Type', $data['content-type']);
250
        $response = $response->withHeader('Content-Disposition', 'attachment; filename='.$dept['departmentName'].$data['extension']);
251
        $response->getBody()->write($data['buffer']);
252
        return $response;
253
    }
254
}
255
256