Issues (294)

src/EBloodBank/Controllers/EditCity.php (7 issues)

1
<?php
2
/**
3
 * Edit city page controller class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Controllers
7
 * @since      1.0
8
 */
9
namespace EBloodBank\Controllers;
10
11
use InvalidArgumentException;
12
use EBloodBank as EBB;
13
use EBloodBank\Notices;
14
use Psr\Container\ContainerInterface;
15
16
/**
17
 * Edit city page controller class
18
 *
19
 * @since 1.0
20
 */
21
class EditCity extends Controller
22
{
23
    /**
24
     * @var   int
25
     * @since 1.6
26
     */
27
    protected $cityId = 0;
28
29
    /**
30
     * @var \EBloodBank\Models\City|null
31
     * @since 1.0
32
     */
33
    protected $city;
34
35
    /**
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
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', 'edit')) {
53
            $this->viewFactory->displayView('error-403');
54
            return;
55
        }
56
57
        if (EBB\isValidID($this->cityId)) {
0 ignored issues
show
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

57
        if (/** @scrutinizer ignore-call */ EBB\isValidID($this->cityId)) {
Loading history...
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()->canEditEntity($this->getAuthenticatedUser(), $city)) {
69
            $this->viewFactory->displayView('error-403');
70
            return;
71
        }
72
73
        $this->doActions();
74
        $this->addNotices();
75
        $this->viewFactory->displayView(
76
            'edit-city',
77
            [
78
                'city' => $city,
79
            ]
80
        );
81
    }
82
83
    /**
84
     * @return void
85
     * @since 1.0
86
     */
87
    protected function doActions()
88
    {
89
        switch (filter_input(INPUT_POST, 'action')) {
90
            case 'submit_city':
91
                $this->doSubmitAction();
92
                break;
93
        }
94
    }
95
96
    /**
97
     * @return void
98
     * @since 1.0
99
     */
100
    protected function addNotices()
101
    {
102
        if (filter_has_var(INPUT_GET, 'flag-edited')) {
103
            Notices::addNotice('edited', __('City edited.'), 'success');
104
        }
105
    }
106
107
    /**
108
     * @return void
109
     * @since 1.0
110
     */
111
    protected function doSubmitAction()
112
    {
113
        try {
114
            $sessionToken = $this->getSession()->getCsrfToken();
115
            $actionToken = filter_input(INPUT_POST, 'token');
116
117
            if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
118
                return;
119
            }
120
121
            $city = $this->city;
122
123
            if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canEditEntity($this->getAuthenticatedUser(), $city)) {
0 ignored issues
show
It seems like $city can also be of type null; however, parameter $resource of EBloodBank\AclInterface::canEditEntity() 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

123
            if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->canEditEntity($this->getAuthenticatedUser(), /** @scrutinizer ignore-type */ $city)) {
Loading history...
124
                return;
125
            }
126
127
            // Set the city name.
128
            $city->set('name', filter_input(INPUT_POST, 'city_name'), true);
0 ignored issues
show
The method set() 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

128
            $city->/** @scrutinizer ignore-call */ 
129
                   set('name', filter_input(INPUT_POST, 'city_name'), true);

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...
129
130
            $duplicateCity = $this->getCityRepository()->findOneBy(['name' => $city->get('name')]);
131
132
            if (! empty($duplicateCity) && $duplicateCity->get('id') != $city->get('id')) {
133
                throw new InvalidArgumentException(__('Please enter a unique city name.'));
134
            }
135
136
            $this->getEntityManager()->flush($city);
137
138
            EBB\redirect(
0 ignored issues
show
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

138
            /** @scrutinizer ignore-call */ 
139
            EBB\redirect(
Loading history...
139
                EBB\addQueryArgs(
0 ignored issues
show
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

139
                /** @scrutinizer ignore-call */ 
140
                EBB\addQueryArgs(
Loading history...
140
                    EBB\getEditCityURL($city->get('id')),
0 ignored issues
show
The function getEditCityURL 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

140
                    /** @scrutinizer ignore-call */ 
141
                    EBB\getEditCityURL($city->get('id')),
Loading history...
141
                    ['flag-edited' => true]
142
                )
143
            );
144
        } catch (InvalidArgumentException $ex) {
145
            Notices::addNotice('invalid_city_argument', $ex->getMessage());
146
        }
147
    }
148
}
149