IntegrationCallbacks::handle()   C
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 89

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 8
nop 2
dl 0
loc 89
rs 6.6844
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Integrations\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 Integrations\Services\IntegrationsService;
14
use Integrations\Scopes\IntegrationsScope;
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 IntegrationCallbacks
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(IntegrationsService::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 IntegrationsScope);
96
            return true;
97
        }
98
99
        if ($method == 'onCreating') {
100
            $business = app()->make(IntegrationsService::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