Taxonomy   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 2
cbo 8
dl 0
loc 66
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isTermInstalled() 0 5 1
A addTerm() 0 5 1
A removeTerm() 0 5 1
A getTerms() 0 5 1
A getTerm() 0 9 3
A isInstalled() 0 4 1
A getDefinitions() 0 7 1
1
<?php
2
/**
3
 * @author Nikola Kostadinov<[email protected]>
4
 * Date: 19.10.2014
5
 * Time: 10:46 ч.
6
 */
7
8
namespace nkostadinov\taxonomy;
9
10
use nkostadinov\taxonomy\components\exceptions\TermNotDefinedException;
11
use nkostadinov\taxonomy\components\terms\BaseTerm;
12
use nkostadinov\taxonomy\components\terms\CategoryTerm;
13
use nkostadinov\taxonomy\components\terms\PropertyTerm;
14
use nkostadinov\taxonomy\components\terms\TagTerm;
15
use nkostadinov\taxonomy\models\TaxonomyDef;
16
use nkostadinov\taxonomy\models\TaxonomyTerms;
17
use yii\base\Component;
18
use yii\base\InvalidConfigException;
19
use yii\db\Connection;
20
use yii\db\Migration;
21
use yii\db\Schema;
22
use yii\log\Logger;
23
24
class Taxonomy extends Component
25
{
26
    /* @var Connection The db connection component */
27
    public $db = 'db';
28
    public $table = 'taxonomy';
29
    //cache array of initialized terms
30
    private $_taxonomy = [];
31
    //
32
    public $definitions = [];
33
34
    public function isTermInstalled($termName)
35
    {
36
        $term = $this->getTerm($termName);
37
        return $term->isInstalled();
38
    }
39
40
    public function addTerm($term, $object_id, $params)
41
    {
42
        $term = $this->getTerm($term);
43
        $term->addTerm($object_id, $params);
44
    }
45
46
    public function removeTerm($term, $object_id, $params = [])
47
    {
48
        $term = $this->getTerm($term);
49
        return $term->removeTerm($object_id, $params);
50
    }
51
52
    public function getTerms($term, $object_id, $name = null)
53
    {
54
        $term = $this->getTerm($term);
55
        return $term->getTerms($object_id, $name);
56
    }
57
58
    /**
59
     * @param $termName
60
     * @return BaseTerm
61
     * @throws InvalidConfigException
62
     * @throws TermNotDefinedException
63
     */
64
    public function getTerm($termName, $reload = false)
65
    {
66
        if(!isset($this->_taxonomy[$termName]) || $reload) {
67
            $tax = TaxonomyDef::findOne(['name' => $termName]);
68
            \Yii::getLogger()->log("Initialising term $termName", Logger::LEVEL_INFO, 'nkostadinov.taxonomy.terms');
69
            $this->_taxonomy[$termName] = \Yii::createObject($tax->attributes);
70
        }
71
        return $this->_taxonomy[$termName];
72
    }
73
74
    public function isInstalled()
75
    {
76
        return \Yii::$app->db->getTableSchema(TaxonomyDef::tableName(), true) !== null;
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function getDefinitions()
83
    {
84
        return array_merge(
85
            [TagTerm::className(), PropertyTerm::className(), CategoryTerm::className()],
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
86
            $this->definitions
87
        );
88
    }
89
}