Completed
Push — master ( 81b65f...3c6407 )
by vistart
07:48
created

BaseMongoBlameableQuery::updatedBy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

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