Completed
Push — master ( 1cd8ff...94fceb )
by vistart
17:24
created

MemberLimit   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
lcom 2
cbo 3
dl 0
loc 69
rs 10
c 1
b 0
f 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A getLimitRules() 0 6 2
A rules() 0 4 1
A attributeLabels() 0 12 1
B getLimit() 0 19 6
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
namespace rhosocial\organization;
13
14
use rhosocial\base\models\models\BaseBlameableModel;
15
use rhosocial\user\User;
16
use Yii;
17
18
class MemberLimit extends BaseBlameableModel
19
{
20
    public $contentAttribute = 'limit';
21
    public $contentAttributeRule = ['integer', 'min' => 0];
22
    public $createdByAttribute = 'organization_guid';
23
    public $updatedByAttribute = false;
24
    public $idAttribute = false;
25
    /**
26
     * @var string The host who owns this model.
27
     * Note: Please assign it with your own Organization model.
28
     */
29
    public $hostClass = Organization::class;
30
    public $defaultLimit = 100;
31
32
    public static function tableName()
33
    {
34
        return '{{%organization_member_limit}}';
35
    }
36
37
    protected function getLimitRules()
38
    {
39
        return [
40
            [$this->contentAttribute, 'default', 'value' => is_numeric($this->defaultLimit) ? (int)$this->defaultLimit : 100]
41
        ];
42
    }
43
44
    public function rules()
45
    {
46
        return array_merge(parent::rules(), $this->getLimitRules());
47
    }
48
49
    public function attributeLabels()
50
    {
51
        return [
52
            $this->guidAttribute => Yii::t('user', 'GUID'),
53
            $this->createdByAttribute => Yii::t('organization', 'Organization GUID'),
54
            $this->contentAttribute => Yii::t('organization', 'Limit'),
55
            $this->ipAttribute => Yii::t('user', 'IP Address'),
56
            $this->ipTypeAttribute => Yii::t('user', 'IP Address Type'),
57
            $this->createdAtAttribute => Yii::t('user', 'Creation Time'),
58
            $this->updatedAtAttribute => Yii::t('user', 'Last Updated Time'),
59
        ];
60
    }
61
62
    /**
63
     * Get the upper limit of members.
64
     * @param $organization
65
     * @return int|boolean
66
     */
67
    public static function getLimit($organization)
68
    {
69
        if (!($organization instanceof Organization) || ($organization->getIsNewRecord() && $organization = $organization->getGUID())) {
70
            $noInit = static::buildNoInitModel();
71
            $class = $noInit->hostClass;
72
            $organization = $class::find()->guidOrId($organization)->one();
73
        }
74
        /* @var $organization Organization */
75
        if (empty($organization->memberLimitClass)) {
76
            return false;
77
        }
78
        $limit = static::find()->createdBy($organization)->one();
79
        /* @var $limit static */
80
        if (!$limit) {
81
            $limit = $organization->create(static::class);
82
            $limit->save();
83
        }
84
        return $limit->limit;
85
    }
86
}
87