BaseBlameableModel::init()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
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\queries\BaseEntityQuery;
17
use rhosocial\base\models\traits\BlameableTrait;
18
19
/**
20
 * BaseBlameableModel automatically fills the specified attributes with
21
 * the current user's GUID.
22
 * For example:<br/>
23
 * ~~~php
24
 * * @property string $comment
25
 * class Comment extends BaseBlameableModel
26
 * {
27
 *     public $contentAttribute = 'commment';
28
 *     public $contentAttributeRule = ['string', 'max' => 140];
29
 *     public static function tableName()
30
 *     {
31
 *         return <table_name>;
32
 *     }
33
 *     public function rules()
34
 *     {
35
 *         $rules = <Your Rules>;
36
 *         return array_merge(parent::rules(), $rules);
37
 *     }
38
 *     public function behaviors()
39
 *     {
40
 *         $behaviors = <Your Behaviors>;
41
 *         return array_merge(parent::behaviors(), $behaviors);
42
 *     }
43
 *     public function attributeLabels()
44
 *     {
45
 *         return [
46
 *             ...
47
 *         ];
48
 *     }
49
 *     
50
 *     // You should specify the `hostClass` property.
51
 *     public $hostClass = User::class;
52
 * }
53
 * ~~~
54
 * Well, when you're signed-in, you can create and save a new `Comment` instance:
55
 * ~~~php
56
 * $comment = new Comment();
57
 * $comment->comment = 'New Comment.';
58
 * $comment->save();
59
 * ~~~
60
 * or update an existing one:
61
 * ~~~php
62
 * $comment = Comment::findByIdentity()->one();
63
 * if ($comment)
64
 * {
65
 *     $comment->comment = 'Updated Comment.';
66
 *     $comment->save();
67
 * }
68
 * ~~~
69
 * @property array createdByAttributeRules the whole validation rules of
70
 * creator attribute only, except of combination rules.
71
 * @property array updatedByAttributeRules the whole validation rules of
72
 * creator attribute only, except of combination rules.
73
 * @version 1.0
74
 * @author vistart <[email protected]>
75
 */
76
abstract class BaseBlameableModel extends BaseEntityModel
77
{
78
    use BlameableTrait;
79
80
    /**
81
     * Initialize the blameable model.
82
     * If query class is not specified, [[BaseBlameableQuery]] will be taken.
83
     * Note: You must override this method and specify your own user class before
84
     * execute the parent one.
85
     */
86 126
    public function init()
87
    {
88 126
        if (!is_string($this->queryClass)) {
89 71
            $this->queryClass = BaseBlameableQuery::class;
90
        }
91 126
        if ($this->skipInit) {
92 66
            return;
93
        }
94 126
        $this->initBlameableEvents();
95 126
        parent::init();
96 126
    }
97
    
98
    /**
99
     * Return the query instance.
100
     * This method is provided for friendly to IDE.
101
     * @return BaseBlameableQuery|BaseEntityQuery
102
     */
103 121
    public static function find()
104
    {
105 121
        return parent::find();
106
    }
107
108
    /**
109
     * Get the query class with specified identity.
110
     * @param BaseUserModel $identity
111
     * @return BaseBlameableQuery
112
     */
113 4
    public static function findByIdentity($identity = null)
114
    {
115 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...
116
    }
117
}
118