Passed
Push — master ( d7e703...d0dd1b )
by Patrick
09:55
created

EventAPI::getEEShiftReportForEvent()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 23
c 0
b 0
f 0
nc 9
nop 3
dl 0
loc 35
rs 9.2408
1
<?php
2
class EventAPI extends VolunteerAPI
3
{
4
    use Processor;
0 ignored issues
show
introduced by
The trait Processor requires some properties which are not provided by EventAPI: $startTime, $certs, $endTime, $mail, $uid, $title
Loading history...
5
6
    public function __construct()
7
    {
8
        parent::__construct('events');
9
    }
10
11
    public function setup($app)
12
    {
13
        parent::setup($app);
14
        $app->get('/{event}/shifts[/]', array($this, 'getShiftsForEvent'));
15
        $app->post('/{event}/shifts[/]', array($this, 'createShiftForEvent'));
16
        $app->get('/{event}/Actions/GetEEShiftReport', array($this, 'getEEShiftReportForEvent'));
17
        $app->post('/{event}/Actions/GetEEShiftReport', array($this, 'getEEShiftReportForEvent'));
18
    }
19
20
    protected function getFilterForPrimaryKey($value)
21
    {
22
        return new \Data\Filter($this->primaryKeyName." eq '$value' or alias eq '$value'");
23
    }
24
25
    protected function canUpdate($request, $entity)
26
    {
27
        if($this->isVolunteerAdmin($request))
28
        {
29
            return true;
30
        }       
31
        return false;
32
    }
33
34
    protected function canDelete($request, $entity)
35
    {
36
        if($this->isVolunteerAdmin($request))
37
        {
38
            return true;
39
        }
40
        return false;
41
    }
42
43
    public function processEntry($entry, $request)
44
    {
45
        $entry['available'] = true;
46
        $endTime = new DateTime($entry['endTime']);
47
        $now = new DateTime();
48
        if($endTime < $now)
49
        {
50
            $entry['available'] = false;
51
            $entry['why'] = 'Event is in the past';
52
        }
53
        if($entry['private'] && !in_array($this->user->mail, $entry['volList']))
54
        {
55
            $entry['available'] = false;
56
            $entry['why'] = 'Event is private and you are not invited';
57
        }
58
        if(!$entry['available'] && !$this->isVolunteerAdmin($request) && !$this->userIsLeadCached($this->user))
59
        {
60
            return null;
61
        }
62
        if(!$this->isVolunteerAdmin($request) && !$this->userIsLeadCached($this->user) && isset($entry['eeLists']))
63
        {
64
            unset($entry['eeLists']);
65
        }
66
        return $entry;
67
    }
68
69
    public function getShiftsForEvent($request, $response, $args)
70
    {
71
        $this->validateLoggedIn($request);
72
        $eventId = $args['event'];
73
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
74
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
75
        $filter = $this->addRequiredFilter('eventID', $eventId, $odata);
76
        if($filter === false)
77
        {
78
            return $response->withStatus(409);
79
        }
80
        $shifts = $dataTable->read($filter, $odata->select, $odata->top,
81
                                    $odata->skip, $odata->orderby);
82
        if($shifts === false)
83
        {
84
            $shifts = array();
85
        }
86
        $count = count($shifts);
0 ignored issues
show
Bug introduced by
It seems like $shifts can also be of type true; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        $count = count(/** @scrutinizer ignore-type */ $shifts);
Loading history...
87
        for($i = 0; $i < $count; $i++)
88
        {
89
            $shifts[$i] = $this->processShift($shifts[$i], $request);
90
        }
91
        $shifts = array_values(array_filter($shifts));
0 ignored issues
show
Bug introduced by
It seems like $shifts can also be of type true; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
        $shifts = array_values(array_filter(/** @scrutinizer ignore-type */ $shifts));
Loading history...
92
        return $response->withJson($shifts);
93
    }
94
95
    public function createShiftForEvent($request, $response, $args)
96
    {
97
        $eventId = $args['event'];
98
        if($this->canUpdate($request, null) === false)
99
        {
100
            return $response->withStatus(401);
101
        }
102
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
103
        $obj = $request->getParsedBody();
104
        if($obj == NULL)
105
        {
106
            $obj = json_decode($request->getBody()->getContents(), true);
107
        }
108
        $obj['eventID'] = $eventId;
109
        $ret = $dataTable->create($obj);
110
        return $response->withJson($ret);
111
    }
112
113
    public function getEEShiftReportForEvent($request, $response, $args)
114
    {
115
        $eventId = $args['event'];
116
        if($this->canUpdate($request, null) === false)
117
        {
118
            return $response->withStatus(401);
119
        }
120
        $shiftDataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
121
        $obj = $request->getParsedBody();
122
        if($obj == NULL)
123
        {
124
            $obj = json_decode($request->getBody()->getContents(), true);
125
        }
126
        $filterStr = 'eventID eq '.$eventId.' and status eq filled';
127
        if(isset($obj['earlyLate']))
128
        {
129
            $filterStr .= ' and earlyLate eq \''.$obj['earlyLate'].'\'';
130
        }
131
        else
132
        {
133
            $filterStr .= " and earlyLate ne '-1'";
134
        }
135
        $filter = new \Data\Filter($filterStr);
136
        $shifts = $shiftDataTable->read($filter);
137
        $ret = array();
138
        $count = count($shifts);
0 ignored issues
show
Bug introduced by
It seems like $shifts can also be of type boolean; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
        $count = count(/** @scrutinizer ignore-type */ $shifts);
Loading history...
139
        for($i = 0; $i < $count; $i++)
140
        {
141
            $shift = new \VolunteerShift(false, $shifts[$i]);
142
            $vol = $shift->participantObj;
0 ignored issues
show
Bug Best Practice introduced by
The property $participantObj is declared protected in VolunteerShift. Since you implement __get, consider adding a @property or @property-read.
Loading history...
143
            $role = $shift->role;
0 ignored issues
show
Bug Best Practice introduced by
The property role does not exist on VolunteerShift. Since you implemented __get, consider adding a @property annotation.
Loading history...
144
            $entry = array('name' => $vol->getDisplayName('paperName'), 'email'=> $vol->email, 'dept'=> $shift->departmentID, 'role' => $role->display_name, 'earlyLate'=>$shift->earlyLate);
145
            array_push($ret, $entry);
146
        }
147
        return $response->withJson($ret);
148
    }
149
}
150
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
151