Completed
Push — master ( ca3e5a...8325fc )
by vistart
04:16
created

OrganizationLimit::tableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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