Completed
Push — stable ( ba1838...bafb54 )
by Nuno
09:32
created

Notifications::notify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 3
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 NunoMaduro\LaravelDesktopNotifier\Contracts\Notifier;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, NunoMaduro\LaravelDesktopNotifier\Notifier.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use NunoMaduro\LaravelDesktopNotifier\Contracts\Notification;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, NunoMaduro\LaravelDesktopNotifier\Notification.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
17
/**
18
 * An helper trait to include on Laravel console commands.
19
 *
20
 * @author Nuno Maduro <[email protected]>
21
 */
22
trait Notifications
23
{
24
    /**
25
     * @param  string $text
26
     * @param  string $body
27
     * @param  null $icon
28
     *
29
     * @return void
30
     */
31
    public function notify($text, $body, $icon = null)
32
    {
33
        $notifier = app(Notifier::class);
34
35
        $notification = app(Notification::class)
36
            ->setTitle($text)
37
            ->setBody($body)
38
            ->setIcon($icon);
39
40
        $notifier->send($notification);
41
    }
42
}
43