Completed
Push — stable ( 4cd2c6...82cf4e )
by Nuno
12:09 queued 08:43
created

LaravelDesktopNotifierServiceProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
lcom 1
cbo 6
dl 0
loc 53
rs 10
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 Joli\JoliNotif\NotifierFactory;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Cannot use Joli\JoliNotif\NotifierFactory as NotifierFactory because the name is already in use
Loading history...
18
use NunoMaduro\LaravelDesktopNotifier\Contracts\Notification as NotificationContract;
19
use NunoMaduro\LaravelDesktopNotifier\Contracts\Notifier as NotifierContract;
20
21
/**
22
 * The is the Laravel Desktop Notifier service provider class.
23
 */
24
class LaravelDesktopNotifierServiceProvider extends ServiceProvider
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function boot()
30
    {
31
        /*
32
         * Sends a desktop notification.
33
         *
34
         * @param  string $title
35
         * @param  string $body
36
         * @param  string|null $icon
37
         *
38
         * @return void
39
         */
40
        Command::macro(
41
            'notify',
42
            function (string $text, string $body, $icon = null) {
43
                $notifier = $this->app[Contracts\Notifier::class];
44
45
                $notification = $this->app[Contracts\Notification::class]
46
                    ->setTitle($text)
47
                    ->setBody($body);
48
49
                if (! empty($icon)) {
50
                    $notification->setIcon($icon);
51
                }
52
53
                $notifier->send($notification);
54
            }
55
        );
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function register()
62
    {
63
        $this->app->singleton('desktop.notifier', function ($app) {
64
            $config = $app['config']['app.notifiers'];
65
66
            $notifier = NotifierFactory::create(is_array($config) ? $config : []);
67
68
            return new Notifier($notifier);
69
        });
70
71
        $this->app->alias('desktop.notifier', NotifierContract::class);
72
73
        $this->app->bind('desktop.notification', function () {
74
            return new Notification();
75
        });
76
77
        $this->app->alias('desktop.notification', NotificationContract::class);
78
    }
79
}
80