Completed
Push — FVSv2 ( 1ec6b9...63e0fa )
by Patrick
01:36
created

DepartmentAPI::canCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
class DepartmentAPI extends VolunteerAPI
4
{
5
    use Processor;
6
7
    public function __construct()
8
    {
9
        parent::__construct('departments', 'departmentID');
10
    }
11
12
    public function setup($app)
13
    {
14
        parent::setup($app);
15
        $app->get('/{dept}/roles[/]', array($this, 'getRolesForDepartment'));
16
        $app->post('/{dept}/roles[/]', array($this, 'createRoleForDepartment'));
17
        $app->patch('/{dept}/roles/{roleName}[/]', array($this, 'updateRoleForDepartment'));
18
        $app->get('/{dept}/shifts[/]', array($this, 'getShiftsForDepartment'));
19
        $app->post('/{dept}/shifts[/]', array($this, 'createShiftForDepartment'));
20
        $app->get('/{dept}/shifts/Actions/GenerateShiftSchedule', array($this, 'generateShiftSchedule'));
21
    }
22
23
    protected function canEditDept($request, $deptId, $dept = null)
24
    {
25
        if($this->isVolunteerAdmin($request))
26
        {
27
            return true;
28
        }
29
        if($dept !== null)
30
        {
31
            return $this->isUserDepartmentLead2($dept, $this->user);
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...
32
        }
33
        return $this->isUserDepartmentLead($deptId, $this->user);
34
    }
35
36
    protected function canUpdate($request, $entity)
37
    {
38
        return $this->canEditDept($request, false);
39
    }
40
41
    protected function canDelete($request, $entity)
42
    {
43
        if($this->isVolunteerAdmin($request))
44
        {
45
            return true;
46
        }
47
        return false;
48
    }
49
50
    protected function processEntry($entry, $request)
51
    {
52
        $entry['available'] = true;
53
        $entry['isAdmin'] = $this->canEditDept($request, null, $entry);
54
        if(isset($entry['public']) && $entry['public'] === false)
55
        {
56
            if(!$this->isUserDepartmentLead2($entry, $this->user))
57
            {
58
                $entry['available'] = false;
59
                $entry['why'] = 'Not lead of department';
60
            }
61
            if(!$entry['available'] && !$entry['isAdmin'])
62
            {
63
                return null;
64
            }
65
        }
66
        return $entry;
67
    }
68
69
    public function getRolesForDepartment($request, $response, $args)
70
    {
71
        $deptId = $args['dept'];
72
        if($this->canEditDept($request, $deptId) === false)
73
        {
74
            return $response->withStatus(401);
75
        }
76
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles');
77
        $filter = new \Data\Filter("departmentID eq '$deptId'");
78
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
79
        $roles = $dataTable->read($filter, $odata->select, $odata->top,
80
                                  $odata->skip, $odata->orderby);
81
        if($roles === false)
82
        {
83
            $roles = array();
84
        }
85
        $count = count($roles);
86
        for($i = 0; $i < $count; $i++)
87
        {
88
            $roles[$i] = $this->processRole($roles[$i], $request);
89
        }
90
        return $response->withJson($roles);
91
    }
92
93
    public function getShiftsForDepartment($request, $response, $args)
94
    {
95
        $deptId = $args['dept'];
96
        if($this->canEditDept($request, $deptId) === false)
97
        {
98
            return $response->withStatus(401);
99
        }
100
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
101
        $filter = new \Data\Filter("departmentID eq '$deptId'");
102
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
103
        $shifts = $dataTable->read($filter, $odata->select, $odata->top,
104
                                  $odata->skip, $odata->orderby);
105
        if($shifts === false)
106
        {
107
            $shifts = array();
108
        }
109
        return $response->withJson($shifts);
110
    }
111
112 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...
113
    {
114
        $deptId = $args['dept'];
115
        if($this->canEditDept($request, $deptId) === false)
116
        {
117
            return $response->withStatus(401);
118
        }
119
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles');
120
        $obj = $request->getParsedBody();
121
        if($obj == NULL)
122
        {
123
            $obj = json_decode($request->getBody()->getContents(), true);
124
        }
125
        $obj['departmentID'] = $deptId;
126
        $ret = $dataTable->create($obj);
127
        return $response->withJson($ret);
128
    }
129
130 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...
131
    {
132
        $deptId = $args['dept'];
133
        if($this->canEditDept($request, $deptId) === false)
134
        {
135
            return $response->withStatus(401);
136
        }
137
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
138
        $obj = $request->getParsedBody();
139
        if($obj == NULL)
140
        {
141
            $obj = json_decode($request->getBody()->getContents(), true);
142
        }
143
        $obj['departmentID'] = $deptId;
144
        $ret = $dataTable->create($obj);
145
        return $response->withJson($ret);
146
    }
147
148
    public function updateRoleForDepartment($request, $response, $args)
149
    {
150
        $deptId = $args['dept'];
151
        $roleId = $args['roleName'];
152
        if($this->canEditDept($request, $deptId) === false)
153
        {
154
            return $response->withStatus(401);
155
        }
156
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles');
157
        $filter = new \Data\Filter("departmentID eq '$deptId' and short_name eq '$roleId'");
158
        $entry = $dataTable->read($filter);
159
        if(empty($entry))
160
        {
161
            return $response->withStatus(404);
162
        }
163
        if(count($entry) === 1 && isset($entry[0]))
164
        {
165
            $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...
166
        }
167
        $obj = $request->getParsedBody();
168
        if($obj === null)
169
        {
170
            $request->getBody()->rewind();
171
            $obj = $request->getBody()->getContents();
172
            $tmp = json_decode($obj, true);
173
            if($tmp !== null)
174
            {
175
                $obj = $tmp;
176
            }
177
        }
178
        $ret = $dataTable->update($filter, $obj);
179
        return $response->withJson($ret);
180
    }
181
182
    public function generateShiftSchedule($request, $response, $args)
183
    {
184
        $deptId = $args['dept'];
185
        if($this->canEditDept($request, $deptId) === false)
186
        {
187
            return $response->withStatus(401);
188
        }
189
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'departments');
190
        $depts = $dataTable->read(new \Data\Filter('departmentID eq '.$deptId));
191
        if(empty($depts))
192
        {
193
            return $response->withStatus(404);
194
        }
195
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
196
        $eventId = $request->getParam('eventID');
197
        $filter = new \Data\Filter('eventID eq '.$eventId.' and departmentID eq '.$deptId);
198
        $shifts = $dataTable->read($filter);
199
        if(empty($shifts))
200
        {
201
            return $response->withStatus(404);
202
        }
203
        switch($request->getParam('type'))
204
        {
205
            case 'simplePDF':
206
               return $this->generateSimplePDFSchedule($depts[0], $shifts, $response);
207
            case 'gridXLSX':
208
               return $this->generateGridSchedule($depts[0], $shifts, $response, 'XLSX');
209
            case 'gridPDF':
210
               return $this->generateGridSchedule($depts[0], $shifts, $response, 'PDF');
211
        }
212
        return $response->withJson($shifts);
213
    }
214
215
    public function generateSimplePDFSchedule($dept, $shifts, $response)
216
    {
217
        $pdf = new \Schedules\SimplePDF($dept, $shifts);
218
        $response = $response->withHeader('Content-Type', 'application/pdf');
219
        $response->getBody()->write($pdf->toPDFBuffer());
220
        return $response;
221
    }
222
223
    public function generateGridSchedule($dept, $shifts, $response, $type)
224
    {
225
        $ss = new \Schedules\GridSchedule($dept, $shifts);
226
        $data = $ss->getBuffer($type);
227
        $response = $response->withHeader('Content-Type', $data['content-type']);
228
        $response = $response->withHeader('Content-Disposition', 'attachment; filename='.$dept['departmentName'].$data['extension']);
229
        $response->getBody()->write($data['buffer']);
230
        return $response;
231
    }
232
}
233
234