Completed
Push — master ( 81b65f...3c6407 )
by vistart
07:48
created

BaseMongoMessageModel::getMutualRules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
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\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 $updatedByAttribute = false;
29
    public $expiredAt = 604800; // 7 days.
30
    
31 2
    public function init()
32
    {
33 2
        if (!is_string($this->queryClass) || empty($this->queryClass)) {
34 2
            $this->queryClass = BaseMongoMessageQuery::class;
35
        }
36 2
        if ($this->skipInit) {
37 2
            return;
38
        }
39 2
        $this->initMessageEvents();
40 2
        parent::init();
41 2
    }
42
43
    /**
44
     * Get recipient.
45
     * @return BaseUserQuery
46
     */
47
    public function getRecipient()
48
    {
49
        if (!is_string($this->otherGuidAttribute) || empty($this->otherGuidAttribute)) {
50
            return null;
51
        }
52
        $hostClass = $this->hostClass;
53
        $model = $hostClass::buildNoInitModel();
54
        return $this->hasOne($hostClass::className(), [$model->guidAttribute => 'otherAttribute']);
55
    }
56
    
57
    /**
58
     * Get updated_by attribute.
59
     * @return string|null
60
     */
61
    public function getOtherAttribute()
62
    {
63
        $updatedByAttribute = $this->updatedByAttribute;
64
        return (!is_string($updatedByAttribute) || empty($updatedByAttribute)) ? null : $this->$updatedByAttribute->getData();
65
    }
66
67
    /**
68
     * Set recipient.
69
     * @param BaseUserModel $user
70
     * @return string
71
     */
72 2
    public function setRecipient($user)
73
    {
74 2
        if (!is_string($this->otherGuidAttribute) || empty($this->otherGuidAttribute)) {
75
            return null;
76
        }
77 2
        if ($user instanceof BaseUserModel) {
78 2
            $user = $user->getGUID();
79
        }
80 2
        $otherGuidAttribute = $this->otherGuidAttribute;
81 2
        return $this->$otherGuidAttribute = new Binary($user, Binary::TYPE_UUID);
82
    }
83
84
    /**
85
     * Get mutual attributes rules.
86
     * @return array
87
     */
88 2
    public function getMutualRules()
89
    {
90 2
        $rules = [];
91 2
        if (is_string($this->otherGuidAttribute)) {
92
            $rules = [
93 2
                [$this->otherGuidAttribute, 'required'],
94
            ];
95
        }
96 2
        return $rules;
97
    }
98
}