Completed
Push — FVSv2 ( abc2fa...7fa7a8 )
by Patrick
01:32
created

DepartmentAPI::generateShiftSchedule()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 7
nop 3
dl 0
loc 32
rs 8.4746
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
    protected function createEntityForDepartment($request, $response, $args, $table)
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', $table);
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
    public function createRoleForDepartment($request, $response, $args)
131
    {
132
        return $this->createEntityForDepartment($request, $response, $args, 'roles');
133
    }
134
135
    public function createShiftForDepartment($request, $response, $args)
136
    {
137
        return $this->createEntityForDepartment($request, $response, $args, 'shifts');
138
    }
139
140
    public function updateRoleForDepartment($request, $response, $args)
141
    {
142
        $deptId = $args['dept'];
143
        $roleId = $args['roleName'];
144
        if($this->canEditDept($request, $deptId) === false)
145
        {
146
            return $response->withStatus(401);
147
        }
148
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'roles');
149
        $filter = new \Data\Filter("departmentID eq '$deptId' and short_name eq '$roleId'");
150
        $entry = $dataTable->read($filter);
151
        if(empty($entry))
152
        {
153
            return $response->withStatus(404);
154
        }
155
        if(count($entry) === 1 && isset($entry[0]))
156
        {
157
            $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...
158
        }
159
        $obj = $request->getParsedBody();
160
        if($obj === null)
161
        {
162
            $request->getBody()->rewind();
163
            $obj = $request->getBody()->getContents();
164
            $tmp = json_decode($obj, true);
165
            if($tmp !== null)
166
            {
167
                $obj = $tmp;
168
            }
169
        }
170
        $ret = $dataTable->update($filter, $obj);
171
        return $response->withJson($ret);
172
    }
173
174
    public function generateShiftSchedule($request, $response, $args)
175
    {
176
        $deptId = $args['dept'];
177
        if($this->canEditDept($request, $deptId) === false)
178
        {
179
            return $response->withStatus(401);
180
        }
181
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'departments');
182
        $depts = $dataTable->read(new \Data\Filter('departmentID eq '.$deptId));
183
        if(empty($depts))
184
        {
185
            return $response->withStatus(404);
186
        }
187
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
188
        $eventId = $request->getParam('eventID');
189
        $filter = new \Data\Filter('eventID eq '.$eventId.' and departmentID eq '.$deptId);
190
        $shifts = $dataTable->read($filter);
191
        if(empty($shifts))
192
        {
193
            return $response->withStatus(404);
194
        }
195
        switch($request->getParam('type'))
196
        {
197
            case 'simplePDF':
198
               return $this->generateSimplePDFSchedule($depts[0], $shifts, $response);
199
            case 'gridXLSX':
200
               return $this->generateGridSchedule($depts[0], $shifts, $response, 'XLSX');
201
            case 'gridPDF':
202
               return $this->generateGridSchedule($depts[0], $shifts, $response, 'PDF');
203
        }
204
        return $response->withJson($shifts);
205
    }
206
207
    public function generateSimplePDFSchedule($dept, $shifts, $response)
208
    {
209
        $pdf = new \Schedules\SimplePDF($dept, $shifts);
210
        $response = $response->withHeader('Content-Type', 'application/pdf');
211
        $response->getBody()->write($pdf->toPDFBuffer());
212
        return $response;
213
    }
214
215
    public function generateGridSchedule($dept, $shifts, $response, $type)
216
    {
217
        $ss = new \Schedules\GridSchedule($dept, $shifts);
218
        $data = $ss->getBuffer($type);
219
        $response = $response->withHeader('Content-Type', $data['content-type']);
220
        $response = $response->withHeader('Content-Disposition', 'attachment; filename='.$dept['departmentName'].$data['extension']);
221
        $response->getBody()->write($data['buffer']);
222
        return $response;
223
    }
224
}
225
226