Completed
Push — master ( a859d8...30587f )
by vistart
08:26
created

BaseMongoBlameableQuery::updatedBy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
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\queries;
14
15
use MongoDB\BSON\Binary;
16
use rhosocial\base\models\models\BaseUserModel;
17
use rhosocial\base\models\traits\BlameableQueryTrait;
18
use Yii;
19
20
/**
21
 * Description of BaseMongoBlameableQuery
22
 *
23
 * @version 1.0
24
 * @author vistart <[email protected]>
25
 */
26
class BaseMongoBlameableQuery extends BaseMongoEntityQuery
27
{
28
    use BlameableQueryTrait;
29
30
    /**
31
     * Specify creator(s).
32
     * @param string|array $guid
33
     * @return $this
34
     */
35 9
    public function createdBy($guid)
36
    {
37 9
        $model = $this->noInitModel;
38 9
        if (!is_string($model->createdByAttribute)) {
39
            return $this;
40
        }
41 9
        if ($guid instanceof BaseUserModel) {
42 3
            $guid = $guid->getGUID();
43
        }
44 9
        return $this->andWhere([$model->createdByAttribute => new Binary($guid, Binary::TYPE_UUID)]);
45
    }
46
47
    /**
48
     * Specify last updater(s).
49
     * @param string|array $guid
50
     * @return $this
51
     */
52 4
    public function updatedBy($guid)
53
    {
54 4
        $model = $this->noInitModel;
55 4
        if (!is_string($model->updatedByAttribute)) {
56 1
            return $this;
57
        }
58 3
        if ($guid instanceof BaseUserModel) {
59 3
            $guid = $guid->getGUID();
60
        }
61 3
        return $this->andWhere([$model->updatedByAttribute => new Binary($guid, Binary::TYPE_UUID)]);
62
    }
63
64
    /**
65
     * Attach current identity to createdBy condition.
66
     * @param BaseUserModel $identity
67
     * @return $this
68
     */
69 6
    public function byIdentity($identity = null)
70
    {
71 6
        if (!$identity) {
72 1
            $identity = Yii::$app->user->identity;
73
        }
74 6
        if (method_exists($identity, 'canGetProperty') && !$identity->canGetProperty('guid')) {
75
            return $this;
76
        }
77 6
        return $this->createdBy($identity->getGUID());
78
    }
79
}
80