WebPushServiceProvider::definePublishing()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
crap 6
1
<?php
2
3
namespace NotificationChannels\WebPush;
4
5
use Illuminate\Support\Str;
6
use Minishlink\WebPush\WebPush;
7
use Illuminate\Support\ServiceProvider;
8
9
class WebPushServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Register the application services.
13
     *
14
     * @return void
15
     */
16
    public function register()
17
    {
18
        $this->commands([VapidKeysGenerateCommand::class]);
19
20
        $this->mergeConfigFrom(__DIR__.'/../config/webpush.php', 'webpush');
21
    }
22
23
    /**
24
     * Bootstrap the application services.
25
     *
26
     * @return void
27
     */
28
    public function boot()
29
    {
30
        $this->app->when(WebPushChannel::class)
31
            ->needs(WebPush::class)
32
            ->give(function () {
33
                return new WebPush($this->webPushConfig());
34
            });
35
36
        if ($this->app->runningInConsole()) {
37
            $this->definePublishing();
38
        }
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    protected function webPushConfig()
45
    {
46
        $config = [];
47
        $webpush = config('webpush');
48
        $publicKey = $webpush['vapid']['public_key'];
49
        $privateKey = $webpush['vapid']['private_key'];
50
51
        if (! empty($webpush['gcm']['key'])) {
52
            $config['GCM'] = $webpush['gcm']['key'];
53
        }
54
55
        if (empty($publicKey) || empty($privateKey)) {
56
            return $config;
57
        }
58
59
        $config['VAPID'] = compact('publicKey', 'privateKey');
60
        $config['VAPID']['subject'] = $webpush['vapid']['subject'];
61
62
        if (empty($config['VAPID']['subject'])) {
63
            $config['VAPID']['subject'] = url('/');
64
        }
65
66
        if (! empty($webpush['vapid']['pem_file'])) {
67
            $config['VAPID']['pemFile'] = $webpush['vapid']['pem_file'];
68
69
            if (Str::startsWith($config['VAPID']['pemFile'], 'storage')) {
70
                $config['VAPID']['pemFile'] = base_path($config['VAPID']['pemFile']);
71
            }
72
        }
73
74
        return $config;
75
    }
76
77
    /**
78
     * Define the publishable migrations and resources.
79
     *
80
     * @return void
81
     */
82
    protected function definePublishing()
83
    {
84
        $this->publishes([
85
            __DIR__.'/../config/webpush.php' => config_path('webpush.php'),
86
        ], 'config');
87
88
        if (! class_exists('CreatePushSubscriptionsTable')) {
89
            $timestamp = date('Y_m_d_His', time());
90
91
            $this->publishes([
92
                __DIR__.'/../migrations/create_push_subscriptions_table.php.stub' => $this->app->databasePath().'/migrations/'.$timestamp.'_create_push_subscriptions_table.php',
0 ignored issues
show
Bug introduced by
The method databasePath() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean basePath()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
93
            ], 'migrations');
94
        }
95
    }
96
}
97