AddDonor   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 15 3
A doActions() 0 6 2
A addNotices() 0 4 2
B doSubmitAction() 0 74 7
1
<?php
2
/**
3
 * Add donor 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\Notices;
14
use EBloodBank\Models\Donor;
15
use Symfony\Component\EventDispatcher\GenericEvent;
16
17
/**
18
 * Add donor page controller class
19
 *
20
 * @since 1.0
21
 */
22
class AddDonor extends Controller
23
{
24
    /**
25
     * @var \EBloodBank\Models\Donor
26
     * @since 1.0
27
     */
28
    protected $donor;
29
30
    /**
31
     * @return void
32
     * @since 1.0
33
     */
34
    public function __invoke()
35
    {
36
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'Donor', 'add')) {
37
            $this->viewFactory->displayView('error-403');
38
            return;
39
        }
40
41
        $this->donor = new Donor();
42
43
        $this->doActions();
44
        $this->addNotices();
45
        $this->viewFactory->displayView(
46
            'add-donor',
47
            [
48
                'donor' => $this->donor,
49
            ]
50
        );
51
    }
52
53
    /**
54
     * @return void
55
     * @since 1.0
56
     */
57
    protected function doActions()
58
    {
59
        switch (filter_input(INPUT_POST, 'action')) {
60
            case 'submit_donor':
61
                $this->doSubmitAction();
62
                break;
63
        }
64
    }
65
66
    /**
67
     * @return void
68
     * @since 1.0
69
     */
70
    protected function addNotices()
71
    {
72
        if (filter_has_var(INPUT_GET, 'flag-added')) {
73
            Notices::addNotice('added', __('Donor added.'), 'success');
74
        }
75
    }
76
77
    /**
78
     * @return void
79
     * @since 1.0
80
     */
81
    protected function doSubmitAction()
82
    {
83
        try {
84
            if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'Donor', 'add')) {
85
                return;
86
            }
87
88
            $sessionToken = $this->getSession()->getCsrfToken();
89
            $actionToken = filter_input(INPUT_POST, 'token');
90
91
            if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
92
                return;
93
            }
94
95
            $donor = $this->donor;
96
97
            // Set the donor name.
98
            $donor->set('name', filter_input(INPUT_POST, 'donor_name'), true);
99
100
            // Set the donor gender.
101
            $donor->set('gender', filter_input(INPUT_POST, 'donor_gender'), true);
102
103
            // Set the donor birthdate.
104
            $donor->set('birthdate', filter_input(INPUT_POST, 'donor_birthdate'), true);
105
106
            // Set the donor blood group.
107
            $donor->set('blood_group', filter_input(INPUT_POST, 'donor_blood_group'), true);
108
109
            // Set the donor district ID.
110
            $donor->set('district', $this->getDistrictRepository()->find(filter_input(INPUT_POST, 'donor_district_id')));
111
112
            // Set the originator user.
113
            $donor->set('created_by', $this->getAuthenticatedUser());
114
115
            // Set the donor status.
116
            if ($this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'Donor', 'approve')) {
117
                $donor->set('status', 'approved');
118
            } else {
119
                $donor->set('status', 'pending');
120
            }
121
122
            // Set the donor weight.
123
            $donor->setMeta('weight', filter_input(INPUT_POST, 'donor_weight'), true);
124
125
            // Set the donor email address.
126
            $donor->setMeta('email', filter_input(INPUT_POST, 'donor_email'), true);
127
128
            // Set the donor email address visibility.
129
            $donor->setMeta('email_visibility', filter_input(INPUT_POST, 'donor_email_visibility'), true);
130
131
            // Set the donor phone number.
132
            $donor->setMeta('phone', filter_input(INPUT_POST, 'donor_phone'), true);
133
134
            // Set the donor phone number visibility.
135
            $donor->setMeta('phone_visibility', filter_input(INPUT_POST, 'donor_phone_visibility'), true);
136
137
            // Set the donor address.
138
            $donor->setMeta('address', filter_input(INPUT_POST, 'donor_address'), true);
139
140
            $this->getEntityManager()->persist($donor);
141
            $this->getEntityManager()->flush();
142
143
            $this->getEventManager()->getEventDispatcher()->dispatch('donor.created', new GenericEvent($donor));
144
145
            $added = $donor->isExists();
146
147
            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

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

148
                /** @scrutinizer ignore-call */ 
149
                EBB\addQueryArgs(
Loading history...
149
                    EBB\getAddDonorURL(),
0 ignored issues
show
Bug introduced by
The function getAddDonorURL 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

149
                    /** @scrutinizer ignore-call */ 
150
                    EBB\getAddDonorURL(),
Loading history...
150
                    ['flag-added' => $added]
151
                )
152
            );
153
        } catch (InvalidArgumentException $ex) {
154
            Notices::addNotice('invalid_donor_argument', $ex->getMessage());
155
        }
156
    }
157
}
158