Test Failed
Push — master ( cb8c8d...a9ed64 )
by vistart
09:27
created

OrganizationSetting   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 60%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 115
ccs 24
cts 40
cp 0.6
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A onAssignOperator() 0 8 2
A getOperator() 0 10 3
A behaviors() 0 13 3
A rules() 0 7 1
A getContentRules() 0 6 1
A attributeLabels() 0 12 1
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\organization;
14
15
use rhosocial\base\models\models\BaseBlameableModel;
16
use rhosocial\base\models\queries\BaseUserQuery;
17
use rhosocial\user\User;
18
use Yii;
19
use yii\base\Event;
20
use yii\behaviors\BlameableBehavior;
21
22
/**
23
 * Class OrganizationSetting
24
 *
25
 * @property string $item
26
 * @property mixed $value
27
 * @property-read User $operator The user who last modified this setting.
28
 * Note: the return value may be null, please note that the case of invalid user.
29
 *
30
 * @package rhosocial\organization
31
 * @version 1.0
32
 * @author vistart <[email protected]>
33
 */
34
class OrganizationSetting extends BaseBlameableModel
35
{
36
    /**
37
     * @var string Host class.
38
     * You must assign with your own [[Organization]] class.
39
     */
40
    public $hostClass = Organization::class;
41
42
    public $idAttribute = 'item';
43
    public $idPreassigned = true;
44
    public $createdByAttribute = 'organization_guid';
45
    public $updatedByAttribute = false;
46
    public $enableIP = false;
47
48
    /**
49
     * @var string
50
     */
51
    public $operatorAttribute = 'operator_guid';
52
53
    /**
54
     * @var string
55
     */
56
    public $contentAttribute = 'value';
57
58
    /**
59
     * Get operator query.
60
     * If you want to get operator, please access [[$operator]] magic-property.
61
     * @return BaseUserQuery
62
     */
63
    public function getOperator()
64
    {
65
        if (empty($this->operatorAttribute) || !is_string($this->operatorAttribute)) {
66
            return null;
67
        }
68
        $userClass = Yii::$app->user->identityClass;
69
        $noInit = $userClass::buildNoInitModel();
70
        /* @var $noInit User */
71
        return $this->hasOne($userClass, [$noInit->guidAttribute => $this->operatorAttribute]);
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 31
    public static function tableName()
78
    {
79 31
        return '{{%organization_setting}}';
80
    }
81
82
    /**
83
     * @param Event $event
84
     * @return null|string
85
     */
86 31
    public function onAssignOperator($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88 31
        $identity = Yii::$app->user->identity;
89 31
        if (empty($identity)) {
90 31
            return null;
91
        }
92
        return $identity->getGUID();
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98 31
    public function behaviors()
99
    {
100 31
        $behaviors = parent::behaviors();
101 31
        if (!empty($this->operatorAttribute) && is_string($this->operatorAttribute)) {
102 31
            $behaviors[] = [
103 31
                'class' => BlameableBehavior::class,
104 31
                'createdByAttribute' => false,
105 31
                'updatedByAttribute' => $this->operatorAttribute,
106 31
                'value' => [$this, 'onAssignOperator'],
107
            ];
108 31
        }
109 31
        return $behaviors;
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115 31
    public function rules()
116
    {
117 31
        return array_merge(parent::rules(), [
118 31
            [$this->idAttribute, 'string', 'max' => 255],
119 31
            [$this->operatorAttribute, 'safe'],
120 31
        ]);
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126 31
    public function getContentRules()
127
    {
128
        return [
129 31
            array_merge([$this->contentAttribute], $this->contentAttributeRule),
130 31
        ];
131
    }
132
133
    /**
134
     * @inheritdoc
135
     */
136
    public function attributeLabels()
137
    {
138
        return [
139
            $this->guidAttribute => Yii::t('user','GUID'),
140
            $this->createdByAttribute => Yii::t('organization', 'Organization GUID'),
141
            $this->idAttribute => 'Item',
142
            $this->contentAttribute => 'Value',
143
            $this->operatorAttribute => Yii::t('organization', 'Operator'),
144
            $this->createdAtAttribute => Yii::t('user', 'Creation Time'),
145
            $this->updatedAtAttribute => Yii::t('user', 'Last Updated Time'),
146
        ];
147
    }
148
}
149