Passed
Push — master ( da6aa6...8697ab )
by vistart
03:49
created

Invitation::findByInvitee()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 4
nop 1
crap 3
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 5
    public function getInviteeRules()
53
    {
54
        $rules = [
55 5
            ['invitee_guid', 'required'],
56
            ['invitee_guid', 'string'],
57
        ];
58 5
        if (!$this->allowRepeated) {
59 5
            $rules[] = [
60 5
                [$this->createdByAttribute, $this->contentAttribute, 'invitee_guid'], 'unique', 'targetAttribute' => [
61 5
                    $this->createdByAttribute, $this->contentAttribute, 'invitee_guid',
62
                ]
63
            ];
64
        }
65 5
        return $rules;
66
    }
67
68
    /**
69
     * @return array
70
     */
71 5
    public function rules()
72
    {
73 5
        return array_merge(parent::rules(), $this->getInviteeRules());
74
    }
75
76
    /**
77
     * @param User|string $invitee
78
     * @return string
79
     */
80 5
    public function setInvitee($invitee)
81
    {
82 5
        return $this->invitee_guid = (string)$invitee;
83
    }
84
85
    /**
86
     * @return User
87
     */
88 3
    public function getInvitee()
89
    {
90 3
        $userClass = $this->hostClass;
91 3
        return $userClass::findOne($this->invitee_guid);
92
    }
93
94
    /**
95
     * @param User|string|array $invitee
96
     * @return BaseBlameableQuery
97
     */
98 1
    public static function findByInvitee($invitee)
99
    {
100 1
        if (!is_array($invitee)) {
101 1
            $i[0] = $invitee;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$i was never initialized. Although not strictly required by PHP, it is generally a good practice to add $i = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
102 1
            $invitee = $i;
103
        }
104 1
        foreach ($invitee as $key => $i) {
105 1
            $invitee[$key] = (string)$i;
106
        }
107 1
        return static::find()->andWhere(['invitee_guid' => $invitee]);
108
    }
109
110
    /**
111
     * @return string
112
     */
113 6
    public static function tableName()
114
    {
115 6
        return '{{%user_invitation}}';
116
    }
117
}
118