1
|
|
|
<?php |
2
|
|
|
namespace App\Controller; |
3
|
|
|
|
4
|
|
|
use App\Observers; |
5
|
|
|
use App\Model; |
6
|
|
|
use App\Common\JsonException; |
7
|
|
|
use App\Common\Auth; |
8
|
|
|
|
9
|
|
|
use App\Requests\IRequest; |
10
|
|
|
use \Neomerx\JsonApi\Encoder\Encoder; |
11
|
|
|
use \Neomerx\JsonApi\Encoder\EncoderOptions; |
12
|
|
|
use \Neomerx\JsonApi\Factories\Factory; |
13
|
|
|
use \Neomerx\JsonApi\Contracts\Document\LinkInterface; |
14
|
|
|
use \Neomerx\JsonApi\Document\Link; |
15
|
|
|
|
16
|
|
|
use Slim\Http\Request; |
17
|
|
|
|
18
|
|
|
abstract class BaseController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \Illuminate\Validation\Factory; |
22
|
|
|
*/ |
23
|
|
|
public $validation; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \App\Common\Renderer |
27
|
|
|
*/ |
28
|
|
|
public $renderer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
public $settings; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \Swift_Mailer |
37
|
|
|
*/ |
38
|
|
|
public $mailer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \App\Common\MailRenderer |
42
|
|
|
*/ |
43
|
|
|
public $mailRenderer; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
private $encodeEntitiesExtended = [ |
49
|
|
|
'App\Model\Log' => 'App\Schema\LogSchema', |
50
|
|
|
'App\Model\Right' => 'App\Schema\RightSchema', |
51
|
|
|
'App\Model\Role' => 'App\Schema\RoleSchema', |
52
|
|
|
'App\Model\User' => 'App\Schema\UserSchemaExtended', |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private $encodeEntities = [ |
59
|
|
|
'App\Model\Log' => 'App\Schema\LogSchema', |
60
|
|
|
'App\Model\Right' => 'App\Schema\RightSchema', |
61
|
|
|
'App\Model\Role' => 'App\Schema\RoleSchema', |
62
|
|
|
'App\Model\User' => 'App\Schema\UserSchema', |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* BaseController constructor. |
67
|
|
|
* |
68
|
|
|
* @param $container |
69
|
|
|
*/ |
70
|
|
|
public function __construct($container) |
71
|
|
|
{ |
72
|
|
|
$this->renderer = $container['renderer']; |
73
|
|
|
$this->validation = $container['validation']; |
74
|
|
|
$this->settings = $container['settings']; |
75
|
|
|
$this->mailer = $container['mailer']; |
76
|
|
|
$this->mailRenderer = $container['mailRenderer']; |
77
|
|
|
|
78
|
|
|
$this->registerModelObservers(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array|null|object $params |
83
|
|
|
* @param string $entity |
84
|
|
|
* @param IRequest $request |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
* @throws JsonException |
88
|
|
|
*/ |
89
|
|
|
public function validationRequest($params, $entity, $request) |
90
|
|
|
{ |
91
|
|
|
if (!isset($params['data']['attributes'])) { |
92
|
|
|
throw new JsonException($entity, 400, 'Invalid Attribute', 'Not required attributes - data.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$validator = $this->validation->make($params['data']['attributes'], $request->rules()); |
96
|
|
|
|
97
|
|
|
if ($validator->fails()) { |
98
|
|
|
$messages = implode(' ', $validator->messages()->all()); |
99
|
|
|
throw new JsonException($entity, 400, 'Invalid Attribute', $messages); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Request $request |
107
|
|
|
* @param mixed $entities |
108
|
|
|
* @param integer|null $pageNumber |
109
|
|
|
* @param integer|null $pageSize |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function encode(Request $request, $entities, $pageNumber = null, $pageSize = null) |
114
|
|
|
{ |
115
|
|
|
$factory = new Factory(); |
116
|
|
|
$parameters = $factory->createQueryParametersParser()->parse($request); |
117
|
|
|
$user = Auth::getUser(); |
118
|
|
|
$encodeEntities = $this->encodeEntities; |
119
|
|
|
|
120
|
|
|
if ($user && $user->role_id == Model\User::ROLE_ADMIN) { |
121
|
|
|
$encodeEntities = $this->encodeEntitiesExtended; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$encoder = Encoder::instance( |
125
|
|
|
$encodeEntities, |
126
|
|
|
new EncoderOptions( |
127
|
|
|
JSON_PRETTY_PRINT, |
128
|
|
|
$this->settings['params']['host'].'/api' |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
if (isset($pageNumber) && isset($pageSize)) { |
133
|
|
|
$links = [ |
134
|
|
|
LinkInterface::SELF => new Link('?page[number]='.$pageNumber.'&page[size]='.$pageSize, null, false), |
135
|
|
|
LinkInterface::FIRST => new Link('?page[number]=1&page[size]='.$pageSize, null, false), |
136
|
|
|
LinkInterface::LAST => new Link('?page[number]='.$entities->lastPage().'&page[size]='.$pageSize, null, false), |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
$meta = [ |
140
|
|
|
'total' => $entities->total(), |
141
|
|
|
'count' => $entities->count(), |
142
|
|
|
]; |
143
|
|
|
|
144
|
|
View Code Duplication |
if (($entities->lastPage() - ($pageNumber + 1)) >= 0) { |
|
|
|
|
145
|
|
|
$links[LinkInterface::NEXT] = new Link('?page[number]='.($pageNumber + 1).'&page[size]='.$pageSize, null, false); |
146
|
|
|
} |
147
|
|
View Code Duplication |
if (($pageNumber - 1) > 0) { |
|
|
|
|
148
|
|
|
$links[LinkInterface::PREV] = new Link('?page[number]='.($pageNumber - 1).'&page[size]='.$pageSize, null, false); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (isset($links)) { |
153
|
|
|
$encoder->withLinks($links); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (isset($meta)) { |
157
|
|
|
$encoder->withMeta($meta); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $encoder->encodeData($entities, $parameters); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Register model observers |
165
|
|
|
*/ |
166
|
|
|
private function registerModelObservers(){ |
167
|
|
|
$observers = [ |
168
|
|
|
Observers\CreatedByAndUpdatedByObserver::class => [ |
169
|
|
|
Model\Right::class, |
170
|
|
|
Model\Role::class, |
171
|
|
|
Model\User::class, |
172
|
|
|
], |
173
|
|
|
|
174
|
|
|
Observers\LoggerObserver::class => [ |
175
|
|
|
Model\Right::class, |
176
|
|
|
Model\Role::class, |
177
|
|
|
Model\User::class, |
178
|
|
|
] |
179
|
|
|
]; |
180
|
|
|
|
181
|
|
|
foreach($observers as $observer => $models){ |
182
|
|
|
foreach($models as $model){ |
183
|
|
|
call_user_func($model. '::observe', $observer); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
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.