Passed
Push — master ( ea0e5f...0a83be )
by Patrick
08:11
created

EventAPI::processEntry()   C

Complexity

Conditions 13
Paths 24

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 19
c 1
b 0
f 0
nc 24
nop 2
dl 0
loc 33
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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(isset($entry['volList']) && !is_array($entry['volList']))
54
        {
55
            $entry['volList'] = explode(',', $entry['volList']);
56
            $count = count($entry['volList']);
57
            for($i = 0; $i < $count; $i++)
58
            {
59
                $entry['volList'][$i] = trim($entry['volList'][$i]);
60
            }
61
        }
62
        if($entry['private'] && !in_array($this->user->mail, $entry['volList']))
63
        {
64
            $entry['available'] = false;
65
            $entry['why'] = 'Event is private and you are not invited';
66
        }
67
        if(!$entry['available'] && !$this->isVolunteerAdmin($request) && !$this->userIsLeadCached($this->user))
68
        {
69
            return null;
70
        }
71
        if(!$this->isVolunteerAdmin($request) && !$this->userIsLeadCached($this->user) && isset($entry['eeLists']))
72
        {
73
            unset($entry['eeLists']);
74
        }
75
        return $entry;
76
    }
77
78
    public function getShiftsForEvent($request, $response, $args)
79
    {
80
        $this->validateLoggedIn($request);
81
        $eventId = $args['event'];
82
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
83
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
84
        $filter = $this->addRequiredFilter('eventID', $eventId, $odata);
85
        if($filter === false)
86
        {
87
            return $response->withStatus(409);
88
        }
89
        $shifts = $dataTable->read($filter, $odata->select, $odata->top,
90
                                    $odata->skip, $odata->orderby);
91
        if($shifts === false)
92
        {
93
            $shifts = array();
94
        }
95
        $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

95
        $count = count(/** @scrutinizer ignore-type */ $shifts);
Loading history...
96
        for($i = 0; $i < $count; $i++)
97
        {
98
            $shifts[$i] = $this->processShift($shifts[$i], $request);
99
        }
100
        $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

100
        $shifts = array_values(array_filter(/** @scrutinizer ignore-type */ $shifts));
Loading history...
101
        return $response->withJson($shifts);
102
    }
103
104
    public function createShiftForEvent($request, $response, $args)
105
    {
106
        $eventId = $args['event'];
107
        if($this->canUpdate($request, null) === false)
108
        {
109
            return $response->withStatus(401);
110
        }
111
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
112
        $obj = $request->getParsedBody();
113
        if($obj == NULL)
114
        {
115
            $obj = json_decode($request->getBody()->getContents(), true);
116
        }
117
        $obj['eventID'] = $eventId;
118
        $ret = $dataTable->create($obj);
119
        return $response->withJson($ret);
120
    }
121
122
    public function getEEShiftReportForEvent($request, $response, $args)
123
    {
124
        $eventId = $args['event'];
125
        if($this->canUpdate($request, null) === false)
126
        {
127
            return $response->withStatus(401);
128
        }
129
        $shiftDataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
130
        $obj = $request->getParsedBody();
131
        if($obj == NULL)
132
        {
133
            $obj = json_decode($request->getBody()->getContents(), true);
134
        }
135
        $filterStr = 'eventID eq '.$eventId.' and status eq filled';
136
        if(isset($obj['earlyLate']))
137
        {
138
            $filterStr .= ' and earlyLate eq \''.$obj['earlyLate'].'\'';
139
        }
140
        else
141
        {
142
            $filterStr .= " and earlyLate ne '-1'";
143
        }
144
        $filter = new \Data\Filter($filterStr);
145
        $shifts = $shiftDataTable->read($filter);
146
        $ret = array();
147
        $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

147
        $count = count(/** @scrutinizer ignore-type */ $shifts);
Loading history...
148
        for($i = 0; $i < $count; $i++)
149
        {
150
            $shift = new \VolunteerShift(false, $shifts[$i]);
151
            $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...
152
            $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...
153
            $entry = array('name' => $vol->getDisplayName('paperName'), 'email'=> $vol->email, 'dept'=> $shift->departmentID, 'role' => $role->display_name, 'earlyLate'=>$shift->earlyLate);
154
            array_push($ret, $entry);
155
        }
156
        return $response->withJson($ret);
157
    }
158
}
159
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
160