EditDonor   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 154
rs 10
c 0
b 0
f 0
wmc 22

5 Methods

Rating   Name   Duplication   Size   Complexity  
A doActions() 0 6 2
A addNotices() 0 7 4
B doSubmitAction() 0 64 8
A __construct() 0 5 2
A __invoke() 0 27 6
1
<?php
2
/**
3
 * Edit 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 Psr\Container\ContainerInterface;
15
16
/**
17
 * Edit donor page controller class
18
 *
19
 * @since 1.0
20
 */
21
class EditDonor extends Controller
22
{
23
    /**
24
     * @var   int
25
     * @since 1.6
26
     */
27
    protected $donorId = 0;
28
29
    /**
30
     * @var \EBloodBank\Models\Donor|null
31
     * @since 1.0
32
     */
33
    protected $donor;
34
35
    /**
36
     * @since 1.0
37
     */
38
    public function __construct(ContainerInterface $container, $donorId)
39
    {
40
        parent::__construct($container);
41
        if (EBB\isValidID($donorId)) {
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

41
        if (/** @scrutinizer ignore-call */ EBB\isValidID($donorId)) {
Loading history...
42
            $this->donorId = (int) $donorId;
43
        }
44
    }
45
46
    /**
47
     * @return void
48
     * @since 1.0
49
     */
50
    public function __invoke()
51
    {
52
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'Donor', 'edit')) {
53
            $this->viewFactory->displayView('error-403');
54
            return;
55
        }
56
57
        if ($this->donorId) {
58
            $this->donor = $this->getDonorRepository()->find($this->donorId);
59
        }
60
61
        if (! $this->donor) {
62
            $this->viewFactory->displayView('error-404');
63
            return;
64
        }
65
66
        $donor = $this->donor;
67
68
        if (! $this->getAcl()->canEditEntity($this->getAuthenticatedUser(), $donor)) {
69
            $this->viewFactory->displayView('error-403');
70
            return;
71
        }
72
73
        $this->doActions();
74
        $this->addNotices();
75
        $this->viewFactory->displayView('edit-donor', [
76
            'donor' => $donor,
77
        ]);
78
    }
79
80
    /**
81
     * @return void
82
     * @since 1.0
83
     */
84
    protected function doActions()
85
    {
86
        switch (filter_input(INPUT_POST, 'action')) {
87
            case 'submit_donor':
88
                $this->doSubmitAction();
89
                break;
90
        }
91
    }
92
93
    /**
94
     * @return void
95
     * @since 1.0
96
     */
97
    protected function addNotices()
98
    {
99
        if (filter_has_var(INPUT_GET, 'flag-edited')) {
100
            Notices::addNotice('edited', __('Donor edited.'), 'success');
101
        }
102
        if ($this->donor && $this->donor->isPending()) {
103
            Notices::addNotice('pending', __('This donor is pendng moderation.'), 'warning');
104
        }
105
    }
106
107
    /**
108
     * @return void
109
     * @since 1.0
110
     */
111
    protected function doSubmitAction()
112
    {
113
        try {
114
            $sessionToken = $this->getSession()->getCsrfToken();
115
            $actionToken = filter_input(INPUT_POST, 'token');
116
117
            if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
118
                return;
119
            }
120
121
            $donor = $this->donor;
122
123
            if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canEditEntity($this->getAuthenticatedUser(), $donor)) {
0 ignored issues
show
Bug introduced by
It seems like $donor can also be of type null; however, parameter $resource of EBloodBank\AclInterface::canEditEntity() does only seem to accept EBloodBank\Models\Entity, 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

123
            if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canEditEntity($this->getAuthenticatedUser(), /** @scrutinizer ignore-type */ $donor)) {
Loading history...
124
                return;
125
            }
126
127
            // Set the donor name.
128
            $donor->set('name', filter_input(INPUT_POST, 'donor_name'), true);
0 ignored issues
show
Bug introduced by
The method set() 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

128
            $donor->/** @scrutinizer ignore-call */ 
129
                    set('name', filter_input(INPUT_POST, 'donor_name'), true);

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...
129
130
            // Set the donor gender.
131
            $donor->set('gender', filter_input(INPUT_POST, 'donor_gender'), true);
132
133
            // Set the donor birthdate.
134
            $donor->set('birthdate', filter_input(INPUT_POST, 'donor_birthdate'), true);
135
136
            // Set the donor blood group.
137
            $donor->set('blood_group', filter_input(INPUT_POST, 'donor_blood_group'), true);
138
139
            // Set the donor district ID.
140
            $donor->set('district', $this->getDistrictRepository()->find(filter_input(INPUT_POST, 'donor_district_id')));
141
142
            // Set the donor weight.
143
            $donor->setMeta('weight', filter_input(INPUT_POST, 'donor_weight'), true);
144
145
            // Set the donor email address.
146
            $donor->setMeta('email', filter_input(INPUT_POST, 'donor_email'), true);
147
148
            // Set the donor email address visibility.
149
            $donor->setMeta('email_visibility', filter_input(INPUT_POST, 'donor_email_visibility'), true);
150
151
            // Set the donor phone number.
152
            $donor->setMeta('phone', filter_input(INPUT_POST, 'donor_phone'), true);
153
154
            // Set the donor phone number visibility.
155
            $donor->setMeta('phone_visibility', filter_input(INPUT_POST, 'donor_phone_visibility'), true);
156
157
            // Set the donor address.
158
            $donor->setMeta('address', filter_input(INPUT_POST, 'donor_address'), true);
159
160
            // Set the donor status.
161
            if ($donor->isApproved() && ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'Donor', 'approve')) {
162
                $donor->set('status', 'pending');
163
            }
164
165
            $this->getEntityManager()->flush($donor);
166
167
            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

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

168
                /** @scrutinizer ignore-call */ 
169
                EBB\addQueryArgs(
Loading history...
169
                    EBB\getEditDonorURL($donor->get('id')),
0 ignored issues
show
Bug introduced by
The function getEditDonorURL 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

169
                    /** @scrutinizer ignore-call */ 
170
                    EBB\getEditDonorURL($donor->get('id')),
Loading history...
170
                    ['flag-edited' => true]
171
                )
172
            );
173
        } catch (InvalidArgumentException $ex) {
174
            Notices::addNotice('invalid_donor_argument', $ex->getMessage());
175
        }
176
    }
177
}
178