Completed
Push — stable ( 82cf4e...d26de4 )
by Nuno
03:30 queued 01:56
created

LaravelDesktopNotifierServiceProvider::register()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 2
eloc 9
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 Illuminate\Console\Command;
15
use Joli\JoliNotif\NotifierFactory;
16
use Illuminate\Support\ServiceProvider;
17
use NunoMaduro\LaravelDesktopNotifier\Contracts\Notification as NotificationContract;
18
use NunoMaduro\LaravelDesktopNotifier\Contracts\Notifier as NotifierContract;
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
48
                if (! empty($icon)) {
49
                    $notification->setIcon($icon);
50
                }
51
52
                $notifier->send($notification);
53
            }
54
        );
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function register()
61
    {
62
        $this->app->singleton('desktop.notifier', function ($app) {
63
            $config = $app['config']['app.notifiers'];
64
65
            $notifier = NotifierFactory::create(is_array($config) ? $config : []);
66
67
            return new Notifier($notifier);
68
        });
69
70
        $this->app->alias('desktop.notifier', NotifierContract::class);
71
72
        $this->app->bind('desktop.notification', function () {
73
            return new Notification();
74
        });
75
76
        $this->app->alias('desktop.notification', NotificationContract::class);
77
    }
78
}
79