MutualTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 61
ccs 21
cts 21
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getInitiator() 0 4 1
A getMutualRules() 0 11 2
A getRecipient() 0 9 3
A setRecipient() 0 11 4
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