AddDistrict   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addNotices() 0 4 2
B doSubmitAction() 0 38 6
A doActions() 0 6 2
A __invoke() 0 15 3
1
<?php
2
/**
3
 * Add district 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 EBloodBank\Models\District;
15
16
/**
17
 * Add district page controller class
18
 *
19
 * @since 1.0
20
 */
21
class AddDistrict extends Controller
22
{
23
    /**
24
     * @var \EBloodBank\Models\District
25
     * @since 1.0
26
     */
27
    protected $district;
28
29
    /**
30
     * @return void
31
     * @since 1.0
32
     */
33
    public function __invoke()
34
    {
35
        if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'District', 'add')) {
36
            $this->viewFactory->displayView('error-403');
37
            return;
38
        }
39
40
        $this->district = new District();
41
42
        $this->doActions();
43
        $this->addNotices();
44
        $this->viewFactory->displayView(
45
            'add-district',
46
            [
47
                'district' => $this->district,
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_district':
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', __('District 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(), 'District', 'add')) {
84
                return;
85
            }
86
87
            $sessionToken = $this->getSession()->getCsrfToken();
88
            $actionToken = filter_input(INPUT_POST, 'token');
89
90
            if (! $actionToken || ! $sessionToken->isValid($actionToken)) {
91
                return;
92
            }
93
94
            $district = $this->district;
95
96
            // Set the district name.
97
            $district->set('name', filter_input(INPUT_POST, 'district_name'), true);
98
99
            // Set the district city ID.
100
            $district->set('city', $this->getCityRepository()->find(filter_input(INPUT_POST, 'district_city_id')));
101
102
            // Set the originator user.
103
            $district->set('created_by', $this->getAuthenticatedUser());
104
105
            $this->getEntityManager()->persist($district);
106
            $this->getEntityManager()->flush();
107
108
            $added = $district->isExists();
109
110
            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

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

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

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