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

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

109
            /** @scrutinizer ignore-call */ 
110
            EBB\addQueryArgs(
Loading history...
110
                EBB\getEditCitiesURL(),
0 ignored issues
show
Bug introduced by
The function getEditCitiesURL 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\getEditCitiesURL(),
Loading history...
111
                ['flag-deleted' => $deletedCitiesCount]
112
            )
113
        );
114
    }
115
}
116