Completed
Push — master ( 7a1f03...304ca2 )
by Patrick
01:42
created

EventAPI::getEEShiftReportForEvent()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 9
nop 3
dl 0
loc 36
rs 9.0328
c 0
b 0
f 0
1
<?php
2
class EventAPI extends VolunteerAPI
3
{
4
    use Processor;
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 canUpdate($request, $entity)
21
    {
22
 	if($this->isVolunteerAdmin($request))
23
        {
24
            return true;
25
        }       
26
        return false;
27
    }
28
29
    protected function canDelete($request, $entity)
30
    {
31
        if($this->isVolunteerAdmin($request))
32
        {
33
            return true;
34
        }
35
        return false;
36
    }
37
38
    public function processEntry($entry, $request)
39
    {
40
        $entry['available'] = true;
41
        $endTime = new DateTime($entry['endTime']);
42
        $now = new DateTime();
43
        if($endTime < $now)
44
        {
45
            $entry['available'] = false;
46
            $entry['why'] = 'Event is in the past';
47
        }
48
        if($entry['private'] && !in_array($this->user->mail, $entry['volList']))
49
        {
50
            $entry['available'] = false;
51
            $entry['why'] = 'Event is private and you are not invited';
52
        }
53
        if(!$entry['available'] && !$this->isVolunteerAdmin($request))
54
        {
55
            return null;
56
        }
57
        return $entry;
58
    }
59
60
    public function getShiftsForEvent($request, $response, $args)
61
    {
62
        $this->validateLoggedIn($request);
63
        $eventId = $args['event'];
64
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
65
        $filter = new \Data\Filter("eventID eq '$eventId'");
66
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
67 View Code Duplication
        if($odata->filter !== false)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
68
        {
69
            $clause = $odata->filter->getClause('eventID');
70
            if($clause !== null)
71
            {
72
                return $response->withStatus(409);
73
            }
74
            else
75
            {
76
                $filter->appendChild('and');
77
                $filter->appendChild($odata->filter);
78
            }
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);
87
        for($i = 0; $i < $count; $i++)
88
        {
89
            $shifts[$i] = $this->processShift($shifts[$i], $request);
90
        }
91
        $shifts = array_values(array_filter($shifts));
92
        return $response->withJson($shifts);
93
    }
94
95 View Code Duplication
    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);
139
        for($i = 0; $i < $count; $i++)
140
        {
141
            $shift = new \VolunteerShift(false, $shifts[$i]);
142
            $vol = $shift->participantObj;
0 ignored issues
show
Documentation introduced by
The property $participantObj is declared protected in VolunteerShift. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
143
            $role = $shift->role;
0 ignored issues
show
Bug introduced by
The property role does not seem to exist in VolunteerShift.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

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