Completed
Push — master ( eb8d71...5983eb )
by vistart
08:59
created

BaseMongoMessageModel::getOtherAttribute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 3
eloc 3
nc 4
nop 0
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\base\models\models;
14
15
use MongoDB\BSON\Binary;
16
use rhosocial\base\models\queries\BaseMongoMessageQuery;
17
use rhosocial\base\models\queries\BaseUserQuery;
18
use rhosocial\base\models\traits\MessageTrait;
19
20
/**
21
 * @version 1.0
22
 * @author vistart <[email protected]>
23
 */
24
abstract class BaseMongoMessageModel extends BaseMongoBlameableModel
25
{
26
    use MessageTrait;
27
    
28
    public $updatedAtAttribute = false;
29
    public $updatedByAttribute = false;
30
    public $expiredAt = 604800; // 7 days.
31
    
32 13
    public function init()
33
    {
34 13
        if (!is_string($this->queryClass) || empty($this->queryClass)) {
35 13
            $this->queryClass = BaseMongoMessageQuery::class;
36
        }
37 13
        if ($this->skipInit) {
38 13
            return;
39
        }
40 13
        $this->initMessageEvents();
41 13
        parent::init();
42 13
    }
43
44
    /**
45
     * Get recipient.
46
     * @return BaseUserQuery
47
     */
48 2
    public function getRecipient()
49
    {
50 2
        if (!is_string($this->otherGuidAttribute) || empty($this->otherGuidAttribute)) {
51 1
            throw new \yii\base\InvalidConfigException('Recipient GUID Attribute Not Specified.');
52
        }
53 1
        $hostClass = $this->hostClass;
54 1
        $model = $hostClass::buildNoInitModel();
55 1
        return $this->hasOne($hostClass::className(), [$model->guidAttribute => 'otherAttribute']);
56
    }
57
    
58
    /**
59
     * Get updated_by attribute.
60
     * @return string|null
61
     */
62 1
    public function getOtherAttribute()
63
    {
64 1
        $otherGuidAttribute = $this->otherGuidAttribute;
65 1
        return (!is_string($otherGuidAttribute) || empty($otherGuidAttribute)) ? null : $this->$otherGuidAttribute->getData();
66
    }
67
68
    /**
69
     * Set recipient.
70
     * @param BaseUserModel $user
71
     * @return string
72
     */
73 13
    public function setRecipient($user)
74
    {
75 13
        if (!is_string($this->otherGuidAttribute) || empty($this->otherGuidAttribute)) {
76 1
            throw new \yii\base\InvalidConfigException('Recipient GUID Attribute Not Specified.');
77
        }
78 13
        if ($user instanceof BaseUserModel) {
79 13
            $user = $user->getGUID();
80
        }
81 13
        $otherGuidAttribute = $this->otherGuidAttribute;
82 13
        return $this->$otherGuidAttribute = new Binary($user, Binary::TYPE_UUID);
83
    }
84
85
    /**
86
     * Get mutual attributes rules.
87
     * @return array
88
     */
89 13
    public function getMutualRules()
90
    {
91 13
        $rules = [];
92 13
        if (is_string($this->otherGuidAttribute)) {
93
            $rules = [
94 13
                [$this->otherGuidAttribute, 'required'],
95
            ];
96
        }
97 13
        return $rules;
98
    }
99
}