Completed
Push — master ( 1cd5c1...a859d8 )
by vistart
12:32
created

BaseMongoBlameableQuery   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 54
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createdBy() 0 11 3
A updatedBy() 0 11 3
A byIdentity() 0 10 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\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 6
    public function createdBy($guid)
36
    {
37 6
        $model = $this->noInitModel;
38 6
        if (!is_string($model->createdByAttribute)) {
39
            return $this;
40
        }
41 6
        if ($guid instanceof BaseUserModel) {
42
            $guid = $guid->getGUID();
43
        }
44 6
        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 3
    public function updatedBy($guid)
53
    {
54 3
        $model = $this->noInitModel;
55 3
        if (!is_string($model->updatedByAttribute)) {
56
            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