DeleteDistrict::doDeleteAction()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 4
nop 0
dl 0
loc 29
rs 9.0777
c 0
b 0
f 0
1
<?php
2
/**
3
 * Delete district 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
 * Delete city page controller class
16
 *
17
 * @since 1.0
18
 */
19
class DeleteDistrict extends Controller
20
{
21
    /**
22
     * @var   int
23
     * @since 1.6
24
     */
25
    protected $districtId = 0;
26
27
    /**
28
     * @var \EBloodBank\Models\District|null
29
     * @since 1.0
30
     */
31
    protected $district;
32
33
    /**
34
     * @since 1.0
35
     */
36
    public function __construct(ContainerInterface $container, $districtId)
37
    {
38
        parent::__construct($container);
39
        if (EBB\isValidID($districtId)) {
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($districtId)) {
Loading history...
40
            $this->districtId = (int) $districtId;
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(), 'District', 'delete')) {
51
            $this->viewFactory->displayView('error-403');
52
            return;
53
        }
54
55
        if ($this->districtId) {
56
            $this->district = $this->getDistrictRepository()->find($this->districtId);
57
        }
58
59
        if (! $this->district) {
60
            $this->viewFactory->displayView('error-404');
61
            return;
62
        }
63
64
        $district = $this->district;
65
66
        if (! $this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $district)) {
67
            $this->viewFactory->displayView('error-403');
68
            return;
69
        }
70
71
        $this->doActions();
72
        $this->viewFactory->displayView(
73
            'delete-district',
74
            [
75
                'district' => $district,
76
            ]
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 'delete_district':
88
                $this->doDeleteAction();
89
                break;
90
        }
91
    }
92
93
    /**
94
     * @return void
95
     * @since 1.0
96
     */
97
    protected function doDeleteAction()
98
    {
99
        $actionToken = filter_input(INPUT_POST, 'token');
100
        $sessionToken = $this->getSession()->getCsrfToken();
101
102
        if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
103
            return;
104
        }
105
106
        $district = $this->district;
107
108
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $district)) {
0 ignored issues
show
Bug introduced by
It seems like $district can also be of type null; however, parameter $resource of EBloodBank\AclInterface::canDeleteEntity() 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

108
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), /** @scrutinizer ignore-type */ $district)) {
Loading history...
109
            return;
110
        }
111
112
        $donorsCount = $this->getDonorRepository()->countBy(['district' => $district]);
113
114
        if ($donorsCount > 0) {
115
            Notices::addNotice('linked_donors_exists', __('At first, delete any linked donors with this district.'));
0 ignored issues
show
Bug introduced by
The type EBloodBank\Controllers\Notices was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
116
            return;
117
        }
118
119
        $this->getEntityManager()->remove($district);
120
        $this->getEntityManager()->flush();
121
122
        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

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

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

124
                /** @scrutinizer ignore-call */ 
125
                EBB\getEditDistrictsURL(),
Loading history...
125
                ['flag-deleted' => 1]
126
            )
127
        );
128
    }
129
}
130