Test Failed
Push — master ( fde873...dc1916 )
by vistart
05:54
created

Invitation::getInvitee()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
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
    public function getInviteeRules()
53
    {
54
        $rules = [
55
            ['invitee_guid', 'required'],
56
            ['invitee_guid', 'string'],
57
        ];
58
        if (!$this->allowRepeated) {
59
            $rules[] = [
60
                [$this->createdByAttribute, 'invitee_guid'], 'unique', 'targetAttribute' => [
61
                    $this->createdByAttribute, 'invitee_guid',
62
                ]
63
            ];
64
        }
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function rules()
71
    {
72
        return array_merge(parent::rules(), $this->getInviteeRules());
73
    }
74
75
    /**
76
     * @param User|string $invitee
77
     * @return string
78
     */
79
    public function setInvitee($invitee)
80
    {
81
        if ($invitee instanceof User) {
82
            $invitee = $invitee->getGUID();
83
        }
84
        return $this->invitee_guid = $invitee;
85
    }
86
87
    /**
88
     * @return User
89
     */
90
    public function getInvitee()
91
    {
92
        $userClass = $this->hostClass;
93
        return $userClass::findOne($this->invitee_guid);
94
    }
95
96
    /**
97
     * @param User|string $invitee
98
     * @return BaseBlameableQuery
99
     */
100
    public static function findByInvitee($invitee)
101
    {
102
        return static::find()->andWhere(['invitee_guid' => (string)$invitee]);
103
    }
104
105
    /**
106
     * @return string
107
     */
108 1
    public static function tableName()
109
    {
110 1
        return '{{%user_invitation}}';
111
    }
112
}
113