Completed
Push — FVSv2 ( 427c44...e6ffd8 )
by Patrick
01:43
created

ParticipantAPI   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 81
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A canCreate() 0 5 1
A canRead() 0 9 2
A canUpdate() 0 9 2
A canDelete() 0 4 1
A validateCreate() 0 18 3
A readEntry() 0 23 5
1
<?php
2
class ParticipantAPI extends VolunteerAPI
3
{
4
    public function __construct()
5
    {
6
        parent::__construct('participants', 'uid');
7
    }
8
9
    protected function canCreate($request)
10
    {
11
        $this->validateLoggedIn($request);
12
        return true;
13
    }
14
15
    protected function canRead($request)
16
    {
17
        if($this->isVolunteerAdmin($request))
18
        {
19
            return true;
20
        }
21
        //TODO give access to department leads
22
        return true;
23
    }
24
25
    protected function canUpdate($request, $entity)
26
    {
27
 	if($this->isVolunteerAdmin($request))
28
        {
29
            return true;
30
        }       
31
        //TODO give access to department lead
32
        return false;
33
    }
34
35
    protected function canDelete($request, $entity)
36
    {
37
        return $this->canUpdate($request, $entity);
38
    }
39
40
    protected function validateCreate(&$obj, $request)
41
    {
42
        if(isset($obj['uid']))
43
        {
44
            return false;
45
        }
46
        $uid = $this->user->uid;
47
        $dataTable = $this->getDataTable();
48
        $filter = $this->getFilterForPrimaryKey($uid);
49
        $users = $dataTable->read($filter);
50
        if(!empty($users))
51
        {
52
            //User is already created...
53
            return false;
54
        }
55
        $obj['uid'] = $uid;
56
        return true;
57
    }
58
59
    public function readEntry($request, $response, $args)
60
    {
61
        $this->validateLoggedIn($request);
62
        $uid = $args['name'];
63
        if($uid === 'me')
64
        {
65
            $uid = $this->user->uid;
66
        }
67
        else if($uid !== $this->user->uid && $this->canRead($request) === false)
68
        {
69
            return $response->withStatus(401);
70
        }
71
        $dataTable = $this->getDataTable();
72
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
73
        $filter = $this->getFilterForPrimaryKey($uid);
74
        $areas = $dataTable->read($filter, $odata->select, $odata->top,
75
                                  $odata->skip, $odata->orderby);
76
        if(empty($areas))
77
        {
78
            return $response->withStatus(404);
79
        }
80
        return $response->withJson($areas[0]);
81
    }
82
}
83