1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Platfourm package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\Database\Eloquent; |
12
|
|
|
|
13
|
|
|
use Illuminate\Database\Eloquent\Model as Eloquent; |
14
|
|
|
use Longman\Platfourm\Contracts\Auth\AuthUserService; |
15
|
|
|
|
16
|
|
|
class Model extends Eloquent |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The name of the "updated by" column. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
const CREATED_BY = 'created_by'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The name of the "updated by" column. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
const UPDATED_BY = 'updated_by'; |
32
|
|
|
|
33
|
|
|
protected $searchableFields = []; |
34
|
|
|
|
35
|
|
|
protected $sortableFields = []; |
36
|
|
|
|
37
|
|
|
protected $filterableFields = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get Searchable Fields. |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public function getSearchableFields() |
45
|
|
|
{ |
46
|
|
|
return $this->searchableFields; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get Sortable Fields. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function getSortableFields() |
55
|
|
|
{ |
56
|
|
|
return $this->sortableFields; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get Sortable Fields. |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function getFilterableFields() |
65
|
|
|
{ |
66
|
|
|
return $this->filterableFields; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Update the creation and update timestamps. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
protected function updateTimestamps() |
75
|
|
|
{ |
76
|
|
|
$time = $this->freshTimestamp(); |
77
|
|
|
|
78
|
|
|
if (!$this->isDirty(static::UPDATED_AT)) { |
79
|
|
|
$this->setUpdatedAt($time); |
80
|
|
|
$this->setUpdatedBy(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!$this->exists && !$this->isDirty(static::CREATED_AT)) { |
84
|
|
|
$this->setCreatedAt($time); |
85
|
|
|
$this->setCreatedBy(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set the value of the "updated by" attribute. |
91
|
|
|
* |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function setUpdatedBy() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$userService = app()->make(AuthUserService::class); |
97
|
|
|
if ($userService->check()) { |
98
|
|
|
$this->{static::UPDATED_BY} = $userService->user()->id; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set the value of the "created by" attribute. |
106
|
|
|
* |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function setCreatedBy() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$userService = app()->make(AuthUserService::class); |
112
|
|
|
if ($userService->check()) { |
113
|
|
|
$this->{static::CREATED_BY} = $userService->user()->id; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
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.