Completed
Push — stable ( fe1d86...a2937d )
by Nuno
11:45
created

LaravelDesktopNotifierServiceProvider::boot()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of Laravel Desktop Notifier.
5
 *
6
 * (c) Nuno Maduro <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace NunoMaduro\LaravelDesktopNotifier;
13
14
use Joli\JoliNotif\NotifierFactory;
15
use Illuminate\Console\Command;
16
use Illuminate\Support\ServiceProvider;
17
use NunoMaduro\LaravelDesktopNotifier\Contracts\Notifier as NotifierContract;
18
use NunoMaduro\LaravelDesktopNotifier\Contracts\Notification as NotificationContract;
19
20
/**
21
 * The is the Laravel Desktop Notifier service provider class.
22
 */
23
class LaravelDesktopNotifierServiceProvider extends ServiceProvider
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function boot()
29
    {
30
        /*
31
         * Sends a desktop notification.
32
         *
33
         * @param  string $title
34
         * @param  string $body
35
         * @param  string|null $icon
36
         *
37
         * @return void
38
         */
39
        Command::macro(
40
            'notify',
41
            function (string $text, string $body, $icon = null) {
42
                $notifier = $this->app[Contracts\Notifier::class];
43
44
                $notification = $this->app[Contracts\Notification::class]
45
                    ->setTitle($text)
46
                    ->setBody($body)
47
                    ->setIcon($icon);
48
49
                $notifier->send($notification);
50
            }
51
        );
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function register()
58
    {
59
        $this->app->singleton('desktop.notifier', function ($app) {
60
            $config = $app['config']['app.notifiers'];
61
62
            $notifier = NotifierFactory::create(is_array($config) ? $config : []);
63
64
            return new Notifier($notifier);
65
        });
66
67
        $this->app->alias('desktop.notifier', NotifierContract::class);
68
69
        $this->app->bind('desktop.notification', function () {
70
            return new Notification();
71
        });
72
73
        $this->app->alias('desktop.notification', NotificationContract::class);
74
    }
75
}
76