Test Failed
Push — master ( 3b0034...da6aa6 )
by vistart
03:59
created

Invitation   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 2
cbo 3
dl 0
loc 79
ccs 23
cts 25
cp 0.92
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getInviteeRules() 0 15 2
A rules() 0 4 1
A setInvitee() 0 7 2
A getInvitee() 0 5 1
A findByInvitee() 0 4 1
A tableName() 0 4 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\user\models\invitation;
14
15
use rhosocial\base\models\models\BaseBlameableModel;
16
use rhosocial\base\models\queries\BaseBlameableQuery;
17
use rhosocial\user\User;
18
19
/**
20
 * Class Invitation
21
 * This class is used to record each invitation of the current user. If you want to use the invitation code, please
22
 * refer to the [[InvitationCode]] class.
23
 * You can not use this class directly, but you need to declare and use specific invitation scenes, see [[Registration]].
24
 *
25
 * @property integer $content Invitation Type. The custom value range should be greater than or equal to 0x80.
26
 * Once this value is determined, no modification is recommended.
27
 * @property User|string $invitee Invited person.
28
 * @property string $invitee_guid The GUID of invited person.
29
 * Once this value is determined, no modification is recommended.
30
 *
31
 * @package rhosocial\user\models\invitation
32
 * @version 1.0
33
 * @author vistart <[email protected]>
34
 */
35
abstract class Invitation extends BaseBlameableModel
36
{
37
    public $hostClass = User::class;
38
    public $idAttribute = false;
39
    public $updatedAtAttribute = false;
40
    /**
41
     * @var array The content field in the data table should be an integer.
42
     */
43
    public $contentAttributeRule = ['integer'];
44
    /**
45
     * @var bool Whether to allow to send invitation to the same invitee repeatedly.
46
     */
47
    public $allowRepeated = true;
48
49
    /**
50
     * @return array
51
     */
52 3
    public function getInviteeRules()
53
    {
54
        $rules = [
55 3
            ['invitee_guid', 'required'],
56 3
            ['invitee_guid', 'string'],
57 3
        ];
58 3
        if (!$this->allowRepeated) {
59 3
            $rules[] = [
60 3
                [$this->createdByAttribute, 'invitee_guid'], 'unique', 'targetAttribute' => [
61 3
                    $this->createdByAttribute, 'invitee_guid',
62
                ]
63 3
            ];
64 3
        }
65 3
        return $rules;
66
    }
67
68
    /**
69
     * @return array
70
     */
71 3
    public function rules()
72
    {
73 3
        return array_merge(parent::rules(), $this->getInviteeRules());
74
    }
75
76
    /**
77
     * @param User|string $invitee
78
     * @return string
79
     */
80 3
    public function setInvitee($invitee)
81
    {
82 3
        if ($invitee instanceof User) {
83 3
            $invitee = $invitee->getGUID();
84 3
        }
85 3
        return $this->invitee_guid = $invitee;
86
    }
87
88
    /**
89
     * @return User
90
     */
91 2
    public function getInvitee()
92
    {
93 2
        $userClass = $this->hostClass;
94 2
        return $userClass::findOne($this->invitee_guid);
95
    }
96
97
    /**
98
     * @param User|string $invitee
99
     * @return BaseBlameableQuery
100
     */
101
    public static function findByInvitee($invitee)
102
    {
103
        return static::find()->andWhere(['invitee_guid' => (string)$invitee]);
104
    }
105
106
    /**
107
     * @return string
108
     */
109 4
    public static function tableName()
110
    {
111 4
        return '{{%user_invitation}}';
112
    }
113
}
114