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

ItemAddController::add()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 32
rs 9.408
c 0
b 0
f 0
1
<?php
2
3
namespace Rudolf\Modules\Appearance\Menu\Item;
4
5
use Rudolf\Component\Alerts\Alert;
6
use Rudolf\Component\Alerts\AlertsCollection;
7
use Rudolf\Framework\Controller\AdminController;
8
use Rudolf\Framework\Model\FrontModel;
9
use Rudolf\Modules\Appearance\Menu\Model;
10
11
class ItemAddController extends AdminController
12
{
13
    /**
14
     * @throws \Exception
15
     */
16
    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...
17
    {
18
        if (isset($_POST['add'])) {
19
            $model = new ItemAddModel();
20
            $id = $model->add($_POST);
21
            if ($id) {
22
                AlertsCollection::add(new Alert(
23
                    'success',
24
                    'Poprawnie dodano!'
25
                ));
26
                $this->redirectTo(DIR.'/admin/appearance/menu/edit/'.$id);
27
                return;
28
            }
29
            AlertsCollection::add(new Alert(
30
                'error',
31
                'Coś się zepsuło!'
32
            ));
33
        }
34
35
        $view = new ItemAddView();
36
        $view->display(new MenuItem([
37
            'id' => -1,
38
            'parent_id' => 0,
39
            'title' => '',
40
            'slug' => '',
41
            'caption' => '',
42
            'menu_type' => 'main',
43
            'item_type' => '',
44
            'position' => 0,
45
        ]), (new Model())->getTypes(), (new FrontModel())->getMenuItems());
46
        $view->render('admin');
47
    }
48
}
49