Login::addNotices()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Log-in 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 EBloodBank\Notices;
13
14
/**
15
 * Log-in page controller class
16
 *
17
 * @since 1.0
18
 */
19
class Login extends Controller
20
{
21
    /**
22
     * @return void
23
     * @since 1.0
24
     */
25
    public function __invoke()
26
    {
27
        $this->doActions();
28
        $this->addNotices();
29
        $view = $this->viewFactory->forgeView('login');
30
        $view();
31
    }
32
33
    /**
34
     * @return void
35
     * @since 1.0
36
     */
37
    protected function doActions()
38
    {
39
        if ('logout' === filter_input(INPUT_GET, 'action')) {
40
            $this->doLogoutAction();
41
        } elseif ('login' === filter_input(INPUT_POST, 'action')) {
42
            $this->doLoginAction();
43
        }
44
    }
45
46
    /**
47
     * @return void
48
     * @since 1.0
49
     */
50
    protected function addNotices()
51
    {
52
        if (filter_has_var(INPUT_GET, 'flag-loggedout')) {
53
            Notices::addNotice('loggedout', __('You are now logged out.'), 'info');
54
        }
55
    }
56
57
    /**
58
     * @return void
59
     * @since 1.0
60
     */
61
    protected function doLoginAction()
62
    {
63
        $userEmail = filter_input(INPUT_POST, 'user_email');
64
        $userPass  = filter_input(INPUT_POST, 'user_pass');
65
66
        if (empty($userEmail) || empty($userPass)) {
67
            Notices::addNotice('empty_login_details', __('Please enter your login details.'), 'warning');
68
            return;
69
        }
70
71
        $user = $this->getUserRepository()->findOneBy(['email' => $userEmail, 'status' => 'any']);
72
73
        if (empty($user) || ! password_verify($userPass, $user->get('pass'))) {
74
            Notices::addNotice('wrong_login_details', __('No match for user e-mail and/or password.'), 'warning');
75
            return;
76
        }
77
78
        if ($user->isPending()) {
79
            Notices::addNotice('account_pending_moderation', __('Your account is pending moderation.'), 'warning');
80
            return;
81
        }
82
83
        $segment = $this->getSession()->getSegment('EBloodBank');
84
        $segment->set('user_id', (int) $user->get('id'));
85
        $this->getSession()->regenerateId();
86
87
        EBB\redirect(EBB\getHomeURL());
0 ignored issues
show
Bug introduced by
The function getHomeURL 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

87
        EBB\redirect(/** @scrutinizer ignore-call */ EBB\getHomeURL());
Loading history...
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

87
        /** @scrutinizer ignore-call */ 
88
        EBB\redirect(EBB\getHomeURL());
Loading history...
88
    }
89
90
    /**
91
     * @return void
92
     * @since 1.0
93
     */
94
    protected function doLogoutAction()
95
    {
96
        if ($this->hasAuthenticatedUser()) {
97
            $this->getSession()->destroy();
98
            $this->getSession()->start();
99
            $this->getSession()->regenerateId();
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\getLoginURL(),
0 ignored issues
show
Bug introduced by
The function getLoginURL 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\getLoginURL(),
Loading history...
103
                    ['flag-loggedout' => true]
104
                )
105
            );
106
        }
107
    }
108
}
109