Menu::beforeUpdate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2011 - 2015 Oleksandr Torosh (http://yonastudio.com)
4
 * @author Oleksandr Torosh <[email protected]>
5
 */
6
namespace Menu\Model\Menu;
7
8
use Application\Mvc\Model\Model;
9
use Phalcon\Mvc\Model\Validator\Uniqueness;
10
11
class Menu extends Model
12
{
13
14
    public function getSource()
15
    {
16
        return "menu";
17
    }
18
19
    protected $translateModel = 'Menu\Model\Translate\MenuTranslate'; // translate
20
21
    private $id;
22
    private $root = 'top';
23
    private $parent_id;
24
    private $work_title;
25
    private $depth = 0;
26
    private $left_key;
27
    private $right_key;
28
    private $created_at;
29
    private $updated_at;
30
    public  $title; // translate
31
32
    public static $roots = [
33
        'top' => 'Top Menu',
34
    ];
35
36
    public function initialize()
37
    {
38
        $this->belongsTo('parent_id', 'Category\Model\Category', 'id', ['alias' => 'Parent']);
39
        $this->hasMany("id", $this->translateModel, "foreign_id"); // translate
40
    }
41
42
    public function validation()
43
    {
44
        $this->validate(new Uniqueness(
0 ignored issues
show
Documentation introduced by
new \Phalcon\Mvc\Model\V.... Take another title')) is of type object<Phalcon\Mvc\Model\Validator\Uniqueness>, but the function expects a object<Phalcon\ValidationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The class Phalcon\Mvc\Model\Validator\Uniqueness has been deprecated with message: 3.1.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
45
            [
46
                "field"   => "slug",
47
                "message" => "Category with slug '" . $this->slug . "' is already exists. Take another title"
0 ignored issues
show
Documentation introduced by
The property slug does not exist on object<Menu\Model\Menu\Menu>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
48
            ]
49
        ));
50
51
        return $this->validationHasFailed() != true;
52
    }
53
54
    public function beforeCreate()
55
    {
56
        $this->created_at = date("Y-m-d H:i:s");
57
    }
58
59
    public function beforeUpdate()
60
    {
61
        $this->updated_at = date("Y-m-d H:i:s");
62
    }
63
64
    public static function treeUpperLeafs($root)
65
    {
66
        $entries = Menu::find([
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $entries is correct as \Menu\Model\Menu\Menu::f...rray('root' => $root))) (which targets Phalcon\Mvc\Model::find()) 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...
67
            'root = :root: AND parent_id IS NULL',
68
            'order' => 'left_key',
69
            'bind'  => ['root' => $root]
70
        ]);
71
        return $entries;
72
    }
73
74 View Code Duplication
    public function children()
75
    {
76
        $entries = $this->find([
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $entries is correct as $this->find(array('left_...$this->getRightKey()))) (which targets Phalcon\Mvc\Model::find()) 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...
77
            'left_key >= :left_key: AND right_key <= :right_key: AND depth = :depth_plus: AND id <> :id: AND root = :root:',
78
            'order' => 'left_key ASC',
79
            'bind'  => [
80
                'id'          => $this->getId(),
81
                'root'        => $this->getRoot(),
82
                'depth_plus' => $this->getDepth() + 1,
83
                'left_key'    => $this->getLeftKey(),
84
                'right_key'   => $this->getRightKey(),
85
            ]
86
        ]);
87
        return $entries;
88
    }
89
90
    public function hasChildren()
91
    {
92
        if (abs($this->getRightKey() - $this->getLeftKey()) > 1) {
93
            return true;
94
        }
95
    }
96
97
    /**
98
     * @return mixed
99
     */
100
    public function getId()
101
    {
102
        return $this->id;
103
    }
104
105
    /**
106
     * @param mixed $id
107
     */
108
    public function setId($id)
109
    {
110
        $this->id = $id;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getRoot()
117
    {
118
        return $this->root;
119
    }
120
121
    /**
122
     * @param string $root
123
     */
124
    public function setRoot($root)
125
    {
126
        $this->root = $root;
127
    }
128
129
    /**
130
     * @return mixed
131
     */
132
    public function getParentId()
133
    {
134
        return $this->parent_id;
135
    }
136
137
    /**
138
     * @param mixed $parent_id
139
     */
140
    public function setParentId($parent_id)
141
    {
142
        $this->parent_id = $parent_id;
143
    }
144
145
    /**
146
     * @return mixed
147
     */
148
    public function getDepth()
149
    {
150
        return $this->depth;
151
    }
152
153
    /**
154
     * @param mixed $depth
155
     */
156
    public function setDepth($depth)
157
    {
158
        $this->depth = $depth;
159
    }
160
161
    /**
162
     * @return mixed
163
     */
164
    public function getLeftKey()
165
    {
166
        return $this->left_key;
167
    }
168
169
    /**
170
     * @param mixed $left_key
171
     */
172
    public function setLeftKey($left_key)
173
    {
174
        $this->left_key = $left_key;
175
    }
176
177
    /**
178
     * @return mixed
179
     */
180
    public function getRightKey()
181
    {
182
        return $this->right_key;
183
    }
184
185
    /**
186
     * @param mixed $right_key
187
     */
188
    public function setRightKey($right_key)
189
    {
190
        $this->right_key = $right_key;
191
    }
192
193
    /**
194
     * @return mixed
195
     */
196
    public function getTitle()
197
    {
198
        return $this->getMLVariable('title');
199
    }
200
201
    /**
202
     * @param mixed $title
203
     */
204
    public function setTitle($title)
205
    {
206
        $this->setMLVariable('title', $title);
207
    }
208
209
    /**
210
     * @return mixed
211
     */
212
    public function getCreatedAt()
213
    {
214
        return $this->created_at;
215
    }
216
217
    /**
218
     * @param mixed $created_at
219
     */
220
    public function setCreatedAt($created_at)
221
    {
222
        $this->created_at = $created_at;
223
    }
224
225
    /**
226
     * @return mixed
227
     */
228
    public function getUpdatedAt()
229
    {
230
        return $this->updated_at;
231
    }
232
233
    /**
234
     * @param mixed $updated_at
235
     */
236
    public function setUpdatedAt($updated_at)
237
    {
238
        $this->updated_at = $updated_at;
239
    }
240
241
    /**
242
     * @return mixed
243
     */
244
    public function getWorkTitle()
245
    {
246
        return $this->work_title;
247
    }
248
249
    /**
250
     * @param mixed $work_title
251
     */
252
    public function setWorkTitle($work_title)
253
    {
254
        $this->work_title = $work_title;
255
    }
256
257
}