Completed
Push — master ( d015d4...c50906 )
by Mikołaj
02:42
created

EditController::edit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 8
Ratio 34.78 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
c 0
b 0
f 0
nc 3
nop 1
dl 8
loc 23
rs 9.0856
1
<?php
2
3
namespace Rudolf\Modules\Appearance\Menu;
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
10
class EditController extends AdminController
11
{
12
    /**
13
     * @param $id
14
     *
15
     * @throws \Exception
16
     */
17
    public function edit($id)
0 ignored issues
show
Coding Style introduced by
edit 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...
18
    {
19
        $model = new EditModel();
20
        $view = new EditView();
21
22
        if (isset($_POST['update'])) {
23 View Code Duplication
            if ($model->edit($id, $_POST)) {
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...
24
                AlertsCollection::add(new Alert(
25
                    'success',
26
                    'Zaktualizowano!'
27
                ));
28
                $this->redirectTo(DIR.'/admin/appearance/menu/edit/'.$id);
29
                return;
30
            }
31
            AlertsCollection::add(new Alert(
32
                'error',
33
                'Coś się zepsuło!'
34
            ));
35
        }
36
37
        $view->display($model->getInfo($id), (new Model())->getTypes(), (new FrontModel())->getMenuItems());
38
        $view->render('admin');
39
    }
40
}
41