DeleteCity   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A doActions() 0 6 2
A __invoke() 0 28 6
A doDeleteAction() 0 29 6
A __construct() 0 5 2
1
<?php
2
/**
3
 * Delete city 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 EBloodBank\Notices;
13
use Psr\Container\ContainerInterface;
14
15
/**
16
 * Delete city page controller class
17
 *
18
 * @since 1.0
19
 */
20
class DeleteCity extends Controller
21
{
22
    /**
23
     * @var   int
24
     * @since 1.6
25
     */
26
    protected $cityId = 0;
27
28
    /**
29
     * @var \EBloodBank\Models\City|null
30
     * @since 1.0
31
     */
32
    protected $city;
33
34
    /**
35
     * @return void
36
     * @since 1.0
37
     */
38
    public function __construct(ContainerInterface $container, $cityId)
39
    {
40
        parent::__construct($container);
41
        if (EBB\isValidID($cityId)) {
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

41
        if (/** @scrutinizer ignore-call */ EBB\isValidID($cityId)) {
Loading history...
42
            $this->cityId = $cityId;
43
        }
44
    }
45
46
    /**
47
     * @return void
48
     * @since 1.0
49
     */
50
    public function __invoke()
51
    {
52
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'City', 'delete')) {
53
            $this->viewFactory->displayView('error-403');
54
            return;
55
        }
56
57
        if ($this->cityId) {
58
            $this->city = $this->getCityRepository()->find($this->cityId);
59
        }
60
61
        if (! $this->city) {
62
            $this->viewFactory->displayView('error-404');
63
            return;
64
        }
65
66
        $city = $this->city;
67
68
        if (! $this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $city)) {
69
            $this->viewFactory->displayView('error-403');
70
            return;
71
        }
72
73
        $this->doActions();
74
        $this->viewFactory->displayView(
75
            'delete-city',
76
            [
77
                'city' => $city,
78
            ]
79
        );
80
    }
81
82
    /**
83
     * @return void
84
     * @since 1.0
85
     */
86
    protected function doActions()
87
    {
88
        switch (filter_input(INPUT_POST, 'action')) {
89
            case 'delete_city':
90
                $this->doDeleteAction();
91
                break;
92
        }
93
    }
94
95
    /**
96
     * @return void
97
     * @since 1.0
98
     */
99
    protected function doDeleteAction()
100
    {
101
        $sessionToken = $this->getSession()->getCsrfToken();
102
        $actionToken = filter_input(INPUT_POST, 'token');
103
104
        if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
105
            return;
106
        }
107
108
        $city = $this->city;
109
110
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $city)) {
0 ignored issues
show
Bug introduced by
It seems like $city 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

110
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), /** @scrutinizer ignore-type */ $city)) {
Loading history...
111
            return;
112
        }
113
114
        $districtsCount = $this->getDistrictRepository()->countBy(['city' => $city]);
115
116
        if ($districtsCount > 0) {
117
            Notices::addNotice('linked_districts_exists', __('At first, delete any linked districts with this city.'));
118
            return;
119
        }
120
121
        $this->getEntityManager()->remove($city);
122
        $this->getEntityManager()->flush();
123
124
        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

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

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

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