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

PendingUserAPI   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 85
Duplicated Lines 14.12 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 12
loc 85
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 7 1
A validateIsAdmin() 12 12 3
A listPendingUsers() 0 9 1
A showPendingUser() 0 14 4
A deletePendingUser() 0 7 1
B activatePendingUser() 0 28 6

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 PendingUserAPI extends Http\Rest\RestAPI
3
{
4
    public function setup($app)
5
    {
6
        $app->get('[/]', array($this, 'listPendingUsers'));
7
        $app->get('/{hash}[/]', array($this, 'showPendingUser'));
8
        $app->delete('/{hash}[/]', array($this, 'deletePendingUser'));
9
        $app->map(['GET', 'POST'], '/{hash}/Actions/activate[/]', array($this, 'activatePendingUser'));
10
    }
11
12 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...
13
    {
14
        $user = $request->getAttribute('user');
15
        if($user === false)
16
        {
17
            throw new Exception('Must be logged in', \Http\Rest\ACCESS_DENIED);
18
        }
19
        if(!$user->isInGroupNamed('LDAPAdmins'))
20
        {
21
            throw new Exception('Must be Admin', \Http\Rest\ACCESS_DENIED);
22
        }
23
    }
24
25
    public function listPendingUsers($request, $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
    {
27
        $this->validateIsAdmin($request);
28
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
29
        $auth = AuthProvider::getInstance();
30
        $users = $auth->getPendingUsersByFilter($odata->filter, $odata->select, $odata->top, $odata->skip, 
31
                                                $odata->orderby);
32
        return $response->withJson($users);
33
    }
34
35
    public function showPendingUser($request, $response, $args)
36
    {
37
        $this->validateIsAdmin($request);
38
        $user = \AuthProvider::getInstance()->getPendingUsersByFilter(new \Data\Filter("hash eq '".$args['hash']."'"));
39
        if($user === false)
40
        {
41
            return $response->withStatus(404);
42
        }
43
        if(!is_object($user) && isset($user[0]))
44
        {
45
            $user = $user[0];
46
        }
47
        return $response->withJson($user);
48
    }
49
50
    public function deletePendingUser($request, $response, $args)
51
    {
52
        $this->validateIsAdmin($request);
53
        $auth = \AuthProvider::getInstance();
54
        $res = $auth->deletePendingUsersByFilter(new \Data\Filter("hash eq '".$args['hash']."'"));
55
        return $response->withJson($res);
56
    }
57
58
    public function activatePendingUser($request, $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
    {
60
        $user = $request->getAttribute('user');
61
        if($user === false)
62
        {
63
            throw new Exception('Must be logged in', \Http\Rest\ACCESS_DENIED);
64
        }
65
        $auth = \AuthProvider::getInstance();
66
        $user = $auth->getPendingUsersByFilter(new \Data\Filter("hash eq '$hash'"));
0 ignored issues
show
Bug introduced by
The variable $hash does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
67
        if($user === false || !isset($user[0]))
68
        {
69
            return $app->withStatus(404);
0 ignored issues
show
Bug introduced by
The variable $app does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
70
        }
71
        $res = $auth->activatePendingUser($user[0]);
72
        if($request->isGet())
73
        {
74
            $uri = '../../activate_error.php';
75
            if($res)
76
            {
77
                $uri = '../../';
78
            }
79
            return $response->withStatus(302)->withHeader('Location', $uri);
80
        }
81
        else
82
        {
83
            return $response->withJson($res);
84
        }
85
    }
86
}
87
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
88