Completed
Push — FVSv2 ( 1ec6b9...63e0fa )
by Patrick
01:36
created

ParticipantAPI::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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;
1 ignored issue
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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
        if(method_exists($this, 'processEntry'))
81
        {
82
            $areas[0] = $this->processEntry($areas[0], $request);
0 ignored issues
show
Bug introduced by
The method processEntry() does not seem to exist on object<ParticipantAPI>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
        }
84
        return $response->withJson($areas[0]);
85
    }
86
}
87