1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Telefonica\Observers; |
4
|
|
|
|
5
|
|
|
use Event; |
6
|
|
|
use Log; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Pedreiro\Models\Base; |
9
|
|
|
use Symfony\Component\Debug\Exception\FatalThrowableError; |
10
|
|
|
use Throwable; |
11
|
|
|
use Illuminate\Support\Facades\Schema; |
12
|
|
|
use Auth; |
13
|
|
|
use Telefonica\Services\TelefonicaService; |
14
|
|
|
use Telefonica\Scopes\TelefonicaScope; |
15
|
|
|
/** |
16
|
|
|
* Call no-op classes on models for all event types. This just simplifies |
17
|
|
|
* the handling of model events for models. |
18
|
|
|
*/ |
19
|
|
|
class TelefonicaCallbacks |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Handle all model events, both Eloquent and Decoy |
23
|
|
|
* |
24
|
|
|
* @param string $event |
25
|
|
|
* @param array $payload Contains: |
26
|
|
|
* - |
27
|
|
|
* Facilitador\Models\Base |
28
|
|
|
* $model |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function handle($event, $payload) |
32
|
|
|
{ |
33
|
|
|
list($model) = $payload; |
34
|
|
|
|
35
|
|
|
// // Payload |
36
|
|
|
// ^ Cmgmyr\Messenger\Models\Message^ {#4332 |
37
|
|
|
// #table: "messages" |
38
|
|
|
// #touches: array:1 [ |
39
|
|
|
// 0 => "thread" |
40
|
|
|
// ] |
41
|
|
|
// #fillable: array:3 [ |
42
|
|
|
// 0 => "thread_id" |
43
|
|
|
// 1 => "user_id" |
44
|
|
|
// 2 => "body" |
45
|
|
|
// ] |
46
|
|
|
// #dates: array:1 [ |
47
|
|
|
// 0 => "deleted_at" |
48
|
|
|
// ] |
49
|
|
|
// #connection: null |
50
|
|
|
// #primaryKey: "id" |
51
|
|
|
// #keyType: "int" |
52
|
|
|
// +incrementing: true |
53
|
|
|
// #with: [] |
54
|
|
|
// #withCount: [] |
55
|
|
|
// #perPage: 15 |
56
|
|
|
// +exists: false |
57
|
|
|
// +wasRecentlyCreated: false |
58
|
|
|
// #attributes: [] |
59
|
|
|
// #original: [] |
60
|
|
|
// #changes: [] |
61
|
|
|
// #casts: [] |
62
|
|
|
// #dateFormat: null |
63
|
|
|
// #appends: [] |
64
|
|
|
// #dispatchesEvents: [] |
65
|
|
|
// #observables: [] |
66
|
|
|
// #relations: [] |
67
|
|
|
// +timestamps: true |
68
|
|
|
// #hidden: [] |
69
|
|
|
// #visible: [] |
70
|
|
|
// #guarded: array:1 [ |
71
|
|
|
// 0 => "*" |
72
|
|
|
// ] |
73
|
|
|
// #forceDeleting: false |
74
|
|
|
// } |
75
|
|
|
|
76
|
|
|
// Ignore |
77
|
|
|
if (!app()->bound(TelefonicaService::class)) { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (!Schema::hasColumn($model->getTable(), 'business_code')) { |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
// Get the action from the event name |
87
|
|
|
preg_match('#\.(\w+)#', $event, $matches); |
88
|
|
|
$action = $matches[1]; |
89
|
|
|
|
90
|
|
|
// If there is matching callback method on the model, call it, passing |
91
|
|
|
// any additional event arguments to it |
92
|
|
|
$method = 'on'.Str::studly($action); |
93
|
|
|
|
94
|
|
|
if ($method == 'onBooting') { |
95
|
|
|
$model::addGlobalScope(new TelefonicaScope); |
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($method == 'onCreating') { |
100
|
|
|
$business = app()->make(TelefonicaService::class); |
101
|
|
|
$model->business_code = $business->getCode(); |
102
|
|
|
if (Auth::check()) { |
103
|
|
|
// @todo Verifica se tem acesso |
104
|
|
|
} |
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($method == 'onCreated') { |
109
|
|
|
|
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($method == 'onValidating' || $method == 'onValidated') { |
114
|
|
|
|
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|