CategoryCollection::indexRow()   B
last analyzed

Complexity

Conditions 9
Paths 54

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.8886

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 22
ccs 14
cts 18
cp 0.7778
rs 8.0555
c 0
b 0
f 0
cc 9
nc 54
nop 2
crap 9.8886
1
<?php
2
/**
3
 * @author @jenschude <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Model\Category;
7
8
use Commercetools\Core\Model\Common\Collection;
9
10
/**
11
 * @package Commercetools\Core\Model\Category
12
 * @link https://docs.commercetools.com/http-api-projects-categories.html#category
13
 * @method Category current()
14
 * @method CategoryCollection add(Category $element)
15
 * @method Category getAt($offset)
16
 */
17
class CategoryCollection extends Collection
18
{
19
    const SLUG = 'slug';
20
    const PARENT = 'parent';
21
    const ROOTS = 'roots';
22
    const KEY = 'key';
23
24
    protected $type = Category::class;
25
26 6
    protected function indexRow($offset, $row)
27
    {
28 6
        if ($row instanceof Category) {
29
            $slugs = $row->getSlug()->toArray();
30
            $parentId = !is_null($row->getParent()) ? $row->getParent()->getId() : null;
31
            $id = $row->getId();
32
            $key = $row->getKey();
33
        } else {
34 6
            $slugs = isset($row[static::SLUG]) ? $row[static::SLUG] : [];
35 6
            $id = isset($row[static::ID]) ? $row[static::ID] : null;
36 6
            $key = isset($row[static::KEY]) ? $row[static::KEY] : null;
37 6
            $parentId = isset($row[static::PARENT][static::ID]) ? $row[static::PARENT][static::ID] : null;
38
        }
39 6
        $this->addToIndex(static::ID, $offset, $id);
40 6
        $this->addToIndex(static::KEY, $offset, $key);
41 6
        foreach ($slugs as $locale => $slug) {
42 5
            $locale = \Locale::canonicalize($locale);
43 5
            $this->addToIndex(static::SLUG, $offset, $locale . '-' . $slug);
44 5
            if (is_null($parentId)) {
45 5
                $this->addToIndex(static::ROOTS, $offset, $id);
46
            } else {
47 1
                $this->addToIndex(static::PARENT . '-' . $parentId, $offset, $id);
48
            }
49
        }
50 6
    }
51
52
    /**
53
     * @param $id
54
     * @return Category|null
55
     */
56
    public function getById($id)
57
    {
58
        return $this->getBy(static::ID, $id);
59
    }
60
61
    /**
62
     * @param $key
63
     * @return Category|null
64
     */
65
    public function getByKey($key)
66
    {
67
        return $this->getBy(static::KEY, $key);
68
    }
69
70
    /**
71
     * @return Category[]
72
     */
73
    public function getRoots()
74
    {
75
        $elements = [];
76
        foreach ($this->index[static::ROOTS] as $key => $offset) {
77
            $elements[$key] = $this->getAt($offset);
78
        }
79
        return $elements;
80
    }
81
82
    /**
83
     * @param $parentId
84
     * @return Category[]
85
     */
86
    public function getByParent($parentId)
87
    {
88
        $elements = [];
89
        if (!isset($this->index[static::PARENT . '-' . $parentId])) {
90
            return $elements;
91
        }
92
93
        foreach ($this->index[static::PARENT . '-' . $parentId] as $key => $offset) {
94
            $elements[$key] = $this->getAt($offset);
95
        }
96
        return $elements;
97
    }
98
99
    /**
100
     * @param $locale
101
     * @param $slug
102
     * @return Category
103
     */
104
    public function getBySlug($slug, $locale)
105
    {
106
        $locale = \Locale::canonicalize($locale);
107
        $category = $this->getBy(static::SLUG, $locale . '-' . $slug);
108
109
        if ($category instanceof Category) {
110
            return $category;
111
        }
112
113
        $language = \Locale::getPrimaryLanguage($locale);
114
        return $this->getBy(static::SLUG, $language . '-' . $slug);
115
    }
116
}
117