Completed
Pull Request — develop (#51)
by Patrick
02:57
created

LeadsAPI::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
class LeadsAPI extends ProfilesAdminDataAPI
3
{
4
    public function __construct()
5
    {
6
        parent::__construct('profiles', 'position', 'short_name');
0 ignored issues
show
Documentation introduced by
'short_name' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
7
    }
8
9
    public function setup($app)
10
    {
11
        parent::setup($app);
12
    }
13
14
    protected function canCreate($request)
15
    {
16
        $this->validateIsAdmin($request);
17
        return true;
18
    }
19
20
    protected function canUpdate($request, $entity)
21
    {
22
        $this->validateIsAdmin($request);
23
        return true;
24
    }
25
26
    protected function hasPositionAccess()
27
    {
28
        return ($this->user->isInGroupNamed('Leads') ||
0 ignored issues
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...
29
                $this->user->isInGroupNamed('CC') ||
30
                $this->user->isInGroupNamed('AFs'));
31
    }
32
33
    protected function getPositionsByType($type, $auth)
34
    {
35
        switch($type)
36
        {
37
            case 'aar':
38
                $aarGroup = $auth->getGroupByName('AAR');
39
                return $aarGroup->members(true, false);
40
            case 'af':
41
                $afGroup = $auth->getGroupByName('AFs');
42
                return $afGroup->members(true, false);
43
            case 'cc':
44
                $ccGroup = $auth->getGroupByName('CC');
45
                return $ccGroup->members(true, false);
46
            case 'lead':
47
                $leadGroup = $auth->getGroupByName('Leads');
48
                return $leadGroup->members(true, false);
49
            default:
50
                $filter = new \Data\Filter('ou eq '.$type);
51
                return $auth->getUsersByFilter($filter);
52
        }
53
    }
54
55
    protected function getPositionsWithParams($params)
56
    {
57
        $auth = AuthProvider::getInstance();
58
        if(isset($params['type']))
59
        {
60
            return $this->getPositionsByType($params['type'], $auth);
61
        }
62
        $leads = array();
63
        $leadGroup = $auth->getGroupByName('Leads');
64
        $aarGroup  = $auth->getGroupByName('AAR');
65
        $afGroup   = $auth->getGroupByName('AFs');
66
        $ccGroup   = $auth->getGroupByName('CC');
67
        $leads     = array_merge($leads, $leadGroup->members(true, false));
68
        $leads     = array_merge($leads, $aarGroup->members(true, false));
69
        $leads     = array_merge($leads, $afGroup->members(true, false));
70
        $leads     = array_merge($leads, $ccGroup->members(true, false));
71
        return $leads;
72
    }
73
74
    public function readEntries($request, $response, $args)
75
    {
76
        if($this->canRead($request) === false || $this->hasPositionAccess() === false)
77
        {
78
            return $response->withStatus(401);
79
        }
80
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
81
        $leads = $this->getPositionsWithParams($request->getQueryParams());
82
        $leads = $odata->filterArrayPerSelect($leads);
83
        return $response->withJson($leads);
84
    }
85
}
86
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
87