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

SubordinateLimit::getLimit()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 19
rs 8.8571
c 1
b 0
f 1
cc 6
eloc 12
nc 6
nop 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
namespace rhosocial\organization;
13
14
use rhosocial\base\models\models\BaseBlameableModel;
15
use rhosocial\organization\Organization;
16
use Yii;
17
18
/**
19
 * Class SubordinateLimit
20
 *
21
 * @property integer $limit
22
 *
23
 * @package rhosocial\organization
24
 * @version 1.0
25
 * @author vistart <[email protected]>
26
 */
27
class SubordinateLimit extends BaseBlameableModel
28
{
29
    public $contentAttribute = 'limit';
30
    public $contentAttributeRule = ['integer', 'min' => 0];
31
    public $createdByAttribute = 'organization_guid';
32
    public $updatedByAttribute = false;
33
    public $idAttribute = false;
34
    /**
35
     * @var string The host who owns this model.
36
     * Note: Please assign it with your own Organization model.
37
     */
38
    public $hostClass = Organization::class;
39
    public $defaultLimit = 50;
40
41
    public static function tableName()
42
    {
43
        return '{{%organization_subordinate_limit}}';
44
    }
45
46
    protected function getLimitRules()
47
    {
48
        return [
49
            [$this->contentAttribute, 'default', 'value' => is_numeric($this->defaultLimit) ? (int)$this->defaultLimit : 50]
50
        ];
51
    }
52
53
    public function rules()
54
    {
55
        return array_merge(parent::rules(), $this->getLimitRules());
56
    }
57
58
    public function attributeLabels()
59
    {
60
        return [
61
            $this->guidAttribute => Yii::t('user', 'GUID'),
62
            $this->createdByAttribute => Yii::t('organization', 'Organization GUID'),
63
            $this->contentAttribute => Yii::t('organization', 'Limit'),
64
            $this->ipAttribute => Yii::t('user', 'IP Address'),
65
            $this->ipTypeAttribute => Yii::t('user', 'IP Address Type'),
66
            $this->createdAtAttribute => Yii::t('user', 'Creation Time'),
67
            $this->updatedAtAttribute => Yii::t('user', 'Last Updated Time'),
68
        ];
69
    }
70
71
    /**
72
     * Get the upper limit of subordinates the user could set up.
73
     * @param $organization
74
     * @return int|boolean
75
     */
76
    public static function getLimit($organization)
77
    {
78
        if (!($organization instanceof Organization) || ($organization->getIsNewRecord() && $organization = $organization->getGUID())) {
79
            $noInit = static::buildNoInitModel();
80
            $class = $noInit->hostClass;
81
            $organization = $class::find()->guidOrId($organization)->one();
82
        }
83
        /* @var $organization Organization */
84
        if (empty($organization->subordinateLimitClass)) {
85
            return false;
86
        }
87
        $limit = static::find()->createdBy($organization)->one();
88
        /* @var $limit static */
89
        if (!$limit) {
90
            $limit = $organization->create(static::class);
91
            $limit->save();
92
        }
93
        return $limit->limit;
94
    }
95
}
96