Issues (294)

src/EBloodBank/Controllers/Signup.php (3 issues)

Labels
Severity
1
<?php
2
/**
3
 * Sign-up page controller class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Controllers
7
 * @since      1.0
8
 */
9
namespace EBloodBank\Controllers;
10
11
use InvalidArgumentException;
12
use EBloodBank as EBB;
13
use EBloodBank\Options;
14
use EBloodBank\Notices;
15
use EBloodBank\Models\User;
16
use EBloodBank\Models\Donor;
17
use Symfony\Component\EventDispatcher\GenericEvent;
18
19
/**
20
 * Sign-up page controller class
21
 *
22
 * @since 1.0
23
 */
24
class Signup extends Controller
25
{
26
    /**
27
     * @return void
28
     * @since 1.0
29
     */
30
    public function __invoke()
31
    {
32
        if ('on' === Options::getOption('self_registration')) {
33
            $this->doActions();
34
            $view = $this->viewFactory->forgeView('signup');
35
        } else {
36
            $view = $this->viewFactory->forgeView('error-403');
37
        }
38
        $view();
39
    }
40
41
    /**
42
     * @return void
43
     * @since 1.0
44
     */
45
    protected function doActions()
46
    {
47
        switch (filter_input(INPUT_POST, 'action')) {
48
            case 'signup':
49
                $this->doSignupAction();
50
                break;
51
        }
52
    }
53
54
    /**
55
     * @return void
56
     * @since 1.0
57
     */
58
    protected function doSignupAction()
59
    {
60
        try {
61
            $user = new User();
62
63
            // Set the user name.
64
            $user->set('name', filter_input(INPUT_POST, 'user_name'), true);
65
66
            // Set the user email.
67
            $user->set('email', filter_input(INPUT_POST, 'user_email'), true);
68
69
            $duplicateUser = $this->getUserRepository()->findOneBy(['email' => $user->get('email'), 'status' => 'any']);
70
71
            if (! empty($duplicateUser)) {
72
                throw new InvalidArgumentException(__('Please enter another e-mail address.'));
73
            }
74
75
            $userPass1 = filter_input(INPUT_POST, 'user_pass_1', FILTER_UNSAFE_RAW);
76
            $userPass2 = filter_input(INPUT_POST, 'user_pass_2', FILTER_UNSAFE_RAW);
77
78
            if (empty($userPass1)) {
79
                throw new InvalidArgumentException(__('Please enter your password.'));
80
            }
81
82
            if (empty($userPass2)) {
83
                throw new InvalidArgumentException(__('Please confirm your password.'));
84
            }
85
86
            if ($userPass1 !== $userPass2) {
87
                throw new InvalidArgumentException(__('Please enter the same password.'));
88
            }
89
90
            // Set the user password.
91
            $user->set('pass', password_hash($userPass1, PASSWORD_BCRYPT), false);
92
93
            // Set the user role.
94
            $user->set('role', Options::getOption('new_user_role'), true);
95
96
            // Set the user status.
97
            $user->set('status', Options::getOption('new_user_status'), true);
98
99
            $this->getEntityManager()->persist($user);
100
            $this->getEntityManager()->flush();
101
102
            $signedup = $user->isExists();
103
104
            $this->getEventManager()->getEventDispatcher()->dispatch('user.created', new GenericEvent($user));
105
106
            $addDonor = filter_input(INPUT_POST, 'add_as_a_donor');
107
108
            if ($addDonor) {
109
                $donor = new Donor();
110
111
                // Set the donor name.
112
                $donor->set('name', filter_input(INPUT_POST, 'user_name'), true);
113
114
                // Set the donor gender.
115
                $donor->set('gender', filter_input(INPUT_POST, 'donor_gender'), true);
116
117
                // Set the donor birthdate.
118
                $donor->set('birthdate', filter_input(INPUT_POST, 'donor_birthdate'), true);
119
120
                // Set the donor blood group.
121
                $donor->set('blood_group', filter_input(INPUT_POST, 'donor_blood_group'), true);
122
123
                // Set the donor district ID.
124
                $donor->set('district', $this->getDistrictRepository()->find(filter_input(INPUT_POST, 'donor_district_id')));
125
126
                // Set the originator user.
127
                $donor->set('created_by', $user);
128
129
                // Set the donor status.
130
                $donor->set('status', 'pending');
131
132
                // Set the donor weight.
133
                $donor->setMeta('weight', filter_input(INPUT_POST, 'donor_weight'), true);
134
135
                // Set the donor email address.
136
                $donor->setMeta('email', filter_input(INPUT_POST, 'user_email'), true);
137
138
                // Set the donor email address visibility.
139
                $donor->setMeta('email_visibility', Options::getOption('default_donor_email_visibility'), true);
140
141
                // Set the donor phone number.
142
                $donor->setMeta('phone', filter_input(INPUT_POST, 'donor_phone'), true);
143
144
                // Set the donor phone number visibility.
145
                $donor->setMeta('phone_visibility', Options::getOption('default_donor_phone_visibility'), true);
146
147
                // Set the donor address.
148
                $donor->setMeta('address', filter_input(INPUT_POST, 'donor_address'), true);
149
150
                $this->getEntityManager()->persist($donor);
151
                $this->getEntityManager()->flush();
152
153
                $this->getEventManager()->getEventDispatcher()->dispatch('donor.created', new GenericEvent($donor));
154
            }
155
156
            EBB\redirect(
0 ignored issues
show
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

156
            /** @scrutinizer ignore-call */ 
157
            EBB\redirect(
Loading history...
157
                EBB\addQueryArgs(
0 ignored issues
show
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

157
                /** @scrutinizer ignore-call */ 
158
                EBB\addQueryArgs(
Loading history...
158
                    EBB\getLoginURL(),
0 ignored issues
show
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

158
                    /** @scrutinizer ignore-call */ 
159
                    EBB\getLoginURL(),
Loading history...
159
                    ['flag-signedup' => $signedup]
160
                )
161
            );
162
        } catch (InvalidArgumentException $ex) {
163
            Notices::addNotice('invalid_user_argument', $ex->getMessage());
164
        }
165
    }
166
}
167