Completed
Push — master ( 113cf9...315017 )
by vistart
05:37
created

MutualQueryTrait::initiators()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
crap 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
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 BaseUserModel|string $user initiator
29
     * @param BaseUserModel|string $other recipient.
30
     * @param Connection $database
31
     * @return 
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
        if ($user instanceof BaseUserModel) {
37 1
            $user = $user->getGUID();
38
        }
39 1
        if ($other instanceof BaseUserModel) {
40 1
            $other = $other->getGUID();
41
        }
42 1
        return $this->andWhere([$model->createdByAttribute => $other, $model->otherGuidAttribute => $user])->one($database);
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...
43
    }
44
45
    /**
46
     * Get all the opposites.
47
     * @param string $user initator.
48
     * @param array $others all recipients.
49
     * @param Connection $database
50
     * @return array instances.
51
     */
52 1
    public function opposites($user, $others = [], $database = null)
53
    {
54 1
        $model = $this->noInitModel;
55 1
        if ($user instanceof BaseUserModel) {
56 1
            $user = $user->getGUID();
57
        }
58 1
        $query = $this->andWhere([$model->otherGuidAttribute => $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...
59 1
        if (!empty($others)) {
60 1
            if ($others instanceof BaseUserModel) {
61 1
                $others = [$others->getGUID()];
62 1
            } elseif (is_array($others)) {
63 1
                $others = BaseUserModel::compositeGUIDs($others);
64
            }
65 1
            $query = $query->andWhere([$model->createdByAttribute => array_values($others)]);
66
        }
67 1
        return $query->all($database);
68
    }
69
70
    /**
71
     * Specify initiators.
72
     * @param string|array $users the guid of initiator if string, or guid array
73
     * of initiators if array.
74
     * @return \static $this
75
     */
76 19
    public function initiators($users = [])
77
    {
78 19
        if (empty($users)) {
79 1
            return $this;
80
        }
81 19
        $model = $this->noInitModel;
82 19
        if ($users instanceof BaseUserModel) {
83 19
            $users = $users->getGUID();
84 1
        } elseif (is_array($users)) {
85 1
            $users = BaseUserModel::compositeGUIDs($users);
86
        }
87 19
        return $this->andWhere([$model->createdByAttribute => $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...
88
    }
89
90
    /**
91
     * Specify recipients.
92
     * @param string|array $users the guid of recipient if string, or guid array
93
     * of recipients if array.
94
     * @return \static $this
95
     */
96 19
    public function recipients($users = [])
97
    {
98 19
        if (empty($users)) {
99 1
            return $this;
100
        }
101 19
        $model = $this->noInitModel;
102 19
        if ($users instanceof BaseUserModel) {
103 19
            $users = $users->getGUID();
104 1
        } elseif (is_array($users)) {
105 1
            $users = BaseUserModel::compositeGUIDs($users);
106
        }
107 19
        return $this->andWhere([$model->otherGuidAttribute => $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...
108
    }
109
}
110