Service   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 31
c 4
b 0
f 0
dl 0
loc 58
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A registerBladeDirective() 0 15 3
A register() 0 9 1
A provides() 0 4 1
A boot() 0 19 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GeneaLabs\LaravelMessenger\Providers;
6
7
use Exception;
8
use GeneaLabs\LaravelMessenger\Messenger;
9
use GeneaLabs\LaravelMessenger\Console\Commands\Publish;
10
use Illuminate\Support\ServiceProvider;
11
12
class Service extends ServiceProvider
13
{
14
    protected $defer = false;
15
16
    public function boot(): void
17
    {
18
        if (! $this->app->routesAreCached()) {
19
            require __DIR__ . '/../../routes/web.php';
20
        }
21
22
        $this->loadViewsFrom(
23
            __DIR__ . '/../../resources/views',
24
            'genealabs-laravel-messenger'
25
        );
26
        $this->publishes([
27
            __DIR__ . '/../../resources/views' => resource_path('views/vendor/genealabs/laravel-messenger'),
28
        ], "views");
29
30
        $configPath = __DIR__ . '/../../config/genealabs-laravel-messenger.php';
31
        $this->mergeConfigFrom($configPath, 'genealabs-laravel-messenger');
32
        $this->publishes([
33
            $configPath => config_path('genealabs-laravel-messenger.php')
34
        ], 'config');
35
    }
36
37
    public function register(): void
38
    {
39
        $this->commands(Publish::class);
40
41
        $this->app->singleton('messenger', function () {
42
            return new Messenger;
43
        });
44
        $this->registerBladeDirective('deliver');
45
        $this->registerBladeDirective('send');
46
    }
47
48
    private function registerBladeDirective($formMethod, $alias = null): void
49
    {
50
        $alias = $alias ?: $formMethod;
51
        $blade = app('view')->getEngineResolver()
52
            ->resolve('blade')
53
            ->getCompiler();
0 ignored issues
show
Bug introduced by
The method getCompiler() does not exist on Illuminate\Contracts\View\Engine. It seems like you code against a sub-type of Illuminate\Contracts\View\Engine such as Illuminate\View\Engines\CompilerEngine. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
            ->/** @scrutinizer ignore-call */ getCompiler();
Loading history...
54
55
        if (array_key_exists($alias, $blade->getCustomDirectives())) {
56
            throw new Exception("Blade directive '{$alias}' is already registered.");
57
        }
58
59
        $blade->directive($alias, function ($parameters) use ($formMethod) {
60
            $parameters = trim($parameters, "()");
61
62
            return "<?php echo app('messenger')->{$formMethod}({$parameters}); ?>";
63
        });
64
    }
65
66
    public function provides(): array
67
    {
68
        return [
69
            'genealabs-laravel-messenger',
70
        ];
71
    }
72
}
73