ActivateUsers   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 19 6
B doActivateAction() 0 36 9
A doActions() 0 6 2
1
<?php
2
/**
3
 * Activate 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
 * Activate users page controller class
15
 *
16
 * @since 1.1
17
 */
18
class ActivateUsers 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', 'activate')) {
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
            'activate-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 'activate_users':
61
                $this->doActivateAction();
62
                break;
63
        }
64
    }
65
66
    /**
67
     * @return void
68
     * @since 1.1
69
     */
70
    protected function doActivateAction()
71
    {
72
        // Double check
73
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'User', 'activate')) {
74
            return;
75
        }
76
77
        $actionToken = filter_input(INPUT_POST, 'token');
78
        $sessionToken = $this->getSession()->getCsrfToken();
79
80
        if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
81
            return;
82
        }
83
84
        if (empty($this->users)) {
85
            return;
86
        }
87
88
        $activatedUsersCount = 0;
89
90
        foreach ($this->users as $user) {
91
            if (! $user->isPending()) {
92
                continue;
93
            }
94
            if ($this->getAcl()->canActivateUser($this->getAuthenticatedUser(), $user)) {
95
                $user->set('status', 'activated');
96
                $activatedUsersCount++;
97
            }
98
        }
99
100
        $this->getEntityManager()->flush();
101
102
        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

102
        /** @scrutinizer ignore-call */ 
103
        EBB\redirect(
Loading history...
103
            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

103
            /** @scrutinizer ignore-call */ 
104
            EBB\addQueryArgs(
Loading history...
104
                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

104
                /** @scrutinizer ignore-call */ 
105
                EBB\getEditUsersURL(),
Loading history...
105
                ['flag-activated' => $activatedUsersCount]
106
            )
107
        );
108
    }
109
}
110