UserController   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 11
Bugs 0 Features 1
Metric Value
wmc 17
c 11
b 0
f 1
lcom 1
cbo 5
dl 0
loc 112
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createUser() 0 16 3
B login() 0 25 5
A decryptPassword() 0 8 2
B logout() 0 21 6
1
<?php
2
3
/**
4
 * UserController Managers user activity login, register and logout
5
 *
6
 * @package Ibonly\NaijaEmoji\UserController
7
 * @author  Ibraheem ADENIYI <[email protected]>
8
 * @license MIT <https://opensource.org/licenses/MIT>
9
 */
10
11
namespace Ibonly\NaijaEmoji;
12
13
use Slim\Slim;
14
use Ibonly\NaijaEmoji\User;
15
use Ibonly\NaijaEmoji\UserInterface;
16
use Ibonly\NaijaEmoji\AuthController;
17
use Ibonly\PotatoORM\DataNotFoundException;
18
use Ibonly\NaijaEmoji\InvalidTokenException;
19
use Ibonly\NaijaEmoji\ProvideTokenException;
20
use Ibonly\NaijaEmoji\PasswordExistException;
21
use Ibonly\PotatoORM\DataAlreadyExistException;
22
23
class UserController implements UserInterface
24
{
25
    protected $user;
26
    protected $auth;
27
28
    public function __construct ()
29
    {
30
        $this->user = new User();
31
        $this->auth = new AuthController();
32
    }
33
34
    /**
35
     * createUser Create a new user
36
     *
37
     * @param  $app
38
     *
39
     * @return json
40
     */
41
    public function createUser (Slim $app)
42
    {
43
        $username = $app->request->params('username');
44
        $this->user->id = NULL;
0 ignored issues
show
Bug introduced by
The property id does not seem to exist in Ibonly\NaijaEmoji\User.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
45
        $this->user->username = $username;
0 ignored issues
show
Bug introduced by
The property username does not seem to exist in Ibonly\NaijaEmoji\User.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
46
        $this->user->password = $this->auth->passwordEncrypt($app->request->params('password'));
0 ignored issues
show
Bug introduced by
The property password does not seem to exist in Ibonly\NaijaEmoji\User.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47
        $this->user->date_created = date('Y-m-d H:i:s');
0 ignored issues
show
Bug introduced by
The property date_created does not seem to exist in Ibonly\NaijaEmoji\User.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
48
        try
49
        {
50
            $save = $this->user->save();
51
            if( $save )
52
                $app->halt(201, json_encode(['message' => 'Registration Successful. Please Login to generate your token']));
53
        } catch ( DataAlreadyExistException $e ) {
54
            $app->halt(404, json_encode(['message' => 'User details already exist']));
55
        }
56
    }
57
58
    /**
59
     * login Log user in and generate token
60
     *
61
     * @param  $app
62
     *
63
     * @return json
64
     */
65
    public function login (Slim $app)
66
    {
67
        $app->response->headers->set('Content-Type', 'application/json');
68
        $username = $app->request->params('username');
69
        $password = $app->request->params('password');
70
        try
71
        {
72
            //check if username is available
73
            $login = $this->user->where(['username' => $username])->toJson();
74
            if( ! empty ($login) )
75
                $hashPassword = "";
76
                $output = json_decode($login);
77
                foreach( $output as $key )
78
                {
79
                    $output = $key->id;
80
                    $hashPassword = $key->password;
81
                }
82
                //confirm the password
83
                return $this->decryptPassword($username, $password, $hashPassword);
0 ignored issues
show
Bug introduced by
The variable $hashPassword does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
84
        } catch ( DataNotFoundException $e ) {
85
            $app->halt(404, json_encode(['message' => 'Not Found']));
86
        } catch ( PasswordException $e ) {
87
            return $e->errorMessage();
88
        }
89
    }
90
91
    /**
92
     * decryptPassword and return token
93
     *
94
     * @param  $user
95
     * @param  $password
96
     * @param  $hashPassword
97
     */
98
    public function decryptPassword ($username, $password, $hashPassword)
99
    {
100
        if( $this->auth->passwordDecrypt($password, $hashPassword) )
101
            return(json_encode([
102
                'Username' => $username,
103
                'Authorization' => $this->auth->authorizationEncode($username)
104
            ]));
105
    }
106
    /**
107
     * logout Log user out and destroy token
108
     *
109
     * @param  $app
110
     *
111
     * @return json
112
     */
113
    public function logout (Slim $app)
114
    {
115
        $app->response->headers->set('Content-Type', 'application/json');
116
        $tokenData = $app->request->headers->get('Authorization');
117
        try
118
        {
119
            if ( ! isset( $tokenData ) )
120
                throw new ProvideTokenException();
121
122
            $checkUser = $this->user->where(['username' => $tokenData->user])->toJson();
123
            if ( ! empty ($checkUser) )
124
                $this->auth->authorizationEncode(NULL);#
125
                $app->halt(200, json_encode(['message' => 'Logged out Successfully']));
126
        } catch ( DataNotFoundException $e) {
127
            $app->halt(404, json_encode(['message' => 'Not Found']));
128
        } catch ( InvalidTokenException $e ) {
129
            $app->halt(405, json_encode(['Message' => 'Invalid Token']));
130
        } catch ( ProvideTokenException $e ) {
131
            $app->halt(406, json_encode(['Message' => 'Enter a valid Token']));
132
        }
133
    }
134
}