Completed
Push — master ( cfb1a5...baf1ef )
by Nikola
03:35
created

CategoryTerm   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 120
c 1
b 0
f 0
wmc 21
lcom 1
cbo 7
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTerms() 0 20 3
C addTerm() 0 40 8
A getParent() 0 15 4
A hasParent() 0 4 1
A getChildren() 0 19 3
A hasChildren() 0 12 2
1
<?php
2
3
namespace nkostadinov\taxonomy\components\terms;
4
5
use insight\core\helpers\ArrayHelper;
6
use nkostadinov\taxonomy\components\interfaces\IHierarchicalTerm;
7
use nkostadinov\taxonomy\models\TaxonomyDef;
8
use nkostadinov\taxonomy\models\TaxonomyTerms;
9
use Yii;
10
use yii\db\Query;
11
12
class CategoryTerm extends TagTerm implements IHierarchicalTerm
13
{
14
    public $templateFile = '@nkostadinov/taxonomy/migrations/template/category.php' ;
15
16
    public function getTerms($object_id, $name = [])
17
    {
18
        $query = (new Query())
19
            ->select(TaxonomyTerms::tableName() . '.term')
20
            ->from(TaxonomyTerms::tableName())
21
            ->innerJoin($this->table, $this->table . '.term_id = taxonomy_terms.id',
0 ignored issues
show
Documentation introduced by
The property table does not exist on object<nkostadinov\taxon...nts\terms\CategoryTerm>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
22
                [':object_id' => $object_id])
23
            ->andFilterWhere(['taxonomy_terms.term' => $name]);
24
25
        if ($object_id) {
26
            $query->onCondition("$this->table.object_id = $object_id");
0 ignored issues
show
Documentation introduced by
The property table does not exist on object<nkostadinov\taxon...nts\terms\CategoryTerm>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
27
        }
28
29
        $result = [];
30
        foreach($query->all() as $v) {
31
            $result[] = $v['term'];
32
        }
33
34
        return $result;
35
    }
36
37
    public function addTerm($object_id, $params)
38
    {
39
        $cachedParents = [];
40
41
        $addTerm = function ($parent, $item) use ($object_id, &$cachedParents) {
42
            $term = $this->getTaxonomyTerm($item);
43
            $data['term_id'] = $term->id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
44
            $data['object_id'] = $object_id;
45
46
            if (array_key_exists($parent, $cachedParents)) {
47
                $term->parent_id = $cachedParents[$parent]->id;
48
            } else if (is_string($parent)) {
49
                $parentTerm = $this->getTaxonomyTerm($parent);
50
                $cachedParents[$parent] = $parentTerm;
51
                $term->parent_id = $parentTerm->id;
52
            }
53
54
            if (!(new Query())->from($this->table)->where($data)->exists(CategoryTerm::getDb())) {
0 ignored issues
show
Documentation introduced by
The property table does not exist on object<nkostadinov\taxon...nts\terms\CategoryTerm>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
                Yii::$app->db->transaction(function() use ($data, $term) {
56
                    if ($term->getDirtyAttributes(['parent_id'])) {
57
                        $term->save(false);
58
                    }
59
                    CategoryTerm::getDb()->createCommand()->insert($this->table, $data)->execute();
0 ignored issues
show
Documentation introduced by
The property table does not exist on object<nkostadinov\taxon...nts\terms\CategoryTerm>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
60
61
                    $term->updateCounters(['total_count' => 1]);
62
                    TaxonomyDef::updateAllCounters(['total_count' => 1], ['id' => $this->id]);
63
                });
64
            }
65
        };
66
67
        foreach ($params as $parent => $item) {
68
            if (is_array($item)) {
69
                foreach ($item as $child) {
70
                    $addTerm($parent, $child);
71
                }
72
            } else {
73
                $addTerm($parent, $item);
74
            }
75
        }
76
    }
77
78
    public function getParent($term)
79
    {
80
        $childTerm = $this->getTaxonomyTerm($term);
81
        if (!$childTerm || is_null($childTerm->parent_id)) {
82
            return null;
83
        }
84
85
        $parentTerm = (new Query())
86
            ->select('term')
87
            ->from(TaxonomyTerms::tableName())
88
            ->where("id = $childTerm->parent_id")
89
            ->one(self::getDb());
90
91
        return $parentTerm ? $parentTerm['term'] : null;
92
    }
93
94
    public function hasParent($term)
95
    {
96
        return $this->getParent($term) != null;
97
    }
98
99
    public function getChildren($term)
100
    {
101
        $parentTerm = $this->getTaxonomyTerm($term);
102
        if (!$parentTerm) {
103
            return [];
104
        }
105
106
        $query = (new Query())
107
            ->select('term')
108
            ->from(TaxonomyTerms::tableName())
109
            ->where("parent_id = $parentTerm->id");
110
111
        $result = [];
112
        foreach ($query->all(self::getDb()) as $row) {
113
            $result[] = $row['term'];
114
        }
115
116
        return $result;
117
    }
118
    
119
    public function hasChildren($term)
120
    {
121
        $parentTerm = $this->getTaxonomyTerm($term);
122
        if (!$parentTerm) {
123
            return false;
124
        }
125
        
126
        return (new Query())
127
            ->from(TaxonomyTerms::tableName())
128
            ->where("parent_id = $parentTerm->id")
129
            ->exists(self::getDb());
130
    }
131
}
132