Completed
Push — master ( cceadc...3df73a )
by Mikołaj
04:41
created

AddController::add()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 35

Duplication

Lines 9
Ratio 25.71 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 0
dl 9
loc 35
rs 9.0488
c 0
b 0
f 0
1
<?php
2
3
namespace Rudolf\Modules\Users\One\Admin\Profile;
4
5
use Rudolf\Component\Alerts\Alert;
6
use Rudolf\Component\Alerts\AlertsCollection;
7
use Rudolf\Framework\Controller\AdminController;
8
9
class AddController extends AdminController
10
{
11
    /**
12
     * @throws \Exception
13
     */
14
    public function add()
0 ignored issues
show
Coding Style introduced by
add uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
15
    {
16
        if (isset($_POST['add'])) {
17
            if (empty($_POST['password']) || trim($_POST['password']) !== $_POST['password_again']) {
18
                AlertsCollection::add(
19
                    new Alert(
20
                        'error',
21
                        'Podane hasła nie są identyczne!'
22
                    )
23
                );
24
                $this->redirect(DIR.'/admin/users/add');
25
            }
26
27
            $id = (new AddModel())->add($_POST);
28
29 View Code Duplication
            if ($id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
                AlertsCollection::add(
31
                    new Alert(
32
                        'success',
33
                        'Dodano użytkownika'
34
                    )
35
                );
36
                $this->redirect(DIR.'/admin/users/edit/'.$id);
37
            }
38
            AlertsCollection::add(
39
                new Alert(
40
                    'error',
41
                    'Wystąpił nieoczekiwany błąd'
42
                )
43
            );
44
        }
45
        $view = new AddView();
46
        $view->display();
47
        $view->render();
48
    }
49
}
50