DeleteDonors::__invoke()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 6
nop 0
dl 0
loc 18
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * Delete donors page controller class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Controllers
7
 * @since      1.1
8
 */
9
namespace EBloodBank\Controllers;
10
11
use EBloodBank as EBB;
12
13
/**
14
 * Delete donors page controller class
15
 *
16
 * @since 1.1
17
 */
18
class DeleteDonors extends Controller
19
{
20
    /**
21
     * @var \EBloodBank\Models\Donor[]
22
     * @since 1.1
23
     */
24
    protected $donors = [];
25
26
    /**
27
     * @return void
28
     * @since 1.1
29
     */
30
    public function __invoke()
31
    {
32
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'Donor', 'delete')) {
33
            $view = $this->viewFactory->forgeView('error-403');
0 ignored issues
show
Unused Code introduced by
The assignment to $view is dead and can be removed.
Loading history...
34
        }
35
36
        if (filter_has_var(INPUT_POST, 'donors')) {
37
            $donorsIDs = filter_input(INPUT_POST, 'donors', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY);
38
            if (! empty($donorsIDs) && is_array($donorsIDs)) {
39
                $this->donors = $this->getDonorRepository()->findBy(['id' => $donorsIDs]);
40
            }
41
        }
42
43
        $this->doActions();
44
        $this->viewFactory->displayView(
45
            'delete-donors',
46
            [
47
                'donors' => $this->donors,
48
            ]
49
        );
50
    }
51
52
    /**
53
     * @return void
54
     * @since 1.1
55
     */
56
    protected function doActions()
57
    {
58
        switch (filter_input(INPUT_POST, 'action')) {
59
            case 'delete_donors':
60
                $this->doDeleteAction();
61
                break;
62
        }
63
    }
64
65
    /**
66
     * @return void
67
     * @since 1.1
68
     */
69
    protected function doDeleteAction()
70
    {
71
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'Donor', 'delete')) {
72
            return;
73
        }
74
75
        $actionToken = filter_input(INPUT_POST, 'token');
76
        $sessionToken = $this->getSession()->getCsrfToken();
77
78
        if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
79
            return;
80
        }
81
82
        $donors = $this->donors;
83
84
        if (! $donors || ! is_array($donors)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $donors of type EBloodBank\Models\Donor[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
85
            return;
86
        }
87
88
        $deletedDonorsCount = 0;
89
90
        foreach ($donors as $donor) {
91
            if ($this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $donor)) {
92
                $this->getEntityManager()->remove($donor);
93
                $deletedDonorsCount++;
94
            }
95
        }
96
97
        $this->getEntityManager()->flush();
98
99
        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

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

100
            /** @scrutinizer ignore-call */ 
101
            EBB\addQueryArgs(
Loading history...
101
                EBB\getEditDonorsURL(),
0 ignored issues
show
Bug introduced by
The function getEditDonorsURL 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\getEditDonorsURL(),
Loading history...
102
                ['flag-deleted' => $deletedDonorsCount]
103
            )
104
        );
105
    }
106
}
107