ApproveDonor::doActions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Approve donor page controller class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Controllers
7
 * @since      1.0
8
 */
9
namespace EBloodBank\Controllers;
10
11
use EBloodBank as EBB;
12
use Psr\Container\ContainerInterface;
13
14
/**
15
 * Approve donor page controller class
16
 *
17
 * @since 1.0
18
 */
19
class ApproveDonor extends Controller
20
{
21
    /**
22
     * @var   int
23
     * @since 1.0
24
     */
25
    protected $donorId = 0;
26
27
    /**
28
     * @var   \EBloodBank\Models\Donor|null
29
     * @since 1.0
30
     */
31
    protected $donor;
32
33
    /**
34
     * @since 1.0
35
     */
36
    public function __construct(ContainerInterface $container, $donorId)
37
    {
38
        parent::__construct($container);
39
        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

39
        if (/** @scrutinizer ignore-call */ EBB\isValidID($donorId)) {
Loading history...
40
            $this->donorId = (int) $donorId;
41
        }
42
    }
43
44
    /**
45
     * @return void
46
     * @since  1.0
47
     */
48
    public function __invoke()
49
    {
50
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'Donor', 'approve')) {
51
            $this->viewFactory->displayView('error-403');
52
            return;
53
        }
54
55
        if ($this->donorId) {
56
            $this->donor = $this->getDonorRepository()->find($this->donorId);
57
        }
58
59
        if (! $this->donor) {
60
            $this->viewFactory->displayView('error-404');
61
            return;
62
        }
63
64
        $donor = $this->donor;
65
66
        if (! $this->getAcl()->canApproveDonor($this->getAuthenticatedUser(), $donor)) {
67
            $this->viewFactory->displayView('error-403');
68
            return;
69
        }
70
71
        $this->doActions();
72
        $this->viewFactory->displayView('approve-donor', [
73
            'donor' => $donor,
74
        ]);
75
    }
76
77
    /**
78
     * @return void
79
     * @since  1.0
80
     */
81
    protected function doActions()
82
    {
83
        switch (filter_input(INPUT_POST, 'action')) {
84
            case 'approve_donor':
85
                $this->doApproveAction();
86
                break;
87
        }
88
    }
89
90
    /**
91
     * @return void
92
     * @since  1.0
93
     */
94
    protected function doApproveAction()
95
    {
96
        $actionToken = filter_input(INPUT_POST, 'token');
97
        $sessionToken = $this->getSession()->getCsrfToken();
98
99
        if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
100
            return;
101
        }
102
103
        $donor = $this->donor;
104
105
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canApproveDonor($this->getAuthenticatedUser(), $donor)) {
0 ignored issues
show
Bug introduced by
It seems like $donor can also be of type null; however, parameter $donor of EBloodBank\AclInterface::canApproveDonor() does only seem to accept EBloodBank\Models\Donor, 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

105
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canApproveDonor($this->getAuthenticatedUser(), /** @scrutinizer ignore-type */ $donor)) {
Loading history...
106
            return;
107
        }
108
109
        if (! $donor->isPending()) {
0 ignored issues
show
Bug introduced by
The method isPending() 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

109
        if (! $donor->/** @scrutinizer ignore-call */ isPending()) {

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...
110
            return;
111
        }
112
113
        $donor->set('status', 'approved');
114
        $this->getEntityManager()->flush($donor);
115
116
        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

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

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

118
                /** @scrutinizer ignore-call */ 
119
                EBB\getEditDonorsURL(),
Loading history...
119
                ['flag-approved' => 1]
120
            )
121
        );
122
    }
123
}
124