Category::children()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.7666
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
7
namespace Tree\Model;
8
9
use Phalcon\Validation;
10
use Application\Mvc\Model\Model;
11
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;
12
13
class Category extends Model
14
{
15
16
    public function getSource()
17
    {
18
        return "tree_category";
19
    }
20
21
    protected $translateModel = 'Tree\Model\Translate\CategoryTranslate'; // translate
22
23
    private $id;
24
    private $root = 'articles';
25
    private $parent_id;
26
    private $slug;
27
    private $depth = 0;
28
    private $left_key;
29
    private $right_key;
30
    private $created_at;
31
    private $updated_at;
32
    public  $title; // translate
33
34
    public static $roots = [
35
        'articles' => 'Articles',
36
        'news'     => 'News'
37
    ];
38
39
    public function initialize()
40
    {
41
        $this->belongsTo('parent_id', 'Category\Model\Category', 'id', ['alias' => 'Parent']);
42
        $this->hasMany("id", $this->translateModel, "foreign_id"); // translate
43
    }
44
45 View Code Duplication
    public function validation()
46
    {
47
        $validator = new Validation();
48
        $validator->add('slug', new UniquenessValidator(
49
           [
50
               "model"   => $this,
51
               "message" => "Category with slug '" . $this->slug . "' is already exists. Take another title"
52
           ]
53
        ));
54
        return $this->validate($validator);
55
    }
56
57
    public function beforeCreate()
58
    {
59
        $this->created_at = date("Y-m-d H:i:s");
60
    }
61
62
    public function beforeUpdate()
63
    {
64
        $this->updated_at = date("Y-m-d H:i:s");
65
    }
66
67
    public static function treeUpperLeafs($root)
68
    {
69
        $entries = Category::find([
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $entries is correct as \Tree\Model\Category::fi...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...
70
            'root = :root: AND parent_id IS NULL',
71
            'order' => 'left_key',
72
            'bind'  => ['root' => $root]
73
        ]);
74
        return $entries;
75
    }
76
77 View Code Duplication
    public function children()
78
    {
79
        $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...
80
            'left_key >= :left_key: AND right_key <= :right_key: AND depth = :depth_plus: AND id <> :id: AND root = :root:',
81
            'order' => 'left_key ASC',
82
            'bind'  => [
83
                'id'          => $this->getId(),
84
                'root'        => $this->getRoot(),
85
                'depth_plus' => $this->getDepth() + 1,
86
                'left_key'    => $this->getLeftKey(),
87
                'right_key'   => $this->getRightKey(),
88
            ]
89
        ]);
90
        return $entries;
91
    }
92
93
    public function hasChildren()
94
    {
95
        if (abs($this->getRightKey() - $this->getLeftKey()) > 1) {
96
            return true;
97
        }
98
    }
99
100
    /**
101
     * @return mixed
102
     */
103
    public function getId()
104
    {
105
        return $this->id;
106
    }
107
108
    /**
109
     * @param mixed $id
110
     */
111
    public function setId($id)
112
    {
113
        $this->id = $id;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getRoot()
120
    {
121
        return $this->root;
122
    }
123
124
    /**
125
     * @param string $root
126
     */
127
    public function setRoot($root)
128
    {
129
        $this->root = $root;
130
    }
131
132
    /**
133
     * @return mixed
134
     */
135
    public function getParentId()
136
    {
137
        return $this->parent_id;
138
    }
139
140
    /**
141
     * @param mixed $parent_id
142
     */
143
    public function setParentId($parent_id)
144
    {
145
        $this->parent_id = $parent_id;
146
    }
147
148
    /**
149
     * @return mixed
150
     */
151
    public function getDepth()
152
    {
153
        return $this->depth;
154
    }
155
156
    /**
157
     * @param mixed $depth
158
     */
159
    public function setDepth($depth)
160
    {
161
        $this->depth = $depth;
162
    }
163
164
    /**
165
     * @return mixed
166
     */
167
    public function getLeftKey()
168
    {
169
        return $this->left_key;
170
    }
171
172
    /**
173
     * @param mixed $left_key
174
     */
175
    public function setLeftKey($left_key)
176
    {
177
        $this->left_key = $left_key;
178
    }
179
180
    /**
181
     * @return mixed
182
     */
183
    public function getRightKey()
184
    {
185
        return $this->right_key;
186
    }
187
188
    /**
189
     * @param mixed $right_key
190
     */
191
    public function setRightKey($right_key)
192
    {
193
        $this->right_key = $right_key;
194
    }
195
196
    /**
197
     * @return mixed
198
     */
199
    public function getSlug()
200
    {
201
        return $this->slug;
202
    }
203
204
    /**
205
     * @param mixed $slug
206
     */
207
    public function setSlug($slug)
208
    {
209
        $this->slug = $slug;
210
    }
211
212
    /**
213
     * @return mixed
214
     */
215
    public function getTitle()
216
    {
217
        return $this->getMLVariable('title');
218
    }
219
220
    /**
221
     * @param mixed $title
222
     */
223
    public function setTitle($title)
224
    {
225
        $this->setMLVariable('title', $title);
226
    }
227
228
    /**
229
     * @return mixed
230
     */
231
    public function getCreatedAt()
232
    {
233
        return $this->created_at;
234
    }
235
236
    /**
237
     * @param mixed $created_at
238
     */
239
    public function setCreatedAt($created_at)
240
    {
241
        $this->created_at = $created_at;
242
    }
243
244
    /**
245
     * @return mixed
246
     */
247
    public function getUpdatedAt()
248
    {
249
        return $this->updated_at;
250
    }
251
252
    /**
253
     * @param mixed $updated_at
254
     */
255
    public function setUpdatedAt($updated_at)
256
    {
257
        $this->updated_at = $updated_at;
258
    }
259
}
260