TenancyServiceProvider::events()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 67
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 42
nc 1
nop 0
dl 0
loc 67
rs 9.248
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
declare(strict_types=1);
4
5
namespace App\Providers;
6
7
use Illuminate\Support\Facades\Event;
8
use Illuminate\Support\Facades\Route;
9
use Illuminate\Support\ServiceProvider;
10
use Stancl\JobPipeline\JobPipeline;
11
use Stancl\Tenancy\Events;
12
use Stancl\Tenancy\Jobs;
13
use Stancl\Tenancy\Listeners;
14
use Stancl\Tenancy\Middleware;
15
16
class TenancyServiceProvider extends ServiceProvider
17
{
18
    // By default, no namespace is used to support the callable array syntax.
19
    public static string $controllerNamespace = '';
20
21
    public function events()
22
    {
23
        return [
24
            // Tenant events
25
            Events\CreatingTenant::class => [],
26
            Events\TenantCreated::class => [
27
                JobPipeline::make([
28
                    Jobs\CreateDatabase::class,
29
                    Jobs\MigrateDatabase::class,
30
                    // Jobs\SeedDatabase::class,
31
32
                    // Your own jobs to prepare the tenant.
33
                    // Provision API keys, create S3 buckets, anything you want!
34
35
                ])->send(fn (Events\TenantCreated $event) => $event->tenant)->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
36
            ],
37
            Events\SavingTenant::class => [],
38
            Events\TenantSaved::class => [],
39
            Events\UpdatingTenant::class => [],
40
            Events\TenantUpdated::class => [],
41
            Events\DeletingTenant::class => [],
42
            Events\TenantDeleted::class => [
43
                JobPipeline::make([
44
                    Jobs\DeleteDatabase::class,
45
                ])->send(fn (Events\TenantDeleted $event) => $event->tenant)->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
46
            ],
47
48
            // Domain events
49
            Events\CreatingDomain::class => [],
50
            Events\DomainCreated::class => [],
51
            Events\SavingDomain::class => [],
52
            Events\DomainSaved::class => [],
53
            Events\UpdatingDomain::class => [],
54
            Events\DomainUpdated::class => [],
55
            Events\DeletingDomain::class => [],
56
            Events\DomainDeleted::class => [],
57
58
            // Database events
59
            Events\DatabaseCreated::class => [],
60
            Events\DatabaseMigrated::class => [],
61
            Events\DatabaseSeeded::class => [],
62
            Events\DatabaseRolledBack::class => [],
63
            Events\DatabaseDeleted::class => [],
64
65
            // Tenancy events
66
            Events\InitializingTenancy::class => [],
67
            Events\TenancyInitialized::class => [
68
                Listeners\BootstrapTenancy::class,
69
            ],
70
71
            Events\EndingTenancy::class => [],
72
            Events\TenancyEnded::class => [
73
                Listeners\RevertToCentralContext::class,
74
            ],
75
76
            Events\BootstrappingTenancy::class => [],
77
            Events\TenancyBootstrapped::class => [],
78
            Events\RevertingToCentralContext::class => [],
79
            Events\RevertedToCentralContext::class => [],
80
81
            // Resource syncing
82
            Events\SyncedResourceSaved::class => [
83
                Listeners\UpdateSyncedResource::class,
84
            ],
85
86
            // Fired only when a synced resource is changed in a different DB than the origin DB (to avoid infinite loops)
87
            Events\SyncedResourceChangedInForeignDatabase::class => [],
88
        ];
89
    }
90
91
    public function register()
92
    {
93
    }
94
95
    public function boot()
96
    {
97
        $this->bootEvents();
98
        $this->mapRoutes();
99
100
        $this->makeTenancyMiddlewareHighestPriority();
101
    }
102
103
    protected function bootEvents()
104
    {
105
        foreach ($this->events() as $event => $listeners) {
106
            foreach ($listeners as $listener) {
107
                if ($listener instanceof JobPipeline) {
108
                    $listener = $listener->toListener();
109
                }
110
111
                Event::listen($event, $listener);
112
            }
113
        }
114
    }
115
116
    protected function mapRoutes()
117
    {
118
        if (file_exists(base_path('routes/tenant.php'))) {
119
            Route::namespace(static::$controllerNamespace)
120
                ->group(base_path('routes/tenant.php'));
121
        }
122
    }
123
124
    protected function makeTenancyMiddlewareHighestPriority()
125
    {
126
        $tenancyMiddleware = [
127
            // Even higher priority than the initialization middleware
128
            Middleware\PreventAccessFromCentralDomains::class,
129
130
            Middleware\InitializeTenancyByDomain::class,
131
            Middleware\InitializeTenancyBySubdomain::class,
132
            Middleware\InitializeTenancyByDomainOrSubdomain::class,
133
            Middleware\InitializeTenancyByPath::class,
134
            Middleware\InitializeTenancyByRequestData::class,
135
        ];
136
137
        foreach (array_reverse($tenancyMiddleware) as $middleware) {
138
            $this->app[\Illuminate\Contracts\Http\Kernel::class]->prependToMiddlewarePriority($middleware);
139
        }
140
    }
141
}
142