MutualTrait::getRecipient()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 3
eloc 6
nc 2
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\traits;
14
15
use rhosocial\base\models\models\BaseUserModel;
16
use rhosocial\base\models\queries\BaseUserQuery;
17
18
/**
19
 * This trait defines two roles: initiator and recipient.
20
 * The initiator is also the owner of this model.
21
 *
22
 * @property-read mixed $initiator
23
 * @property mixed $recipient
24
 * @property-read array $mutualRules
25
 * @version 1.0
26
 * @author vistart <[email protected]>
27
 */
28
trait MutualTrait
29
{
30
31
    public $otherGuidAttribute = 'other_guid';
32
33
    /**
34
     * Get initiator.
35
     * @return BaseUserQuery
36
     */
37 17
    public function getInitiator()
38
    {
39 17
        return $this->getHost();
0 ignored issues
show
Bug introduced by
It seems like getHost() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
40
    }
41
42
    /**
43
     * Get recipient.
44
     * @return BaseUserQuery
45
     */
46 18
    public function getRecipient()
47
    {
48 18
        if (!is_string($this->otherGuidAttribute) || empty($this->otherGuidAttribute)) {
49 1
            throw new \yii\base\InvalidConfigException('Recipient GUID Attribute Not Specified.');
50
        }
51 17
        $hostClass = $this->hostClass;
0 ignored issues
show
Bug introduced by
The property hostClass does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52 17
        $model = $hostClass::buildNoInitModel();
53 17
        return $this->hasOne($hostClass::className(), [$model->guidAttribute => $this->otherGuidAttribute]);
0 ignored issues
show
Bug introduced by
It seems like hasOne() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
    }
55
56
    /**
57
     * Set recipient.
58
     * @param BaseUserModel $user
59
     * @return string
60
     */
61 56
    public function setRecipient($user)
62
    {
63 56
        if (!is_string($this->otherGuidAttribute) || empty($this->otherGuidAttribute)) {
64 1
            throw new \yii\base\InvalidConfigException('Recipient GUID Attribute Not Specified.');
65
        }
66 56
        if ($user instanceof BaseUserModel) {
67 7
            $user = $user->getGUID();
68
        }
69 56
        $otherGuidAttribute = $this->otherGuidAttribute;
70 56
        return $this->$otherGuidAttribute = $user;
71
    }
72
73
    /**
74
     * Get mutual attributes rules.
75
     * @return array
76
     */
77 53
    public function getMutualRules()
78
    {
79 53
        $rules = [];
80 53
        if (is_string($this->otherGuidAttribute)) {
81
            $rules = [
82 53
                [$this->otherGuidAttribute, 'required'],
83 53
                [$this->otherGuidAttribute, 'string', 'max' => 16],
84
            ];
85
        }
86 53
        return $rules;
87
    }
88
}
89