Completed
Push — master ( 64430c...1d94a7 )
by vistart
05:26
created

BlameableTrait::onContentChanged()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link http://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license http://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
use yii\behaviors\BlameableBehavior;
16
use yii\caching\TagDependency;
17
18
/**
19
 * 该 Trait 用于处理属于用户的实例。包括以下功能:
20
 * 1.单列内容;多列内容待定;
21
 * 2.内容类型;具体类型应当自定义;
22
 * 3.内容规则;自动生成;
23
 * 4.归属用户 GUID;
24
 * 5.创建用户 GUID;
25
 * 6.上次更新用户 GUID;
26
 * 7.确认功能由 ConfirmationTrait 提供;
27
 * 8.实例功能由 EntityTrait 提供。
28
 * @property-read array $blameableAttributeRules Get all rules associated with
29
 * blameable.
30
 * @property array $blameableRules Get or set all the rules associated with
31
 * creator, updater, content and its ID, as well as all the inherited rules.
32
 * @property array $blameableBehaviors Get or set all the behaviors assoriated
33
 * with creator and updater, as well as all the inherited behaviors.
34
 * @property-read array $descriptionRules Get description property rules.
35
 * @property-read mixed $content Content.
36
 * @property-read boolean $contentCanBeEdited Whether this content could be edited.
37
 * @property-read array $contentRules Get content rules.
38
 * @property-read \vistart\Models\models\BaseUserModel $user
39
 * @property-read \vistart\Models\models\BaseUserModel $updater
40
 * @version 2.0
41
 * @author vistart <[email protected]>
42
 */
43
trait BlameableTrait
44
{
45
    use ConfirmationTrait,
46
        SelfBlameableTrait;
47
48
    private $blameableLocalRules = [];
49
    private $blameableLocalBehaviors = [];
50
51
    /**
52
     * @var boolean|string|array Specify the attribute(s) name of content(s). If
53
     * there is only one content attribute, you can assign its name. Or there
54
     * is multiple attributes associated with contents, you can assign their
55
     * names in array. If you don't want to use this feature, please assign
56
     * false.
57
     * For example:
58
     * ```php
59
     * public $contentAttribute = 'comment'; // only one field named as 'comment'.
60
     * ```
61
     * or
62
     * ```php
63
     * public $contentAttribute = ['year', 'month', 'day']; // multiple fields.
64
     * ```
65
     * or
66
     * ```php
67
     * public $contentAttribute = false; // no need of this feature.
68
     * ```
69
     * If you don't need this feature, you should add rules corresponding with
70
     * `content` in `rules()` method of your user model by yourself.
71
     */
72
    public $contentAttribute = 'content';
73
74
    /**
75
     * @var array built-in validator name or validatation method name and
76
     * additional parameters.
77
     */
78
    public $contentAttributeRule = ['string', 'max' => 255];
79
80
    /**
81
     * @var boolean|string Specify the field which stores the type of content.
82
     */
83
    public $contentTypeAttribute = false;
84
85
    /**
86
     * @var boolean|array Specify the logic type of content, not data type. If
87
     * your content doesn't need this feature. please specify false. If the
88
     * $contentAttribute is specified to false, this attribute will be skipped.
89
     * ```php
90
     * public $contentTypes = [
91
     *     'public',
92
     *     'private',
93
     *     'friend',
94
     * ];
95
     * ```
96
     */
97
    public $contentTypes = false;
98
99
    /**
100
     * @var boolean|string This attribute speicfy the name of description
101
     * attribute. If this attribute is assigned to false, this feature will be
102
     * skipped.
103
     */
104
    public $descriptionAttribute = false;
105
106
    /**
107
     * @var string
108
     */
109
    public $initDescription = '';
110
111
    /**
112
     * @var string the attribute that will receive current user ID value. This
113
     * attribute must be assigned.
114
     */
115
    public $createdByAttribute = "user_guid";
116
117
    /**
118
     * @var string the attribute that will receive current user ID value.
119
     * Set this property to false if you do not want to record the updater ID.
120
     */
121
    public $updatedByAttribute = "user_guid";
122
123
    /**
124
     * @var boolean Add combinated unique rule if assigned to true.
125
     */
126
    public $idCreatorCombinatedUnique = true;
127
128
    /**
129
     * @var boolean|string The name of user class which own the current entity.
130
     * If this attribute is assigned to false, this feature will be skipped, and
131
     * when you use create() method of UserTrait, it will be assigned with
132
     * current user class.
133
     */
134
    public $userClass;
135
    public static $cacheKeyBlameableRules = 'blameable_rules';
136
    public static $cacheTagBlameableRules = 'tag_blameable_rules';
137
    public static $cacheKeyBlameableBehaviors = 'blameable_behaviors';
138
    public static $cacheTagBlameableBehaviors = 'tag_blameable_behaviors';
139
140
    /**
141
     * @inheritdoc
142
     */
143 25
    public function rules()
144
    {
145 25
        return $this->getBlameableRules();
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151 25
    public function behaviors()
152
    {
153 25
        return $this->getBlameableBehaviors();
154
    }
155
156
    /**
157
     * Get total of contents which owned by their owner.
158
     * @return integer
159
     */
160 2
    public function count()
161
    {
162 2
        $createdByAttribute = $this->createdByAttribute;
163 2
        return static::find()->where([$createdByAttribute => $this->$createdByAttribute])->count();
164
    }
165
166
    /**
167
     * Get content.
168
     * @return mixed
169
     */
170
    public function getContent()
171
    {
172
        $contentAttribute = $this->contentAttribute;
173
        if ($contentAttribute === false) {
174
            return null;
175
        }
176
        if (is_array($contentAttribute)) {
177
            $content = [];
178
            foreach ($contentAttribute as $key => $value) {
179
                $content[$key] = $this->$value;
180
            }
181
            return $content;
182
        }
183
        return $this->$contentAttribute;
184
    }
185
186
    /**
187
     * Set content.
188
     * @param mixed $content
189
     */
190
    public function setContent($content)
191
    {
192
        $contentAttribute = $this->contentAttribute;
193
        if ($contentAttribute === false) {
194
            return;
195
        }
196
        if (is_array($contentAttribute)) {
197
            foreach ($contentAttribute as $key => $value) {
198
                $this->$value = $content[$key];
199
            }
200
            return;
201
        }
202
        $this->$contentAttribute = $content;
203
    }
204
205
    /**
206
     * Determines whether content could be edited. Your should implement this
207
     * method by yourself.
208
     * @return boolean
209
     * @throws \yii\base\NotSupportedException
210
     */
211
    public function getContentCanBeEdited()
212
    {
213
        if ($this->contentAttribute === false) {
214
            return false;
215
        }
216
        throw new \yii\base\NotSupportedException("This method is not implemented.");
217
    }
218
219
    /**
220
     * Check it has been ever edited.
221
     * @return boolean Whether this content has ever been edited.
222
     */
223
    public function hasEverEdited()
224
    {
225
        $createdAtAttribute = $this->createdByAttribute;
226
        $updatedAtAttribute = $this->updatedByAttribute;
227
        if (!$createdAtAttribute || !$updatedAtAttribute) {
228
            return false;
229
        }
230
        return $this->$createdAtAttribute === $this->$updatedAtAttribute;
231
    }
232
233
    /**
234
     * Get blameable rules cache key.
235
     * @return string cache key.
236
     */
237 25
    public function getBlameableRulesCacheKey()
238
    {
239 25
        return static::className() . $this->cachePrefix . static::$cacheKeyBlameableRules;
0 ignored issues
show
Bug introduced by
The property cachePrefix does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
240
    }
241
242
    /**
243
     * Get blameable rules cache tag.
244
     * @return string cache tag
245
     */
246 6
    public function getBlameableRulesCacheTag()
247
    {
248 6
        return static::className() . $this->cachePrefix . static::$cacheTagBlameableRules;
249
    }
250
251
    /**
252
     * Get the rules associated with content to be blamed.
253
     * @return array rules.
254
     */
255 25
    public function getBlameableRules()
256
    {
257 25
        $cache = $this->getCache();
0 ignored issues
show
Bug introduced by
It seems like getCache() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
258 25
        if ($cache) {
259 25
            $this->blameableLocalRules = $cache->get($this->getBlameableRulesCacheKey());
260 25
        }
261
        // 若当前规则不为空,且是数组,则认为是规则数组,直接返回。
262 25
        if (!empty($this->blameableLocalRules) && is_array($this->blameableLocalRules)) {
263 22
            return $this->blameableLocalRules;
264
        }
265
266
        // 父类规则与确认规则合并。
267 6
        if ($cache) {
268 6
            TagDependency::invalidate($cache, [$this->getEntityRulesCacheTag()]);
0 ignored issues
show
Bug introduced by
It seems like getEntityRulesCacheTag() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
269 6
        }
270 6
        $rules = array_merge(
271 6
            parent::rules(),
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (rules() instead of getBlameableRules()). Are you sure this is correct? If so, you might want to change this to $this->rules().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
272 6
            $this->getConfirmationRules(),
273 6
            $this->getBlameableAttributeRules(),
274 6
            $this->getDescriptionRules(),
275 6
            $this->getContentRules(),
276 6
            $this->getSelfBlameableRules()
277 6
        );
278 6
        $this->setBlameableRules($rules);
279 6
        return $this->blameableLocalRules;
280
    }
281
282
    /**
283
     * Get the rules associated with `createdByAttribute`, `updatedByAttribute`
284
     * and `idAttribute`-`createdByAttribute` combination unique.
285
     * @return array rules.
286
     */
287 6
    public function getBlameableAttributeRules()
288
    {
289 6
        $rules = [];
290
        // 创建者和上次修改者由 BlameableBehavior 负责,因此标记为安全。
291 6
        if (!is_string($this->createdByAttribute) || empty($this->createdByAttribute)) {
292
            throw new \yii\base\NotSupportedException('You must assign the creator.');
293
        }
294 6
        $rules[] = [
295 6
            [$this->createdByAttribute],
296 6
            'safe',
297
        ];
298
299 6
        if (is_string($this->updatedByAttribute) && !empty($this->updatedByAttribute)) {
300 1
            $rules[] = [
301 1
                [$this->updatedByAttribute],
302 1
                'safe',
303
            ];
304 1
        }
305
306 6
        if ($this->idCreatorCombinatedUnique && is_string($this->idAttribute)) {
0 ignored issues
show
Bug introduced by
The property idAttribute does not seem to exist. Did you mean refIdAttribute?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
307 3
            $rules [] = [
308 3
                [$this->idAttribute,
0 ignored issues
show
Bug introduced by
The property idAttribute does not seem to exist. Did you mean refIdAttribute?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
309 3
                    $this->createdByAttribute],
310 3
                'unique',
311 3
                'targetAttribute' => [$this->idAttribute,
0 ignored issues
show
Bug introduced by
The property idAttribute does not seem to exist. Did you mean refIdAttribute?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
312 3
                    $this->createdByAttribute],
313
            ];
314 3
        }
315 6
        return $rules;
316
    }
317
318
    /**
319
     * Get the rules associated with `description` attribute.
320
     * @return array rules.
321
     */
322 6
    public function getDescriptionRules()
323
    {
324 6
        $rules = [];
325 6
        if (is_string($this->descriptionAttribute) && !empty($this->descriptionAttribute)) {
326 1
            $rules[] = [
327 1
                [$this->descriptionAttribute],
328
                'string'
329 1
            ];
330 1
            $rules[] = [
331 1
                [$this->descriptionAttribute],
332 1
                'default',
333 1
                'value' => $this->initDescription,
334
            ];
335 1
        }
336 6
        return $rules;
337
    }
338
339
    /**
340
     * Get the rules associated with `content` and `contentType` attributes.
341
     * @return array rules.
342
     */
343 6
    public function getContentRules()
344
    {
345 6
        if (!$this->contentAttribute) {
346 2
            return [];
347
        }
348 4
        $rules = [];
349 4
        $rules[] = [[
350 4
            $this->contentAttribute],
351 4
            'required'];
352 4
        if ($this->contentAttributeRule) {
353 4
            if (is_string($this->contentAttributeRule)) {
354
                $this->contentAttributeRule = [$this->contentAttributeRule];
355
            }
356 4
            if (is_array($this->contentAttributeRule)) {
357 4
                $rules[] = array_merge([$this->contentAttribute], $this->contentAttributeRule);
358 4
            }
359 4
        }
360
361 4
        if (!$this->contentTypeAttribute) {
362 2
            return $rules;
363
        }
364
365 2
        if (is_array($this->contentTypes) && !empty($this->contentTypes)) {
366 2
            $rules[] = [[
367 2
                $this->contentTypeAttribute],
368 2
                'required'];
369 2
            $rules[] = [[
370 2
                $this->contentTypeAttribute],
371 2
                'in',
372 2
                'range' => array_keys($this->contentTypes)];
373 2
        }
374 2
        return $rules;
375
    }
376
377
    /**
378
     * Set blameable rules.
379
     * @param array $rules
380
     */
381 6
    protected function setBlameableRules($rules = [])
382
    {
383 6
        $this->blameableLocalRules = $rules;
384 6
        $cache = $this->getCache();
0 ignored issues
show
Bug introduced by
It seems like getCache() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
385 6
        if ($cache) {
386 6
            $tagDependency = new \yii\caching\TagDependency(['tags' => [$this->getBlameableRulesCacheTag()]]);
387 6
            $cache->set($this->getBlameableRulesCacheKey(), $rules, 0, $tagDependency);
388 6
        }
389 6
    }
390
391
    /**
392
     * Get blameable behaviors cache key.
393
     * @return string cache key.
394
     */
395 25
    public function getBlameableBehaviorsCacheKey()
396
    {
397 25
        return static::className() . $this->cachePrefix . static::$cacheKeyBlameableBehaviors;
398
    }
399
400
    /**
401
     * Get blameable behaviors cache tag.
402
     * @return string cache tag.
403
     */
404 6
    public function getBlameableBehaviorsCacheTag()
405
    {
406 6
        return static::className() . $this->cachePrefix . static::$cacheTagBlameableBehaviors;
407
    }
408
409
    /**
410
     * Get blameable behaviors. If current behaviors array is empty, the init
411
     * array will be given.
412
     * @return array
413
     */
414 25
    public function getBlameableBehaviors()
415
    {
416 25
        $cache = $this->getCache();
0 ignored issues
show
Bug introduced by
It seems like getCache() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
417 25
        if ($cache) {
418 25
            $this->blameableLocalBehaviors = $cache->get($this->getBlameableBehaviorsCacheKey());
419 25
        }
420 25
        if (empty($this->blameableLocalBehaviors) || !is_array($this->blameableLocalBehaviors)) {
421 6
            if ($cache) {
422 6
                TagDependency::invalidate($cache, [$this->getEntityBehaviorsCacheTag()]);
0 ignored issues
show
Bug introduced by
The method getEntityBehaviorsCacheTag() does not exist on vistart\Models\traits\BlameableTrait. Did you maybe mean behaviors()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
423 6
            }
424 6
            $behaviors = parent::behaviors();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (behaviors() instead of getBlameableBehaviors()). Are you sure this is correct? If so, you might want to change this to $this->behaviors().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
425 6
            $behaviors['blameable'] = [
426 6
                'class' => BlameableBehavior::className(),
427 6
                'createdByAttribute' => $this->createdByAttribute,
428 6
                'updatedByAttribute' => $this->updatedByAttribute,
429 6
                'value' => [$this,
430 6
                    'onGetCurrentUserGuid'],
431
            ];
432 6
            $this->setBlameableBehaviors($behaviors);
433 6
        }
434 25
        return $this->blameableLocalBehaviors;
435
    }
436
437
    /**
438
     * Set blameable behaviors.
439
     * @param array $behaviors
440
     */
441 6
    protected function setBlameableBehaviors($behaviors = [])
442
    {
443 6
        $this->blameableLocalBehaviors = $behaviors;
444 6
        $cache = $this->getCache();
0 ignored issues
show
Bug introduced by
It seems like getCache() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
445 6
        if ($cache) {
446 6
            $tagDependencyConfig = ['tags' => [$this->getBlameableBehaviorsCacheTag()]];
447 6
            $tagDependency = new \yii\caching\TagDependency($tagDependencyConfig);
448 6
            $cache->set($this->getBlameableBehaviorsCacheKey(), $behaviors, 0, $tagDependency);
449 6
        }
450 6
    }
451
452
    /**
453
     * Set description.
454
     * @return string description.
455
     */
456
    public function getDescription()
457
    {
458
        $descAttribute = $this->descriptionAttribute;
459
        return is_string($descAttribute) ? $this->$descAttribute : null;
460
    }
461
462
    /**
463
     * Get description.
464
     * @param string $desc description.
465
     * @return string|null description if enabled, or null if disabled.
466
     */
467
    public function setDescription($desc)
468
    {
469
        $descAttribute = $this->descriptionAttribute;
470
        return is_string($descAttribute) ? $this->$descAttribute = $desc : null;
471
    }
472
473
    /**
474
     * Get blame.
475
     * @return \vistart\Models\queries\BaseUserQuery user.
476
     */
477 1
    public function getUser()
478
    {
479 1
        $userClass = $this->userClass;
480 1
        $model = $userClass::buildNoInitModel();
481 1
        return $this->hasOne($userClass::className(), [$model->guidAttribute => $this->createdByAttribute]);
0 ignored issues
show
Bug introduced by
It seems like hasOne() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
482
    }
483
484 1
    public function getUpdater()
485
    {
486 1
        if (!is_string($this->updatedByAttribute)) {
487 1
            return null;
488
        }
489
        $userClass = $this->userClass;
490
        $model = $userClass::buildNoInitModel();
491
        return $this->hasOne($userClass::className(), [$model->guidAttribute => $this->updatedByAttribute]);
0 ignored issues
show
Bug introduced by
It seems like hasOne() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
492
    }
493
494
    /**
495
     * This event is triggered before the model update.
496
     * This method is ONLY used for being triggered by event. DO NOT call,
497
     * override or modify it directly, unless you know the consequences.
498
     * @param \yii\base\Event $event
499
     */
500 9
    public function onContentChanged($event)
501
    {
502 9
        $sender = $event->sender;
503 9
        $sender->resetConfirmation();
504 9
    }
505
506
    /**
507
     * Return the current user's GUID if current model doesn't specify the owner
508
     * yet, or return the owner's GUID if current model has been specified.
509
     * This method is ONLY used for being triggered by event. DO NOT call,
510
     * override or modify it directly, unless you know the consequences.
511
     * @param \yii\base\Event $event
512
     * @return string the GUID of current user or the owner.
513
     */
514 25
    public function onGetCurrentUserGuid($event)
515
    {
516 25
        $sender = $event->sender;
517 25
        if (isset($sender->attributes[$sender->createdByAttribute])) {
518 25
            return $sender->attributes[$sender->createdByAttribute];
519
        }
520
        $identity = \Yii::$app->user->identity;
521
        if ($identity) {
522
            $igAttribute = $identity->guidAttribute;
523
            return $identity->$igAttribute;
524
        }
525
    }
526
527
    /**
528
     * Initialize type of content. the first of element[index is 0] of
529
     * $contentTypes will be used.
530
     * @param \yii\base\Event $event
531
     */
532 24
    public function onInitContentType($event)
533
    {
534 24
        $sender = $event->sender;
535 24
        if (!isset($sender->contentTypeAttribute) || !is_string($sender->contentTypeAttribute)) {
536 17
            return;
537
        }
538 7
        $contentTypeAttribute = $sender->contentTypeAttribute;
539 7
        if (!isset($sender->$contentTypeAttribute) &&
540 7
            !empty($sender->contentTypes) &&
541 7
            is_array($sender->contentTypes)) {
542 7
            $sender->$contentTypeAttribute = $sender->contentTypes[0];
543 7
        }
544 7
    }
545
546
    /**
547
     * Initialize description property with $initDescription.
548
     * @param \yii\base\Event $event
549
     */
550 25
    public function onInitDescription($event)
551
    {
552 25
        $sender = $event->sender;
553 25
        if (!isset($sender->descriptionAttribute) || !is_string($sender->descriptionAttribute)) {
554 19
            return;
555
        }
556 6
        $descriptionAttribute = $sender->descriptionAttribute;
557 6
        if (empty($sender->$descriptionAttribute)) {
558 6
            $sender->$descriptionAttribute = $sender->initDescription;
559 6
        }
560 6
    }
561
562
    /**
563
     * Attach events associated with blameable model.
564
     */
565 25
    public function initBlameableEvents()
566
    {
567 25
        $this->on(static::$eventConfirmationChanged, [$this, "onConfirmationChanged"]);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
568 25
        $this->on(static::$eventNewRecordCreated, [$this, "onInitConfirmation"]);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
569 25
        $contentTypeAttribute = $this->contentTypeAttribute;
570 25
        if (!isset($this->$contentTypeAttribute)) {
571 24
            $this->on(static::$eventNewRecordCreated, [$this, "onInitContentType"]);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
572 24
        }
573 25
        $descriptionAttribute = $this->descriptionAttribute;
574 25
        if (!isset($this->$descriptionAttribute)) {
575 25
            $this->on(static::$eventNewRecordCreated, [$this, 'onInitDescription']);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
576 25
        }
577 25
        $this->on(static::EVENT_BEFORE_UPDATE, [$this, "onContentChanged"]);
0 ignored issues
show
Bug introduced by
It seems like on() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
578 25
        $this->initSelfBlameableEvents();
579 25
    }
580
}
581