DeleteDistricts   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 18

3 Methods

Rating   Name   Duplication   Size   Complexity  
A doActions() 0 6 2
B doDeleteAction() 0 41 10
A __invoke() 0 19 6
1
<?php
2
/**
3
 * Delete districts 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
use EBloodBank\Notices;
13
use Psr\Container\ContainerInterface;
14
15
/**
16
 * Delete districts page controller class
17
 *
18
 * @since 1.1
19
 */
20
class DeleteDistricts extends Controller
21
{
22
    /**
23
     * @var \EBloodBank\Models\District[]
24
     * @since 1.1
25
     */
26
    protected $districts = [];
27
28
    /**
29
     * @return void
30
     * @since 1.1
31
     */
32
    public function __invoke()
33
    {
34
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'District', 'delete')) {
35
            $this->viewFactory->displayView('error-403');
36
            return;
37
        }
38
39
        if (filter_has_var(INPUT_POST, 'districts')) {
40
            $districtsIDs = filter_input(INPUT_POST, 'districts', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY);
41
            if (! empty($districtsIDs) && is_array($districtsIDs)) {
42
                $this->districts = $this->getDistrictRepository()->findBy(['id' => $districtsIDs]);
43
            }
44
        }
45
46
        $this->doActions();
47
        $this->viewFactory->displayView(
48
            'delete-districts',
49
            [
50
                'districts' => $this->districts,
51
            ]
52
        );
53
    }
54
55
    /**
56
     * @return void
57
     * @since 1.1
58
     */
59
    protected function doActions()
60
    {
61
        switch (filter_input(INPUT_POST, 'action')) {
62
            case 'delete_districts':
63
                $this->doDeleteAction();
64
                break;
65
        }
66
    }
67
68
    /**
69
     * @return void
70
     * @since 1.1
71
     */
72
    protected function doDeleteAction()
73
    {
74
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'District', 'delete')) {
75
            return;
76
        }
77
78
        $sessionToken = $this->getSession()->getCsrfToken();
79
        $actionToken = filter_input(INPUT_POST, 'token');
80
81
        if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
82
            return;
83
        }
84
85
        $districts = $this->districts;
86
87
        if (! $districts || ! is_array($districts)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $districts of type EBloodBank\Models\District[] 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...
88
            return;
89
        }
90
91
        $deletedDistrictsCount = 0;
92
93
        foreach ($districts as $district) {
94
            if ($this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $district)) {
95
                $donorsCount = $this->getDonorRepository()->countBy(['district' => $districts]);
96
97
                if ($donorsCount > 0) {
98
                    Notices::addNotice('linked_donors_exists', sprintf(__('At first, delete any linked donors with district "%s".'), $district->get('id')));
99
                    return;
100
                }
101
102
                $this->getEntityManager()->remove($district);
103
                $deletedDistrictsCount++;
104
            }
105
        }
106
107
        $this->getEntityManager()->flush();
108
109
        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

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

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

111
                /** @scrutinizer ignore-call */ 
112
                EBB\getEditDistrictsURL(),
Loading history...
112
                ['flag-deleted' => $deletedDistrictsCount]
113
            )
114
        );
115
    }
116
}
117