AdminController::indexAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2011 - 2014 Aleksandr Torosh (http://wezoom.net)
4
 * @author Aleksandr Torosh <[email protected]>
5
 */
6
7
namespace Tree\Controller;
8
9
use Application\Localization\Transliterator;
10
use Application\Mvc\Controller;
11
use Tree\Form\CategoryForm;
12
use Tree\Model\Category;
13
14
class AdminController extends Controller
15
{
16
17
    public function initialize()
18
    {
19
        $this->helper->activeMenu()->setActive('tree');
20
    }
21
22
    public function indexAction()
23
    {
24
        $this->setAdminEnvironment();
25
        $this->view->roots = Category::$roots;
0 ignored issues
show
Bug introduced by
Accessing roots on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
26
27
        $assets = $this->getDI()->get('assets');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDI() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
28
        $assets->collection('modules-admin-less')->addCss(__DIR__ . '/../assets/tree.less');
29
        $assets->collection('modules-admin-js')->addJs(__DIR__ . '/../assets/tree.js');
30
31
        $this->helper->title($this->helper->at('Tree Categories'), true);
32
    }
33
34
    public function addAction()
35
    {
36
        if (!$this->request->getPost() || !$this->request->isAjax()) {
37
            return $this->flash->error('post ajax required');
38
        }
39
40
        $root = $this->request->getPost('root');
41
        $title = $this->request->getPost('title', 'string');
42
43
        $model = new Category();
44
        $model->setRoot($root);
45
        if ($model->create()) {
46
            $model->setTitle($title);
47
            $model->setSlug(Transliterator::slugify($title));
48
            if ($model->update()) {
49
                $this->returnJSON([
50
                    'success' => true,
51
                    'id' => $model->getId(),
52
                    'slug' => $model->getSlug(),
53
                    'title' => $title,
54
                ]);
55
            } else {
56
                $this->returnJSON(['error' => implode(' | ', $model->getMessages())]);
57
            }
58
        } else {
59
            $this->returnJSON(['error' => implode(' | ', $model->getMessages())]);
60
        }
61
    }
62
63
    public function editAction($id)
64
    {
65
        $this->setAdminEnvironment();
66
67
        $form = new CategoryForm();
68
        $model = Category::findFirst($id);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as \Tree\Model\Category::findFirst($id) (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
69
        if (!$model) {
70
            $this->redirect($this->url->get() . 'tree/admin?lang=' . LANG);
71
        }
72
73
        if ($this->request->isPost()) {
74
            $form->bind($this->request->getPost(), $model);
75
            if ($form->isValid()) {
76
                if ($model->save()) {
0 ignored issues
show
Bug introduced by
The method save cannot be called on $model (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
77
                    $this->flash->success($this->helper->at('Updated has been successful'));
78
                    $this->redirect($this->url->get() . 'tree/admin?lang=' . LANG);
79
                } else {
80
                    $this->flashErrors($model);
81
                }
82
            } else {
83
                $this->flashErrors($form);
84
            }
85
        } else {
86
            $form->setEntity($model);
87
        }
88
89
        $this->helper->title($this->helper->at('Edit Category'), true);
90
91
        $this->view->form = $form;
0 ignored issues
show
Bug introduced by
Accessing form on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
92
        $this->view->model = $model;
0 ignored issues
show
Bug introduced by
Accessing model on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
93
    }
94
95
    public function deleteAction()
96
    {
97
        if (!$this->request->getPost() || !$this->request->isAjax()) {
98
            return $this->flash->error('post ajax required');
99
        }
100
101
        $category_id = $this->request->getPost('category_id');
102
103
        $model = Category::findFirst($category_id);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as \Tree\Model\Category::findFirst($category_id) (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
104
        if ($model) {
105
            if ($model->delete()) {
106
                $this->returnJSON([
107
                    'success' => true,
108
                    'root' => $model->getRoot(),
109
                ]);
110
            }
111
        }
112
    }
113
114
    public function saveTreeAction()
115
    {
116
        if (!$this->request->getPost() || !$this->request->isAjax()) {
117
            return $this->flash->error('post ajax required');
118
        }
119
120
        $data = $this->request->getPost('data');
121
122
        foreach ($data as $el) {
123
            if ($el['item_id']) {
124
                $model = Category::findFirst($el['item_id']);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as \Tree\Model\Category::findFirst($el['item_id']) (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
125
                if ($model) {
126
                    if ($el['parent_id']) {
127
                        $model->setParentId($el['parent_id']);
128
                    } else {
129
                        $model->setParentId(null);
130
                    }
131
                    $model->setDepth($el['depth']);
132
                    $model->setLeftKey($el['left']);
133
                    $model->setRightKey($el['right']);
134
                    $model->update();
135
                }
136
            }
137
        }
138
139
        $this->returnJSON(['success' => true]);
140
141
    }
142
143
}