MutualQueryTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initiators() 0 8 2
A recipients() 0 8 2
A opposite() 0 7 1
A opposites() 0 10 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\traits;
14
15
use rhosocial\base\models\models\BaseUserModel;
16
17
/**
18
 * This trait is used for building query class which contains mutual relation operations.
19
 *
20
 * @version 1.0
21
 * @author vistart <[email protected]>
22
 */
23
trait MutualQueryTrait
24
{
25
26
    /**
27
     * Get the opposite relation.
28
     * @param array|BaseUserModel|string $user initiator
29
     * @param array|BaseUserModel|string $other recipient.
30
     * @param Connection $database
31
     * @return mixed It's type depends on {$model->class}.
32
     */
33 1
    public function opposite($user, $other, $database = null)
34
    {
35 1
        $model = $this->noInitModel;
0 ignored issues
show
Bug introduced by
The property noInitModel 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...
36 1
        return $this->andWhere(
0 ignored issues
show
Bug introduced by
It seems like andWhere() 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...
37 1
            [$model->createdByAttribute => BaseUserModel::compositeGUIDs($other),
38 1
        $model->otherGuidAttribute => BaseUserModel::compositeGUIDs($user)])->one($database);
39
    }
40
41
    /**
42
     * Get all the opposites.
43
     * @param array|string $user initator.
44
     * @param array $others all recipients.
45
     * @param Connection $database
46
     * @return array instances.
47
     */
48 1
    public function opposites($user, $others = [], $database = null)
49
    {
50 1
        $model = $this->noInitModel;
51 1
        $query = $this->andWhere([$model->otherGuidAttribute => BaseUserModel::compositeGUIDs($user)]);
0 ignored issues
show
Bug introduced by
It seems like andWhere() 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...
52
        /* @var $query static */
53 1
        if (!empty($others)) {
54 1
            $query = $query->andWhere([$model->createdByAttribute => BaseUserModel::compositeGUIDs($others)]);
0 ignored issues
show
Bug introduced by
It seems like andWhere() 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...
55
        }
56 1
        return $query->all($database);
57
    }
58
59
    /**
60
     * Specify initiators.
61
     * @param string|array $users the guid of initiator if string, or guid array
62
     * of initiators if array.
63
     * @return static $this
64
     */
65 49
    public function initiators($users = [])
66
    {
67 49
        if (empty($users)) {
68 1
            return $this;
69
        }
70 49
        $model = $this->noInitModel;
71 49
        return $this->andWhere([$model->createdByAttribute => BaseUserModel::compositeGUIDs($users)]);
0 ignored issues
show
Bug introduced by
It seems like andWhere() 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...
72
    }
73
74
    /**
75
     * Specify recipients.
76
     * @param string|array $users the guid of recipient if string, or guid array
77
     * of recipients if array.
78
     * @return static $this
79
     */
80 51
    public function recipients($users = [])
81
    {
82 51
        if (empty($users)) {
83 1
            return $this;
84
        }
85 51
        $model = $this->noInitModel;
86 51
        return $this->andWhere([$model->otherGuidAttribute => BaseUserModel::compositeGUIDs($users)]);
0 ignored issues
show
Bug introduced by
It seems like andWhere() 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...
87
    }
88
}
89