ActivateUser   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A doActivateAction() 0 24 6
A doActions() 0 6 2
A __invoke() 0 26 6
1
<?php
2
/**
3
 * Activate user page controller class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Controllers
7
 * @since      1.0
8
 */
9
namespace EBloodBank\Controllers;
10
11
use EBloodBank as EBB;
12
use Psr\Container\ContainerInterface;
13
14
/**
15
 * Activate user page controller class
16
 *
17
 * @since 1.0
18
 */
19
class ActivateUser extends Controller
20
{
21
    /**
22
     * @var int
23
     * @since 1.6
24
     */
25
    protected $userId = 0;
26
27
    /**
28
     * @var \EBloodBank\Models\User|null
29
     * @since 1.0
30
     */
31
    protected $user;
32
33
    /**
34
     * @since 1.0
35
     */
36
    public function __construct(ContainerInterface $container, $userID)
37
    {
38
        parent::__construct($container);
39
        if (EBB\isValidID($userID)) {
0 ignored issues
show
Bug introduced by
The function isValidID 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

39
        if (/** @scrutinizer ignore-call */ EBB\isValidID($userID)) {
Loading history...
40
            $this->userId = (int) $userID;
41
        }
42
    }
43
44
    /**
45
     * @return void
46
     * @since 1.0
47
     */
48
    public function __invoke()
49
    {
50
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'User', 'activate')) {
51
            $this->viewFactory->displayView('error-403');
52
            return;
53
        }
54
55
        if ($this->userID) {
0 ignored issues
show
Bug introduced by
The property userID does not exist on EBloodBank\Controllers\ActivateUser. Did you mean userId?
Loading history...
56
            $this->user = $this->getUserRepository()->find($this->userID);
57
        }
58
59
        if (! $this->user) {
60
            $this->viewFactory->displayView('error-404');
61
            return;
62
        }
63
64
        if (! $this->getAcl()->canActivateUser($this->getAuthenticatedUser(), $this->user)) {
65
            $this->viewFactory->displayView('error-403');
66
            return;
67
        }
68
69
        $this->doActions();
70
        $this->viewFactory->displayView(
71
            'activate-user',
72
            [
73
                'user' => $this->user,
74
            ]
75
        );
76
    }
77
78
    /**
79
     * @return void
80
     * @since 1.0
81
     */
82
    protected function doActions()
83
    {
84
        switch (filter_input(INPUT_POST, 'action')) {
85
            case 'activate_user':
86
                $this->doActivateAction();
87
                break;
88
        }
89
    }
90
91
    /**
92
     * @return void
93
     * @since 1.0
94
     */
95
    protected function doActivateAction()
96
    {
97
        $actionToken = filter_input(INPUT_POST, 'token');
98
        $sessionToken = $this->getSession()->getCsrfToken();
99
100
        if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
101
            return;
102
        }
103
104
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canActivateUser($this->getAuthenticatedUser(), $this->user)) {
0 ignored issues
show
Bug introduced by
It seems like $this->user can also be of type null; however, parameter $user of EBloodBank\AclInterface::canActivateUser() does only seem to accept EBloodBank\Models\User, maybe add an additional type check? ( Ignorable by Annotation )

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

104
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canActivateUser($this->getAuthenticatedUser(), /** @scrutinizer ignore-type */ $this->user)) {
Loading history...
105
            return;
106
        }
107
108
        if (! $this->user->isPending()) {
0 ignored issues
show
Bug introduced by
The method isPending() does not exist on null. ( Ignorable by Annotation )

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

108
        if (! $this->user->/** @scrutinizer ignore-call */ isPending()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
            return;
110
        }
111
112
        $this->user->set('status', 'activated');
113
        $this->getEntityManager()->flush($this->user);
114
115
        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

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

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

117
                /** @scrutinizer ignore-call */ 
118
                EBB\getEditUsersURL(),
Loading history...
118
                ['flag-activated' => 1]
119
            )
120
        );
121
    }
122
}
123