1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: vadim |
5
|
|
|
* Date: 26.05.15 |
6
|
|
|
* Time: 15:12 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace sibds\behaviors; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use yii\behaviors\BlameableBehavior; |
13
|
|
|
|
14
|
|
|
class UserDataBehavior extends BlameableBehavior { |
15
|
|
|
|
16
|
|
|
public $userClass = null; |
17
|
|
|
|
18
|
|
|
public function init() |
19
|
|
|
{ |
20
|
|
|
if (is_null($this->userClass)) { |
21
|
|
|
$this->userClass = \Yii::$app->user->className(); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @getCreateUser |
27
|
|
|
* @return null|\yii\db\ActiveQuery |
28
|
|
|
*/ |
29
|
|
|
public function getCreateUser() |
30
|
|
|
{ |
31
|
|
|
/* @var $owner BaseActiveRecord */ |
32
|
|
|
$owner = $this->owner; |
33
|
|
|
|
34
|
|
|
if ($owner->hasAttribute($this->createdByAttribute) && $owner->hasAttribute($this->updatedByAttribute)) { |
35
|
|
|
return $owner->hasOne($this->userClass, ['id' => $this->createdByAttribute]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @getCreateUserName |
43
|
|
|
* @return null|\yii\db\ActiveQuery |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function getCreateUserName() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
/* @var $owner BaseActiveRecord */ |
48
|
|
|
$owner = $this->owner; |
49
|
|
|
|
50
|
|
|
if ($owner->hasAttribute($this->createdByAttribute) && $owner->hasAttribute($this->updatedByAttribute)) { |
51
|
|
|
return $this->createUser ? $this->createUser->username : '- no user -'; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @getUpdateUser |
59
|
|
|
* @return null|\yii\db\ActiveQuery |
60
|
|
|
*/ |
61
|
|
|
public function getUpdateUser() |
62
|
|
|
{ |
63
|
|
|
/* @var $owner BaseActiveRecord */ |
64
|
|
|
$owner = $this->owner; |
65
|
|
|
|
66
|
|
|
if ($owner->hasAttribute($this->createdByAttribute) && $owner->hasAttribute($this->updatedByAttribute)) { |
67
|
|
|
return $this->hasOne($this->userClass, ['id' => $this->updatedByAttribute]); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @getUpdateUserName |
75
|
|
|
* @return null|\yii\db\ActiveQuery |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function getUpdateUserName() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
/* @var $owner BaseActiveRecord */ |
80
|
|
|
$owner = $this->owner; |
81
|
|
|
|
82
|
|
|
if ($owner->hasAttribute($this->createdByAttribute) && $owner->hasAttribute($this->updatedByAttribute)) { |
83
|
|
|
return $this->createUser ? $this->updateUser->username : '- no user -'; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.