DeleteUsers::__invoke()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 4
nop 0
dl 0
loc 19
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * Delete users page controller class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Controllers
7
 * @since      1.1
8
 */
9
namespace EBloodBank\Controllers;
10
11
use EBloodBank as EBB;
12
13
/**
14
 * Delete users page controller class
15
 *
16
 * @since 1.1
17
 */
18
class DeleteUsers extends Controller
19
{
20
    /**
21
     * @var \EBloodBank\Models\User[]
22
     * @since 1.1
23
     */
24
    protected $users = [];
25
26
    /**
27
     * @return void
28
     * @since 1.1
29
     */
30
    public function __invoke()
31
    {
32
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'User', 'delete')) {
33
            $this->viewFactory->displayView('error-403');
34
            return;
35
        }
36
37
        if (filter_has_var(INPUT_POST, 'users')) {
38
            $usersIDs = filter_input(INPUT_POST, 'users', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY);
39
            if (! empty($usersIDs) && is_array($usersIDs)) {
40
                $this->users = $this->getUserRepository()->findBy(['id' => $usersIDs]);
41
            }
42
        }
43
44
        $this->doActions();
45
        $this->viewFactory->displayView(
46
            'delete-users',
47
            [
48
                'users' => $this->users,
49
            ]
50
        );
51
    }
52
53
    /**
54
     * @return void
55
     * @since 1.1
56
     */
57
    protected function doActions()
58
    {
59
        switch (filter_input(INPUT_POST, 'action')) {
60
            case 'delete_users':
61
                $this->doDeleteAction();
62
                break;
63
        }
64
    }
65
66
    /**
67
     * @return void
68
     * @since 1.1
69
     */
70
    protected function doDeleteAction()
71
    {
72
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'User', 'delete')) {
73
            return;
74
        }
75
76
        $sessionToken = $this->getSession()->getCsrfToken();
77
        $actionToken = filter_input(INPUT_POST, 'token');
78
79
        if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
80
            return;
81
        }
82
83
        $users = $this->users;
84
85
        if (! $users || ! is_array($users)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $users of type EBloodBank\Models\User[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
86
            return;
87
        }
88
89
        $deletedUsersCount = 0;
90
91
        foreach ($users as $user) {
92
            if ($this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $user)) {
93
                $this->getEntityManager()->remove($user);
94
                $deletedUsersCount++;
95
            }
96
        }
97
98
        $this->getEntityManager()->flush();
99
100
        EBB\redirect(
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

100
        /** @scrutinizer ignore-call */ 
101
        EBB\redirect(
Loading history...
101
            EBB\addQueryArgs(
0 ignored issues
show
Bug introduced by
The function addQueryArgs was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

101
            /** @scrutinizer ignore-call */ 
102
            EBB\addQueryArgs(
Loading history...
102
                EBB\getEditUsersURL(),
0 ignored issues
show
Bug introduced by
The function getEditUsersURL was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

102
                /** @scrutinizer ignore-call */ 
103
                EBB\getEditUsersURL(),
Loading history...
103
                ['flag-deleted' => $deletedUsersCount]
104
            )
105
        );
106
    }
107
}
108