Completed
Push — master ( df6d12...eab9e2 )
by Patrick
01:41 queued 10s
created

EventAPI::canDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 8
rs 10
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
    }
17
18
    protected function canUpdate($request, $entity)
19
    {
20
 	if($this->isVolunteerAdmin($request))
21
        {
22
            return true;
23
        }       
24
        return false;
25
    }
26
27
    protected function canDelete($request, $entity)
28
    {
29
        if($this->isVolunteerAdmin($request))
30
        {
31
            return true;
32
        }
33
        return false;
34
    }
35
36
    public function processEntry($entry, $request)
37
    {
38
        $entry['available'] = true;
39
        $endTime = new DateTime($entry['endTime']);
40
        $now = new DateTime();
41
        if($endTime < $now)
42
        {
43
            $entry['available'] = false;
44
            $entry['why'] = 'Event is in the past';
45
        }
46
        if($entry['private'] && !in_array($this->user->mail, $entry['volList']))
47
        {
48
            $entry['available'] = false;
49
            $entry['why'] = 'Event is private and you are not invited';
50
        }
51
        if(!$entry['available'] && !$this->isVolunteerAdmin($request))
52
        {
53
            return null;
54
        }
55
        return $entry;
56
    }
57
58
    public function getShiftsForEvent($request, $response, $args)
59
    {
60
        $this->validateLoggedIn($request);
61
        $eventId = $args['event'];
62
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
63
        $filter = new \Data\Filter("eventID eq '$eventId'");
64
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
65 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...
66
        {
67
            $clause = $odata->filter->getClause('eventID');
68
            if($clause !== null)
69
            {
70
                return $response->withStatus(409);
71
            }
72
            else
73
            {
74
                $filter->appendChild('and');
75
                $filter->appendChild($odata->filter);
76
            }
77
        }
78
        $shifts = $dataTable->read($filter, $odata->select, $odata->top,
79
                                  $odata->skip, $odata->orderby);
80
        if($shifts === false)
81
        {
82
            $shifts = array();
83
        }
84
        $count = count($shifts);
85
        for($i = 0; $i < $count; $i++)
86
        {
87
            $shifts[$i] = $this->processShift($shifts[$i], $request);
88
        }
89
        $shifts = array_values(array_filter($shifts));
90
        return $response->withJson($shifts);
91
    }
92
93 View Code Duplication
    public function createShiftForEvent($request, $response, $args)
94
    {
95
        $eventId = $args['event'];
96
        if($this->canUpdate($request, null) === false)
97
        {
98
            return $response->withStatus(401);
99
        }
100
        $dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts');
101
        $obj = $request->getParsedBody();
102
        if($obj == NULL)
103
        {
104
            $obj = json_decode($request->getBody()->getContents(), true);
105
        }
106
        $obj['eventID'] = $eventId;
107
        $ret = $dataTable->create($obj);
108
        return $response->withJson($ret);
109
    }
110
}
111