Completed
Push — master ( ce492c...ed54c9 )
by Freek
03:04 queued 11s
created

WebhookClientServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 9
dl 0
loc 56
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\WebhookClient;
4
5
use Illuminate\Support\Facades\Route;
6
use Illuminate\Support\ServiceProvider;
7
use Illuminate\Support\Str;
8
use Spatie\WebhookClient\Exceptions\InvalidConfig;
9
10
class WebhookClientServiceProvider extends ServiceProvider
11
{
12
    public function boot()
13
    {
14
        if ($this->app->runningInConsole()) {
15
            $this->publishes([
16
                __DIR__.'/../config/webhook-client.php' => config_path('webhook-client.php'),
17
            ], 'config');
18
        }
19
20
        if (! class_exists('CreateWebhookCallsTable')) {
21
            $timestamp = date('Y_m_d_His', time());
22
23
            $this->publishes([
24
                __DIR__.'/../database/migrations/create_webhook_calls_table.php.stub' => database_path("migrations/{$timestamp}_create_webhook_calls_table.php"),
25
            ], 'migrations');
26
        }
27
28
        Route::macro('webhooks', fn(string $url, string $name = 'default') => Route::post($url, '\Spatie\WebhookClient\WebhookController')->name("webhook-client-{$name}"));
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_VARIABLE, expecting ',' or ')'
Loading history...
29
30
        $this->app->singleton(WebhookConfigRepository::class, function () {
31
            $configRepository = new WebhookConfigRepository();
32
33
            collect(config('webhook-client.configs'))
34
                ->map(fn(array $config) => new WebhookConfig($config))
35
                ->each(function (WebhookConfig $webhookConfig) use ($configRepository) {
36
                    $configRepository->addConfig($webhookConfig);
37
                });
38
39
            return $configRepository;
40
        });
41
42
        $this->app->bind(WebhookConfig::class, function () {
43
            $routeName = Route::currentRouteName();
44
45
            $configName = Str::after($routeName, 'webhook-client-');
46
47
            $webhookConfig = app(WebhookConfigRepository::class)->getConfig($configName);
48
49
            if (is_null($webhookConfig)) {
50
                throw InvalidConfig::couldNotFindConfig($configName);
51
            }
52
53
            return $webhookConfig;
54
        });
55
    }
56
57
    public function register()
58
    {
59
        $this->mergeConfigFrom(__DIR__.'/../config/webhook-client.php', 'webhook-client');
60
    }
61
}
62