ApproveDonors   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 18

3 Methods

Rating   Name   Duplication   Size   Complexity  
A doActions() 0 6 2
B doApproveAction() 0 37 10
A __invoke() 0 19 6
1
<?php
2
/**
3
 * Approve 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
 * Approve donors page controller class
15
 *
16
 * @since 1.1
17
 */
18
class ApproveDonors 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', 'approve')) {
33
            $this->viewFactory->displayView('error-403');
34
            return;
35
        }
36
37
        if (filter_has_var(INPUT_POST, 'donors')) {
38
            $donorsIDs = filter_input(INPUT_POST, 'donors', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY);
39
            if (! empty($donorsIDs) && is_array($donorsIDs)) {
40
                $this->donors = $this->getDonorRepository()->findBy(['id' => $donorsIDs]);
41
            }
42
        }
43
44
        $this->doActions();
45
        $this->viewFactory->displayView(
46
            'approve-donors',
47
            [
48
                'donors' => $this->donors,
49
            ]
50
        );
51
    }
52
53
    /**
54
     * @return void
55
     * @since 1.1
56
     */
57
    protected function doActions()
58
    {
59
        switch (filter_input(INPUT_POST, 'action')) {
60
            case 'approve_donors':
61
                $this->doApproveAction();
62
                break;
63
        }
64
    }
65
66
    /**
67
     * @return void
68
     * @since 1.1
69
     */
70
    protected function doApproveAction()
71
    {
72
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'Donor', 'approve')) {
73
            return;
74
        }
75
76
        $actionToken = filter_input(INPUT_POST, 'token');
77
        $sessionToken = $this->getSession()->getCsrfToken();
78
79
        if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
80
            return;
81
        }
82
83
        $donors = $this->donors;
84
85
        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...
86
            return;
87
        }
88
89
        $approvedDonorsCount = 0;
90
91
        foreach ($donors as $donor) {
92
            if (! $donor->isPending()) {
93
                continue;
94
            }
95
            if ($this->getAcl()->canApproveDonor($this->getAuthenticatedUser(), $donor)) {
96
                $donor->set('status', 'approved');
97
                $approvedDonorsCount++;
98
            }
99
        }
100
101
        $this->getEntityManager()->flush();
102
103
        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

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

104
            /** @scrutinizer ignore-call */ 
105
            EBB\addQueryArgs(
Loading history...
105
                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

105
                /** @scrutinizer ignore-call */ 
106
                EBB\getEditDonorsURL(),
Loading history...
106
                ['flag-approved' => $approvedDonorsCount]
107
            )
108
        );
109
    }
110
}
111