Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
created

src/Eccube/Entity/Category.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Entity;
15
16
use Doctrine\Common\Collections\Criteria;
17
use Doctrine\ORM\Mapping as ORM;
18
19 1
if (!class_exists('\Eccube\Entity\Category')) {
20
    /**
21
     * Category
22
     *
23
     * @ORM\Table(name="dtb_category")
24
     * @ORM\InheritanceType("SINGLE_TABLE")
25
     * @ORM\DiscriminatorColumn(name="discriminator_type", type="string", length=255)
26
     * @ORM\HasLifecycleCallbacks()
27
     * @ORM\Entity(repositoryClass="Eccube\Repository\CategoryRepository")
28
     */
29
    class Category extends \Eccube\Entity\AbstractEntity
30
    {
31
        /**
32
         * @return string
33
         */
34
        public function __toString()
35
        {
36
            return (string) $this->getName();
37
        }
38
39
        /**
40
         * @return integer
41
         */
42
        public function countBranches()
43
        {
44
            $count = 1;
45
46
            foreach ($this->getChildren() as $Child) {
47
                $count += $Child->countBranches();
48
            }
49
50
            return $count;
51
        }
52
53
        /**
54
         * @param  \Doctrine\ORM\EntityManager $em
55
         * @param  integer                     $sortNo
56
         *
57
         * @return \Eccube\Entity\Category
58
         */
59
        public function calcChildrenSortNo(\Doctrine\ORM\EntityManager $em, $sortNo)
60
        {
61
            $this->setSortNo($this->getSortNo() + $sortNo);
62
            $em->persist($this);
63
64
            foreach ($this->getChildren() as $Child) {
65
                $Child->calcChildrenSortNo($em, $sortNo);
66
            }
67
68
            return $this;
69
        }
70
71 1
        public function getParents()
72
        {
73 1
            $path = $this->getPath();
74 1
            array_pop($path);
75
76 1
            return $path;
77
        }
78
79 19
        public function getPath()
80
        {
81 19
            $path = [];
82 19
            $Category = $this;
83
84 19
            $max = 10;
85 19
            while ($max--) {
86 19
                $path[] = $Category;
87
88 19
                $Category = $Category->getParent();
89 19
                if (!$Category || !$Category->getId()) {
90 19
                    break;
91
                }
92
            }
93
94 19
            return array_reverse($path);
95
        }
96
97 131
        public function getNameWithLevel()
98
        {
99 131
            return str_repeat(' ', $this->getHierarchy() - 1).$this->getName();
100
        }
101
102 140
        public function getDescendants()
103
        {
104 140
            $DescendantCategories = [];
105
106 140
            $max = 10;
0 ignored issues
show
$max is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
107 140
            $ChildCategories = $this->getChildren();
108 140
            foreach ($ChildCategories as $ChildCategory) {
109 138
                $DescendantCategories[$ChildCategory->getId()] = $ChildCategory;
110 138
                $DescendantCategories2 = $ChildCategory->getDescendants();
111 138
                foreach ($DescendantCategories2 as $DescendantCategory) {
112 138
                    $DescendantCategories[$DescendantCategory->getId()] = $DescendantCategory;
113
                }
114
            }
115
116 140
            return $DescendantCategories;
117
        }
118
119 140
        public function getSelfAndDescendants()
120
        {
121 140
            return array_merge([$this], $this->getDescendants());
122
        }
123
124
        /**
125
         * カテゴリに紐づく商品があるかどうかを調べる.
126
         *
127
         * ProductCategoriesはExtra Lazyのため, lengthやcountで評価した際にはCOUNTのSQLが発行されるが,
128
         * COUNT自体が重いので, LIMIT 1で取得し存在チェックを行う.
129
         *
130
         * @see http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#filtering-collections
131
         *
132
         * @return bool
133
         */
134 1
        public function hasProductCategories()
135
        {
136 1
            $criteria = Criteria::create()
137 1
            ->orderBy(['category_id' => Criteria::ASC])
138 1
            ->setFirstResult(0)
139 1
            ->setMaxResults(1);
140
141 1
            return $this->ProductCategories->matching($criteria)->count() > 0;
142
        }
143
144
        /**
145
         * @var int
146
         *
147
         * @ORM\Column(name="id", type="integer", options={"unsigned":true})
148
         * @ORM\Id
149
         * @ORM\GeneratedValue(strategy="IDENTITY")
150
         */
151
        private $id;
152
153
        /**
154
         * @var string
155
         *
156
         * @ORM\Column(name="category_name", type="string", length=255)
157
         */
158
        private $name;
159
160
        /**
161
         * @var int
162
         *
163
         * @ORM\Column(name="hierarchy", type="integer", options={"unsigned":true})
164
         */
165
        private $hierarchy;
166
167
        /**
168
         * @var int
169
         *
170
         * @ORM\Column(name="sort_no", type="integer")
171
         */
172
        private $sort_no;
173
174
        /**
175
         * @var \DateTime
176
         *
177
         * @ORM\Column(name="create_date", type="datetimetz")
178
         */
179
        private $create_date;
180
181
        /**
182
         * @var \DateTime
183
         *
184
         * @ORM\Column(name="update_date", type="datetimetz")
185
         */
186
        private $update_date;
187
188
        /**
189
         * @var \Doctrine\Common\Collections\Collection
190
         *
191
         * @ORM\OneToMany(targetEntity="Eccube\Entity\ProductCategory", mappedBy="Category", fetch="EXTRA_LAZY")
192
         */
193
        private $ProductCategories;
194
195
        /**
196
         * @var \Doctrine\Common\Collections\Collection
197
         *
198
         * @ORM\OneToMany(targetEntity="Eccube\Entity\Category", mappedBy="Parent")
199
         * @ORM\OrderBy({
200
         *     "sort_no"="DESC"
201
         * })
202
         */
203
        private $Children;
204
205
        /**
206
         * @var \Eccube\Entity\Category
207
         *
208
         * @ORM\ManyToOne(targetEntity="Eccube\Entity\Category", inversedBy="Children")
209
         * @ORM\JoinColumns({
210
         *   @ORM\JoinColumn(name="parent_category_id", referencedColumnName="id")
211
         * })
212
         */
213
        private $Parent;
214
215
        /**
216
         * @var \Eccube\Entity\Member
217
         *
218
         * @ORM\ManyToOne(targetEntity="Eccube\Entity\Member")
219
         * @ORM\JoinColumns({
220
         *   @ORM\JoinColumn(name="creator_id", referencedColumnName="id")
221
         * })
222
         */
223
        private $Creator;
224
225
        /**
226
         * Constructor
227
         */
228 33
        public function __construct()
229
        {
230 33
            $this->ProductCategories = new \Doctrine\Common\Collections\ArrayCollection();
231 33
            $this->Children = new \Doctrine\Common\Collections\ArrayCollection();
232
        }
233
234
        /**
235
         * Get id.
236
         *
237
         * @return int
238
         */
239 475
        public function getId()
240
        {
241 475
            return $this->id;
242
        }
243
244
        /**
245
         * Set name.
246
         *
247
         * @param string $name
248
         *
249
         * @return Category
250
         */
251 18
        public function setName($name)
252
        {
253 18
            $this->name = $name;
254
255 18
            return $this;
256
        }
257
258
        /**
259
         * Get name.
260
         *
261
         * @return string
262
         */
263 161
        public function getName()
264
        {
265 161
            return $this->name;
266
        }
267
268
        /**
269
         * Set hierarchy.
270
         *
271
         * @param int $hierarchy
272
         *
273
         * @return Category
274
         */
275 15
        public function setHierarchy($hierarchy)
276
        {
277 15
            $this->hierarchy = $hierarchy;
278
279 15
            return $this;
280
        }
281
282
        /**
283
         * Get hierarchy.
284
         *
285
         * @return int
286
         */
287 141
        public function getHierarchy()
288
        {
289 141
            return $this->hierarchy;
290
        }
291
292
        /**
293
         * Set sortNo.
294
         *
295
         * @param int $sortNo
296
         *
297
         * @return Category
298
         */
299 13
        public function setSortNo($sortNo)
300
        {
301 13
            $this->sort_no = $sortNo;
302
303 13
            return $this;
304
        }
305
306
        /**
307
         * Get sortNo.
308
         *
309
         * @return int
310
         */
311 11
        public function getSortNo()
312
        {
313 11
            return $this->sort_no;
314
        }
315
316
        /**
317
         * Set createDate.
318
         *
319
         * @param \DateTime $createDate
320
         *
321
         * @return Category
322
         */
323 26
        public function setCreateDate($createDate)
324
        {
325 26
            $this->create_date = $createDate;
326
327 26
            return $this;
328
        }
329
330
        /**
331
         * Get createDate.
332
         *
333
         * @return \DateTime
334
         */
335
        public function getCreateDate()
336
        {
337
            return $this->create_date;
338
        }
339
340
        /**
341
         * Set updateDate.
342
         *
343
         * @param \DateTime $updateDate
344
         *
345
         * @return Category
346
         */
347 26
        public function setUpdateDate($updateDate)
348
        {
349 26
            $this->update_date = $updateDate;
350
351 26
            return $this;
352
        }
353
354
        /**
355
         * Get updateDate.
356
         *
357
         * @return \DateTime
358
         */
359 1
        public function getUpdateDate()
360
        {
361 1
            return $this->update_date;
362
        }
363
364
        /**
365
         * Add productCategory.
366
         *
367
         * @param \Eccube\Entity\ProductCategory $productCategory
368
         *
369
         * @return Category
370
         */
371
        public function addProductCategory(\Eccube\Entity\ProductCategory $productCategory)
372
        {
373
            $this->ProductCategories[] = $productCategory;
374
375
            return $this;
376
        }
377
378
        /**
379
         * Remove productCategory.
380
         *
381
         * @param \Eccube\Entity\ProductCategory $productCategory
382
         *
383
         * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
384
         */
385
        public function removeProductCategory(\Eccube\Entity\ProductCategory $productCategory)
386
        {
387
            return $this->ProductCategories->removeElement($productCategory);
388
        }
389
390
        /**
391
         * Get productCategories.
392
         *
393
         * @return \Doctrine\Common\Collections\Collection
394
         */
395
        public function getProductCategories()
396
        {
397
            return $this->ProductCategories;
398
        }
399
400
        /**
401
         * Add child.
402
         *
403
         * @param \Eccube\Entity\Category $child
404
         *
405
         * @return Category
406
         */
407 21
        public function addChild(\Eccube\Entity\Category $child)
408
        {
409 21
            $this->Children[] = $child;
410
411 21
            return $this;
412
        }
413
414
        /**
415
         * Remove child.
416
         *
417
         * @param \Eccube\Entity\Category $child
418
         *
419
         * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
420
         */
421
        public function removeChild(\Eccube\Entity\Category $child)
422
        {
423
            return $this->Children->removeElement($child);
424
        }
425
426
        /**
427
         * Get children.
428
         *
429
         * @return \Doctrine\Common\Collections\Collection
430
         */
431 144
        public function getChildren()
432
        {
433 144
            return $this->Children;
434
        }
435
436
        /**
437
         * Set parent.
438
         *
439
         * @param \Eccube\Entity\Category|null $parent
440
         *
441
         * @return Category
442
         */
443 25
        public function setParent(\Eccube\Entity\Category $parent = null)
444
        {
445 25
            $this->Parent = $parent;
446
447 25
            return $this;
448
        }
449
450
        /**
451
         * Get parent.
452
         *
453
         * @return \Eccube\Entity\Category|null
454
         */
455 27
        public function getParent()
456
        {
457 27
            return $this->Parent;
458
        }
459
460
        /**
461
         * Set creator.
462
         *
463
         * @param \Eccube\Entity\Member|null $creator
464
         *
465
         * @return Category
466
         */
467 12
        public function setCreator(\Eccube\Entity\Member $creator = null)
468
        {
469 12
            $this->Creator = $creator;
470
471 12
            return $this;
472
        }
473
474
        /**
475
         * Get creator.
476
         *
477
         * @return \Eccube\Entity\Member|null
478
         */
479
        public function getCreator()
480
        {
481
            return $this->Creator;
482
        }
483
    }
484
}
485