Completed
Push — master ( bf23bc...4f23d4 )
by Patrick
06:42
created

AreasAPI   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 23.08 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 12
loc 52
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setup() 0 5 1
A validateIsAdmin() 12 12 3
A canCreate() 0 5 1
A canUpdate() 0 5 1
A getLeads() 0 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
class AreasAPI extends Http\Rest\DataTableAPI
3
{
4
    public function __construct()
5
    {
6
        parent::__construct('profiles', 'area', '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
        $app->get('/{name}/leads', array($this, 'getLeads'));
13
    }
14
15 View Code Duplication
    protected function validateIsAdmin($request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
16
    {
17
        $user = $request->getAttribute('user');
18
        if($user === false)
19
        {
20
            throw new Exception('Must be logged in', \Http\Rest\ACCESS_DENIED);
21
        }
22
        if(!$user->isInGroupNamed('LDAPAdmins'))
23
        {
24
            throw new Exception('Must be Admin', \Http\Rest\ACCESS_DENIED);
25
        }
26
    }
27
28
    protected function canCreate($request)
29
    {
30
        $this->validateIsAdmin($request);
31
        return true;
32
    }
33
34
    protected function canUpdate($request, $entity)
35
    {
36
        $this->validateIsAdmin($request);
37
        return true;
38
    }
39
40
    public function getLeads($request, $response, $args)
41
    {
42
        $this->validateLoggedIn($request);
43
        $dataTable = DataSetFactory::getDataTableByNames('profiles', 'position');
44
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
45
        $leads = $dataTable->read(new \Data\Filter("area eq '".$args['name']."'"), $odata->select, $odata->top,
46
                                  $odata->skip, $odata->orderby);
47
        if(empty($leads))
48
        {
49
            return $response->withStatus(404);
50
        }
51
        return $response->withJson($leads);
52
    }
53
}
54
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
55