1
|
|
|
<?php |
2
|
|
|
namespace App\Controller; |
3
|
|
|
|
4
|
|
|
use App\Observers; |
5
|
|
|
use App\Model; |
6
|
|
|
use App\Common\JsonException; |
7
|
|
|
use App\Requests\IRequest; |
8
|
|
|
|
9
|
|
|
abstract class BaseController |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Illuminate\Validation\Factory; |
13
|
|
|
*/ |
14
|
|
|
public $validation; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var \App\Common\JsonApiEncoder |
18
|
|
|
*/ |
19
|
|
|
public $encoder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \App\Common\ApiRenderer |
23
|
|
|
*/ |
24
|
|
|
public $apiRenderer; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
public $settings; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Swift_Mailer |
33
|
|
|
*/ |
34
|
|
|
public $mailer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \App\Common\MailRenderer |
38
|
|
|
*/ |
39
|
|
|
public $mailRenderer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* BaseController constructor. |
43
|
|
|
* |
44
|
|
|
* @param $container |
45
|
|
|
*/ |
46
|
|
|
public function __construct($container) |
47
|
|
|
{ |
48
|
|
|
$this->apiRenderer = $container['apiRenderer']; |
49
|
|
|
$this->validation = $container['validation']; |
50
|
|
|
$this->settings = $container['settings']; |
51
|
|
|
$this->mailer = $container['mailer']; |
52
|
|
|
$this->mailRenderer = $container['mailRenderer']; |
53
|
|
|
$this->encoder = $container['encoder']; |
54
|
|
|
|
55
|
|
|
$this->registerModelObservers(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array|null|object $params |
60
|
|
|
* @param string $entity |
61
|
|
|
* @param IRequest $request |
62
|
|
|
* |
63
|
|
|
* @return bool |
64
|
|
|
* @throws JsonException |
65
|
|
|
*/ |
66
|
|
|
public function validateRequestParams($params, $entity, $request) |
67
|
|
|
{ |
68
|
|
|
if (!isset($params['data']['attributes'])) { |
69
|
|
|
throw new JsonException($entity, 400, 'Invalid Attribute', 'Do not see required attribute - data.'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$validator = $this->validation->make($params['data']['attributes'], $request->rules()); |
73
|
|
|
|
74
|
|
|
if ($validator->fails()) { |
75
|
|
|
$messages = implode(' ', $validator->messages()->all()); |
76
|
|
|
throw new JsonException($entity, 400, 'Invalid Attribute', $messages); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Register model observers |
84
|
|
|
*/ |
85
|
|
|
private function registerModelObservers() |
86
|
|
|
{ |
87
|
|
|
$observers = [ |
88
|
|
|
Observers\CreatedByAndUpdatedByObserver::class => [ |
89
|
|
|
Model\Right::class, |
90
|
|
|
Model\Role::class, |
91
|
|
|
Model\User::class, |
92
|
|
|
], |
93
|
|
|
|
94
|
|
|
Observers\LoggerObserver::class => [ |
95
|
|
|
Model\Right::class, |
96
|
|
|
Model\Role::class, |
97
|
|
|
Model\User::class, |
98
|
|
|
] |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
foreach ($observers as $observer => $models) { |
102
|
|
|
foreach ($models as $model) { |
103
|
|
|
call_user_func($model. '::observe', $observer); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|