UsersController::changeAccountRole()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
1
<?php namespace App\Http\Controllers;
2
3
use App\Exceptions\RepositoryException;
4
use App\Exceptions\ValidationException;
5
use App\Repositories\ConnectRepository;
6
use App\Repositories\UserRepository;
7
use Input;
8
9
class UsersController extends Controller {
10
	
11
	private $userRepository;
12
    private $connectRepository;
13
14
	public function __construct(UserRepository $repository, ConnectRepository $connectRepository)
15
	{
16
		$this->userRepository = $repository;
17
        $this->connectRepository = $connectRepository;
18
	}
19
20 View Code Duplication
    public function getActiveUsers()
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...
21
    {
22
    	$allUsers = $this->userRepository->allByStatus(true);
23
        $otherUsersCount = count($this->userRepository->allByStatus(false));
24
25
        return view('users.app')->with('users', $allUsers)
26
        						->with('roles', $this->getRoleNames())
27
        						->with('isActive', true)
28
                                ->with('otherUsersCount', $otherUsersCount);
29
    }
30
    
31 View Code Duplication
    public function getDisabledUsers()
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...
32
    {
33
    	$allUsers = $this->userRepository->allByStatus(false);
34
        $otherUsersCount = count($this->userRepository->allByStatus(true));
35
36
    	return view('users.app')->with('users', $allUsers)
37
    							->with('roles', $this->getRoleNames())
38
    							->with('isActive', false)
39
                                ->with('otherUsersCount', $otherUsersCount);
40
    }
41
42
    public function showConnections($userId)
43
    {
44
        $user = $this->userRepository->get($userId);
45
        $connections = $this->connectRepository->forUser($userId);
46
47
        return view('users.history')->with('user', $user)
48
                                    ->with('connections', $connections);
49
    }
50
    
51
    public function getRegisterForm()
52
    {
53
    	return view('users.register');
54
    }
55
    
56 View Code Duplication
    public function registerNewUser()
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...
57
    {
58
    	try {
59
    		$user = $this->userRepository->store(Input::all());
60
    	}
61
        catch(ValidationException $e) {
62
            return redirect('app/users/register')->withInput()
63
                                                 ->with('error', $e->getMessageBag()->first());
64
        }
65
        catch (RepositoryException $e) {
66
    		return redirect('app/users/register')->with('error', 'Could not add new user: '.strtolower($e->getMessage()))
67
                                                 ->withInput();
68
    	}
69
    	    	
70
    	return redirect('app/users')->with('success', $user->firstname.' account added');
0 ignored issues
show
Documentation introduced by
The property firstname does not exist on object<App\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
71
    }
72
    
73
    public function changeAccountStatus($userId)
74
    {    	
75
    	try {
76
    		$user = $this->userRepository->changeStatus($userId);
77
    	}
78
    	catch(RepositoryException $e) {
79
    		return redirect('app/users')->with('error', 'Could not change account status');
80
    	}
81
    	
82
    	if($user->is_active)
83
    		return redirect('app/users/disabled')->with('success', 'User <b>'.$user->firstname.'</b> enabled');
84
    	else
85
    		return redirect('app/users')->with('success', 'User <b>'.$user->firstname.'</b> disabled');
86
    }
87
    
88
    public function changeAccountRole($userId)
89
    {
90
    	try {
91
    		$user = $this->userRepository->changeRole($userId);
92
    	}
93
    	catch(RepositoryException $e) {
94
    		return redirect('app/users')->with('error', 'Could not change user role');
95
    	}
96
    	
97
    	$redirectUrl = 'app/users';
98
    	if( !$user->is_active )
99
    		$redirectUrl .= '/disabled';
100
    	
101
    	$roleNames = ['ADMN'=>'administrator','MNGR'=>'manager','USER'=>'user'];
102
    	$roleName = $roleNames[$user->role];
103
    	
104
    	return redirect($redirectUrl)->with('success', 'User <b>'.$user->firstname.'</b> is now <i>'.$roleName.'</i>');
105
    }
106
    
107
    public function deleteUser($userId)
108
    {
109
    	try {
110
    		$user = $this->userRepository->softDelete($userId);
111
    	}
112
    	catch(RepositoryException $e) {
113
    		return redirect('app/users')->with('error', 'User could not be deleted: '.$e->getMessage());
114
    	}
115
    	
116
    	return redirect('app/users')->with('success', 'User <b>'.$user->firstname.'</b> deleted');
117
    }
118
119
    private function getRoleNames()
120
    {
121
        return ['ADMN'=>'Administrator','MNGR'=>'Manager','USER'=>'User'];
122
    }
123
124
}
125