Completed
Push — master ( eec31e...9249bd )
by vistart
05:05
created

BaseBlameableModel::init()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 0
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\models;
14
15
use rhosocial\base\models\queries\BaseBlameableQuery;
16
use rhosocial\base\models\traits\BlameableTrait;
17
18
/**
19
 * BaseBlameableModel automatically fills the specified attributes with
20
 * the current user's GUID.
21
 * For example:<br/>
22
 * ~~~php
23
 * * @property string $comment
24
 * class Comment extends BaseBlameableModel
25
 * {
26
 *     public $contentAttribute = 'commment';
27
 *     public $contentAttributeRule = ['string', 'max' => 140];
28
 *     public static function tableName()
29
 *     {
30
 *         return <table_name>;
31
 *     }
32
 *     public function rules()
33
 *     {
34
 *         $rules = <Your Rules>;
35
 *         return array_merge(parent::rules(), $rules);
36
 *     }
37
 *     public function behaviors()
38
 *     {
39
 *         $behaviors = <Your Behaviors>;
40
 *         return array_merge(parent::behaviors(), $behaviors);
41
 *     }
42
 *     public function attributeLabels()
43
 *     {
44
 *         return [
45
 *             ...
46
 *         ];
47
 *     }
48
 * }
49
 * ~~~
50
 * Well, when you're signed-in, you can create and save a new `Comment` instance:
51
 * ~~~php
52
 * $comment = new Comment();
53
 * $comment->comment = 'New Comment.';
54
 * $comment->save();
55
 * ~~~
56
 * or update an existing one:
57
 * ~~~php
58
 * $comment = Comment::findByIdentity()->one();
59
 * if ($comment)
60
 * {
61
 *     $comment->comment = 'Updated Comment.';
62
 *     $comment->save();
63
 * }
64
 * ~~~
65
 * @property array createdByAttributeRules the whole validation rules of
66
 * creator attribute only, except of combination rules.
67
 * @property array updatedByAttributeRules the whole validation rules of
68
 * creator attribute only, except of combination rules.
69
 * @version 1.0
70
 * @author vistart <[email protected]>
71
 */
72
abstract class BaseBlameableModel extends BaseEntityModel
73
{
74
    use BlameableTrait;
75
76
    /**
77
     * Initialize the blameable model.
78
     * If query class is not specified, [[BaseBlameableQuery]] will be taken.
79
     */
80 12
    public function init()
81
    {
82 12
        if (!is_string($this->queryClass)) {
83 9
            $this->queryClass = BaseBlameableQuery::class;
84 9
        }
85 12
        if ($this->skipInit) {
86 9
            return;
87
        }
88 12
        $this->initBlameableEvents();
89 12
        parent::init();
90 12
    }
91
92
    /**
93
     * Get the query class with specified identity.
94
     * @param BaseUserModel $identity
95
     * @return BaseBlameableQuery
96
     */
97 4
    public static function findByIdentity($identity = null)
98
    {
99 4
        return static::find()->byIdentity($identity);
0 ignored issues
show
Documentation Bug introduced by
The method byIdentity does not exist on object<rhosocial\base\mo...ueries\BaseEntityQuery>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
100
    }
101
}
102