Completed
Push — master ( d7cd82...c94b19 )
by Mikołaj
04:50
created

TypeAddController::add()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 36
rs 9.344
c 0
b 0
f 0
1
<?php
2
3
namespace Rudolf\Modules\Appearance\Menu\Type;
4
5
use Rudolf\Component\Alerts\Alert;
6
use Rudolf\Component\Alerts\AlertsCollection;
7
use Rudolf\Framework\Controller\AdminController;
8
9
class TypeAddController 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
            $model = new TypeAddModel();
18
            $id = $model->add($_POST);
19
            if ($id) {
20
                AlertsCollection::add(new Alert(
21
                    'success',
22
                    'Poprawnie dodano!'
23
                ));
24
                $this->redirectTo(DIR.'/admin/appearance/menu/edit-type/'.$id);
25
                return;
26
            }
27
            AlertsCollection::add(new Alert(
28
                'error',
29
                'Coś się zepsuło!'
30
            ));
31
            $item = new MenuType([
32
                'id' => -1,
33
                'title' => $_POST['title'],
34
                'menu_type' => $_POST['menu_type'],
35
                'description' => $_POST['description'],
36
            ]);
37
        } else {
38
            $item = new MenuType([
39
                'id' => -1,
40
                'title' => '',
41
                'menu_type' => '',
42
                'description' => ''
43
            ]);
44
        }
45
46
        $view = new TypeAddView();
47
        $view->display($item);
48
        $view->render('admin');
49
    }
50
}
51