AddCity   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 100
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A doActions() 0 6 2
A __invoke() 0 15 3
B doSubmitAction() 0 41 7
A addNotices() 0 4 2
1
<?php
2
/**
3
 * Add 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 EBloodBank\Models\City;
14
use InvalidArgumentException;
15
16
/**
17
 * Add city page controller class
18
 *
19
 * @since 1.0
20
 */
21
class AddCity extends Controller
22
{
23
    /**
24
     * @var \EBloodBank\Models\City
25
     * @since 1.0
26
     */
27
    protected $city;
28
29
    /**
30
     * @return void
31
     * @since 1.0
32
     */
33
    public function __invoke()
34
    {
35
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'City', 'add')) {
36
            $this->viewFactory->displayView('error-403');
37
            return;
38
        }
39
40
        $this->city = new City();
41
42
        $this->doActions();
43
        $this->addNotices();
44
        $this->viewFactory->displayView(
45
            'add-city',
46
            [
47
                'city' => $this->city,
48
            ]
49
        );
50
    }
51
52
    /**
53
     * @return void
54
     * @since 1.0
55
     */
56
    protected function doActions()
57
    {
58
        switch (filter_input(INPUT_POST, 'action')) {
59
            case 'submit_city':
60
                $this->doSubmitAction();
61
                break;
62
        }
63
    }
64
65
    /**
66
     * @return void
67
     * @since 1.0
68
     */
69
    protected function addNotices()
70
    {
71
        if (filter_has_var(INPUT_GET, 'flag-added')) {
72
            Notices::addNotice('added', __('City added.'), 'success');
73
        }
74
    }
75
76
    /**
77
     * @return void
78
     * @since 1.0
79
     */
80
    protected function doSubmitAction()
81
    {
82
        try {
83
            if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'City', 'add')) {
84
                return;
85
            }
86
87
            $actionToken = filter_input(INPUT_POST, 'token');
88
            $sessionToken = $this->getSession()->getCsrfToken();
89
90
            if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
91
                return;
92
            }
93
94
            $city = $this->city;
95
96
            // Set the city name.
97
            $city->set('name', filter_input(INPUT_POST, 'city_name'), true);
98
99
            $duplicateCity = $this->getCityRepository()->findOneBy(['name' => $city->get('name')]);
100
101
            if (! empty($duplicateCity)) {
102
                throw new InvalidArgumentException(__('Please enter a unique city name.'));
103
            }
104
105
            // Set the originator user.
106
            $city->set('created_by', $this->getAuthenticatedUser());
107
108
            $this->getEntityManager()->persist($city);
109
            $this->getEntityManager()->flush();
110
111
            $added = $city->isExists();
112
113
            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

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

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

115
                    /** @scrutinizer ignore-call */ 
116
                    EBB\getAddCityURL(),
Loading history...
116
                    ['flag-added' => $added]
117
                )
118
            );
119
        } catch (InvalidArgumentException $ex) {
120
            Notices::addNotice('invalid_city_argument', $ex->getMessage());
121
        }
122
    }
123
}
124