PropertySupportBehavior   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 3
cbo 2
dl 0
loc 42
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A canGetProperty() 0 8 2
A __get() 0 8 2
A hasMethod() 0 4 1
A getPropertyTerm() 0 4 1
A getProperties() 0 4 1
1
<?php
2
3
namespace nkostadinov\taxonomy\behaviors;
4
5
use Yii;
6
use yii\base\Behavior;
7
use yii\base\Event;
8
use yii\db\ActiveRecord;
9
use yii\db\AfterSaveEvent;
10
11
/**
12
 * @author Nikolay Traykov
13
 */
14
class PropertySupportBehavior extends Behavior
15
{
16
    public $property;
17
18
    private $_allowedMethods = [
19
        'propertyTerm' => 'getPropertyTerm',
20
        'properties' => 'getProperties',
21
    ];
22
23
    public function canGetProperty($name, $checkVars = true)
24
    {
25
        if (array_key_exists($name, $this->_allowedMethods)) {
26
            return true;
27
        }
28
29
        return false;
30
    }
31
32
    public function __get($name)
33
    {
34
        if (array_key_exists($name, $this->_allowedMethods)) {
35
            return $this->{$this->_allowedMethods[$name]}();
36
        }
37
38
        return false;
39
    }
40
41
    public function hasMethod($name)
42
    {
43
        return in_array($name, $this->_allowedMethods);
44
    }
45
46
    public function getPropertyTerm()
47
    {
48
        return Yii::$app->taxonomy->getTerm($this->property);
49
    }
50
51
    public function getProperties($properties = [])
52
    {
53
        return $this->propertyTerm->getTerms($this->owner->id, $properties);
0 ignored issues
show
Bug introduced by
The property propertyTerm does not seem to exist. Did you mean property?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
54
    }
55
}
56