Completed
Push — master ( 9b5529...8e3c70 )
by vistart
09:42 queued 03:37
created

BlameableQueryTrait::content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
/**
16
 * This trait is used for building blameable query class for blameable model,
17
 * which would be attached three conditions.
18
 * For example:
19
 * ```php
20
 * class BlameableQuery {
21
 *     use BlameableQueryTrait;
22
 * }
23
 * ```
24
 *
25
 * @version 2.0
26
 * @author vistart <[email protected]>
27
 */
28
trait BlameableQueryTrait
29
{
30
    use QueryTrait;
31
32
    /**
33
     * Specify confirmation.
34
     * @param boolean $isConfirmed
35
     * @return $this
36
     */
37
    public function confirmed($isConfirmed = true)
38
    {
39
        $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...
40
        if (!is_string($model->confirmationAttribute)) {
41
            return $this;
42
        }
43
        return $this->andWhere([$model->confirmationAttribute => $isConfirmed]);
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...
44
    }
45
46
    /**
47
     * Specify content.
48
     * @param mixed $content
49
     * @param false|string $like false, 'like', 'or like', 'not like', 'or not like'.
50
     * @return $this
51
     */
52 3
    public function content($content, $like = false)
53
    {
54 3
        $model = $this->noInitModel;
55 3
        return $this->likeCondition($content, $model->contentAttribute, $like);
56
    }
57
58
    /**
59
     * Specify parent.
60
     * @param array|string $guid parent guid or array of them. non-parent if
61
     * empty. If you don't want to specify parent, please do not access this
62
     * method.
63
     * @return $this
64
     */
65
    public function parentGuid($guid)
66
    {
67
        $model = $this->noInitModel;
68
        if (!is_string($model->parentAttribute)) {
69
            return $this;
70
        }
71
        if (empty($guid)) {
72
            return $this->andWhere([$model->parentAttribute => '']);
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...
73
        }
74
        return $this->andWhere([$model->parentAttribute => $guid]);
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...
75
    }
76
77
    /**
78
     * Specify creator(s).
79
     * @param string|array $guid
80
     * @return $this
81
     */
82 17
    public function createdBy($guid)
83
    {
84 17
        $model = $this->noInitModel;
85 17
        if (!is_string($model->createdByAttribute)) {
86
            return $this;
87
        }
88 17
        if ($guid instanceof \vistart\Models\models\BaseUserModel) {
89 8
            $guid = $guid->guid;
90 8
        }
91 17
        return $this->andWhere([$model->createdByAttribute => $guid]);
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...
92
    }
93
94
    /**
95
     * Specify last updater(s).
96
     * @param string|array $guid
97
     * @return $this
98
     */
99
    public function updatedBy($guid)
100
    {
101
        $model = $this->noInitModel;
102
        if (!is_string($model->updatedByAttribute)) {
103
            return $this;
104
        }
105
        if ($guid instanceof \vistart\Models\models\BaseUserModel) {
106
            $guid = $guid->guid;
107
        }
108
        return $this->andWhere([$model->updatedByAttribute => $guid]);
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...
109
    }
110
111
    /**
112
     * Attach current identity to createdBy condition.
113
     * @param \vistart\Models\models\BaseUserModel $identity
114
     * @return $this
115
     */
116 9
    public function byIdentity($identity = null)
117
    {
118 9
        if (!$identity) {
119
            $identity = \Yii::$app->user->identity;
120
        }
121 9
        if (!$identity || !$identity->canGetProperty('guid')) {
122
            return $this;
123
        }
124 9
        return $this->createdBy($identity->guid);
125
    }
126
}
127