Passed
Pull Request — master (#50)
by Ronan
09:06
created

UsersController::toggleLevel()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 40
rs 8.9297
cc 6
nc 13
nop 3
1
<?php
2
3
namespace App\Controller;
4
5
use App\Facades\Router;
6
use App\Facades\Security;
7
use App\Facades\Session;
8
use App\Facades\View;
9
use App\Model\User;
10
use Exception;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Ronanchilvers\Orm\Orm;
14
use RuntimeException;
15
16
/**
17
 * Controller for administering users
18
 *
19
 * @author Ronan Chilvers <[email protected]>
20
 */
21
class UsersController
22
{
23
    /**
24
     * Login action for users
25
     *
26
     * @author Ronan Chilvers <[email protected]>
27
     */
28
    public function index(
29
        ServerRequestInterface $request,
30
        ResponseInterface $response
31
    ) {
32
        $users = Orm::finder(User::class)->all();
33
34
        return View::render(
35
            $response,
36
            '@web/users/index.html.twig',
37
            [
38
                'users' => $users,
39
            ]
40
        );
41
    }
42
43
    /**
44
     * Toggle a user's level
45
     *
46
     * @author Ronan Chilvers <[email protected]>
47
     */
48
    public function toggleLevel(
49
        ServerRequestInterface $request,
50
        ResponseInterface $response,
51
        $args
52
    ) {
53
        try {
54
            $id = filter_var($args['id'], FILTER_VALIDATE_INT, ['options' => ['default' => null ]]);
55
            if (is_null($id)) {
56
                throw new Exception('Invalid user id');
57
            }
58
            $user = Orm::finder(User::class)->one($id);
59
            if (! $user instanceof User) {
60
                throw new Exception('Unknown user');
61
            }
62
            if (Security::isCurrent($user)) {
0 ignored issues
show
Bug introduced by
The method isCurrent() does not exist on App\Facades\Security. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            if (Security::/** @scrutinizer ignore-call */ isCurrent($user)) {
Loading history...
63
                throw new Exception("Can't change your own level");
64
            }
65
            $user->toggleLevel();
66
            if (!$user->save()) {
67
                throw new Exception('Unable to update user level');
68
            }
69
            Session::flash(
70
                [
71
                    'heading' => "User updated",
72
                ],
73
                'info'
74
            );
75
            return $response->withRedirect(
0 ignored issues
show
Bug introduced by
The method withRedirect() does not exist on Psr\Http\Message\ResponseInterface. It seems like you code against a sub-type of Psr\Http\Message\ResponseInterface such as Slim\Http\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
            return $response->/** @scrutinizer ignore-call */ withRedirect(
Loading history...
76
                Router::pathFor('users.index')
77
            );
78
79
        } catch (Exception $ex) {
80
            Session::flash(
81
                [
82
                    'heading' => $ex->getMessage(),
83
                ],
84
                'error'
85
            );
86
            return $response->withRedirect(
87
                Router::pathFor('users.index')
88
            );
89
        }
90
    }
91
92
    /**
93
     * Toggle a user's status
94
     *
95
     * @author Ronan Chilvers <[email protected]>
96
     */
97
    public function toggleStatus(
98
        ServerRequestInterface $request,
99
        ResponseInterface $response,
100
        $args
101
    ) {
102
        try {
103
            $id = filter_var($args['id'], FILTER_VALIDATE_INT, ['options' => ['default' => null ]]);
104
            if (is_null($id)) {
105
                throw new Exception('Invalid user id');
106
            }
107
            $user = Orm::finder(User::class)->one($id);
108
            if (! $user instanceof User) {
109
                throw new Exception('Unknown user');
110
            }
111
            if (Security::isCurrent($user)) {
112
                throw new Exception("Can't change your own status");
113
            }
114
            if ($user->isActive() || $user->isInvited()) {
115
                $user->deactivate();
116
            } else {
117
                $user->activate();
118
            }
119
            if (!$user->save()) {
120
                throw new Exception('Unable to update user status');
121
            }
122
            Session::flash(
123
                [
124
                    'heading' => "User updated",
125
                ],
126
                'info'
127
            );
128
            return $response->withRedirect(
129
                Router::pathFor('users.index')
130
            );
131
132
        } catch (Exception $ex) {
133
            Session::flash(
134
                [
135
                    'heading' => $ex->getMessage(),
136
                ],
137
                'error'
138
            );
139
            return $response->withRedirect(
140
                Router::pathFor('users.index')
141
            );
142
        }
143
    }
144
}
145