GSCPService   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Test Coverage

Coverage 98.73%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 28
eloc 78
c 1
b 0
f 0
dl 0
loc 164
ccs 78
cts 79
cp 0.9873
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B parse() 0 31 7
A getLocale() 0 6 2
A format() 0 11 3
A toArray() 0 14 3
A addChildren() 0 12 4
A getCategories() 0 20 4
A toJson() 0 3 1
A addParents() 0 12 3
1
<?php
2
namespace GSCP;
3
4
use Exception;
5
6
class GSCPService
7
{
8
    use GSCPSettersTrait;
9
    use GSCPDownloadTrait;
10
11
    private const DEPTH = 6;
12
13
    /** @var string */
14
    private $filename;
15
16
    /** @var string */
17
    private $locale;
18
19
    /** @var array */
20
    private $categories;
21
22
    /** @var string[] */
23
    private $columns;
24
25 5
    public function __construct(array $options = [])
26
    {
27 5
        $this->filename = $options['filename'] ?? 'gscp.txt';
28 5
        $this->locale = $options['locale'] ?? 'en-US';
29 5
        $this->columns = $options['columns'] ?? [
30 5
            'id',
31
            'name',
32
            'parentId',
33
            'parents',
34
            'children',
35
        ];
36 5
    }
37
38
    /**
39
     * @return array
40
     */
41 5
    public function toArray(): array
42
    {
43 5
        $this->getCategories();
44
45 4
        $this->parse();
46 4
        if (in_array('children', $this->columns, true)) {
47 3
            $this->addChildren();
48
        }
49 4
        if (in_array('parents', $this->columns, true)) {
50 3
            $this->addParents();
51
        }
52 4
        $this->format();
53
54 4
        return $this->categories;
55
    }
56
57
    /**
58
     * @return false|string
59
     */
60 2
    public function toJson()
61
    {
62 2
        return json_encode($this->toArray());
63
    }
64
65 4
    private function parse(): void
66
    {
67 4
        $parsedCategories = [];
68 4
        for ($depth = 0; $depth < self::DEPTH; $depth++) {
69 4
            foreach ($this->categories as $id => $category) {
70 4
                if (count($category) !== $depth + 1) {
71 4
                    continue;
72
                }
73
74 4
                $parentId = 0;
75 4
                $parentCategoryDepth = $depth - 1;
76 4
                if ($parentCategoryDepth >= 0) {
77 4
                    foreach ($parsedCategories as $item) {
78 4
                        if ($item['name'] === $category[$parentCategoryDepth]) {
79 4
                            $parentId = $item['id'];
80 4
                            break;
81
                        }
82
                    }
83
                }
84
85
                $parsedCategories[$id] = [
86 4
                    'id' => $id,
87 4
                    'parentId' => $parentId,
88
                    'parents' => [],
89
                    'children' => [],
90 4
                    'name' => $category[$depth]
91
                ];
92
            }
93
        }
94
95 4
        $this->categories = $parsedCategories;
96 4
    }
97
98 3
    private function addChildren(): void
99
    {
100 3
        foreach ($this->categories as $id => &$category) {
101 3
            $children = [];
102
103 3
            foreach ($this->categories as $row) {
104 3
                if ($row['parentId'] === $id) {
105 3
                    $children[] = $row['id'];
106
                }
107
            }
108
109 3
            $category['children'] = $children;
110
        }
111 3
    }
112
113 3
    private function addParents(): void
114
    {
115 3
        foreach ($this->categories as &$item) {
116
117 3
            $parents = [];
118 3
            $parentId = $item['parentId'];
119 3
            while ($parentId !== 0) {
120 3
                $parents[] = (int) $parentId;
121 3
                $parentId = $this->categories[$parentId]['parentId'];
122
            }
123
124 3
            $item['parents'] = $parents;
125
        }
126 3
    }
127
128 4
    private function format(): void
129
    {
130 4
        $formattedCategories = [];
131 4
        foreach ($this->categories as $id => $category) {
132 4
            $formattedCategory = [];
133 4
            foreach ($this->columns as $column) {
134 4
                $formattedCategory[$column] = $category[$column];
135
            }
136 4
            $formattedCategories[$id] = $formattedCategory;
137
        }
138 4
        $this->categories = $formattedCategories;
139 4
    }
140
141 5
    private function getCategories(): void
142
    {
143 5
        $this->download();
144 4
        $content = (string) file_get_contents($this->filename);
145 4
        $list = explode(PHP_EOL, $content);
146
147 4
        $this->categories = [];
148
149 4
        foreach ($list as $row) {
150 4
            if ('' === $row) {
151 4
                continue;
152
            }
153
154 4
            if ('#' === $row[0]) {
155 4
                continue;
156
            }
157
158 4
            [$id, $text] = explode(' - ', $row);
159
160 4
            $this->categories[(int) $id] = explode(' > ', trim($text));
161
        }
162 4
    }
163
164 5
    private function getLocale(): string
0 ignored issues
show
Unused Code introduced by
The method getLocale() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
165
    {
166 5
        if (!preg_match('/^[a-z]{2}-[A-Z]{2}$/i', $this->locale)) {
167 1
            throw new Exception('Please setup correct locale');
168
        }
169 4
        return $this->locale;
170
    }
171
}
172