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

ProfilesAPI   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 6 1
A login() 0 18 4
A logout() 0 5 1
B validateZip() 0 34 5
1
<?php
2
class ProfilesAPI extends Http\Rest\RestAPI
3
{
4
    public function setup($app)
5
    {
6
        $app->post('/login[/]', array($this, 'login'));
7
        $app->post('/logout[/]', array($this, 'logout'));
8
        $app->post('/zip[/]', array($this, 'validateZip'));
9
    }
10
11
    public function login($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...
12
    {
13
        $params = $request->getParams();
14
        if(!isset($params['username']) || !isset($params['password']))
15
        {
16
            return $response->withStatus(400);
17
        }
18
        $auth = AuthProvider::getInstance();
19
        $res = $auth->login($params['username'], $params['password']);
20
        if($res === false)
21
        {
22
            return $response->withStatus(403);
23
        }
24
        else
25
        {
26
            return $response->withJson($res);
27
        }
28
    }
29
30
    public function logout($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...
31
    {
32
        FlipSession::end();
33
        return $response->withJson(true);
34
    }
35
36
    public function validateZip($request, $response, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $response 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...
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...
37
    {
38
        $obj = $request->getQueryParams();
39
        if(empty($obj))
40
        {
41
            $obj = (array)$request->getParsedBody();
42
        }
43
        $ret = false;
0 ignored issues
show
Unused Code introduced by
$ret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
        if($obj['c'] == 'US')
45
        {
46
            if(preg_match("/^([0-9]{5})(-[0-9]{4})?$/i", $obj['postalCode']))
47
            {
48
                $contents = file_get_contents('http://ziptasticapi.com/'.$obj['postalCode']);
49
                $resp = json_decode($contents);
50
                if(isset($resp->error))
51
                {
52
                    $ret = $resp->error;
53
                }
54
                else
55
                {
56
                    $ret = true;
57
                }
58
            }
59
            else
60
            {
61
                $ret = 'Invalid Zip Code!';
62
            }
63
        }
64
        else
65
        {
66
            $ret = true;
67
        }
68
        return $request->withJson($ret);
69
    }
70
}
71
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
72