Taxonomy::getTree()   C
last analyzed

Complexity

Conditions 13
Paths 66

Size

Total Lines 88

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
nc 66
nop 1
dl 0
loc 88
ccs 0
cts 75
cp 0
crap 182
rs 5.5551
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Rocket\UI\Taxonomy;
2
3
use Rocket\Taxonomy\Model\Hierarchy;
4
use Rocket\Taxonomy\Model\TermContainer;
5
use Rocket\Taxonomy\Model\TermData;
6
use Rocket\Taxonomy\Support\Laravel5\Facade as T;
7
use Rocket\Translation\Support\Laravel5\Facade as I18N;
8
9
class Taxonomy
10
{
11
    /**
12
     * Get a tree of all the data in the vocabulary, use with caution
13
     *
14
     * @param  array $vocabulary_id
15
     * @return string
16
     */
17
    public static function getTree($vocabulary_id)
18
    {
19
        $term_table = (new TermContainer)->getTable();
20
        $data_table = (new TermData)->getTable();
21
22
        $terms = TermContainer::select(
23
            "$term_table.id",
24
            "$term_table.type",
25
            "$term_table.vocabulary_id",
26
            "$data_table.language_id",
27
            "$data_table.title",
28
            "$data_table.description"
29
        )
30
            ->join($data_table, "$term_table.id", '=', 'term_id')
31
            ->whereIn('vocabulary_id', $vocabulary_id)
32
            ->get();
33
34
        $translations = [];
35
        foreach ($terms as $t) {
36
            $translations[$t->id][$t->language_id] = $t;
37
        }
38
39
        $hierarchy = Hierarchy::whereIn('term_id', $terms->pluck('id'))->pluck('parent_id', 'term_id');
40
41
        unset($terms);
42
43
        $table_data = [];
44
        foreach ($translations as $langs) {
45
            $f = array_slice($langs, 0, 1);
46
            $f = $f[0];
47
48
            $row = [
49
                'id' => $f->id,
50
                'parent_id' => (array_key_exists($f->id, $hierarchy)) ? $hierarchy[$f->id] : null,
51
                'text' => $f->title,
52
                'vid' => $f->vocabulary_id,
53
            ];
54
55
            if (T::isTranslatable($vocabulary_id[0])) {
56
                foreach (I18N::languages() as $lang => $d) {
57
                    if (array_key_exists($d['id'], $langs)) {
58
                        $content = $langs[$d['id']]->title;
59
                    } else {
60
                        $content = '<span class="not_translated" title="' . $f->id . '">Not Translated</span>';
61
                    }
62
63
                    $row['data'][$lang] =
64
                        (($langs[1]->type == 1) ? '<strong>' : '') .
65
                        $content .
66
                        (($langs[1]->type == 1) ? '</strong>' : '');
67
                }
68
            } else {
69
                $row['data'][I18N::languages(1, 'iso')] =
70
                    (($langs[1]->type == 1) ? '<strong>' : '') .
71
                    $langs[1]->title .
72
                    (($langs[1]->type == 1) ? '</strong>' : '');
73
            }
74
75
            $table_data[$f->id] = $row;
76
        }
77
78
        unset($translations);
79
80
        $root_node = [
81
            'id' => 'root',
82
            'parent_id' => null,
83
            'text' => '',
84
            'vid' => 'tags',
85
        ];
86
        foreach (I18N::languages() as $lang => $d) {
87
            if ($d['id'] == 1) {
88
                $root_node['data'][$lang] = 'Root';
89
            } else {
90
                $root_node['data'][$lang] = '--';
91
            }
92
        }
93
94
        $tree = new ParentChildTree(
95
            $table_data,
96
            [
97
                'create_root' => true,
98
                'default_root' => $root_node,
99
                'default_root_id' => 'root',
100
            ]
101
        );
102
103
        return $tree;
104
    }
105
106
    /**
107
     * Get the values for a select form element
108
     *
109
     * @param  string|int $vid
110
     * @return array
111
     */
112
    public static function forSelectField($vid)
113
    {
114
        //get vid
115
        if (!is_numeric($vid)) {
116
            $vid = T::vocabulary($vid);
117
        }
118
119
        //get terms
120
        $tids = T::getTermsForVocabulary($vid);
121
122
        $terms = [];
123
        foreach ($tids as $tid) {
124
            $term = T::getTerm($tid);
125
            $terms[$term['term_id']] = ucfirst($term['title']);
126
        }
127
128
        //sort
129
        natsort($terms);
130
131
        //return
132
        return $terms;
133
    }
134
}
135