Passed
Push — stable ( 7698e0...f9d7ca )
by Nuno
03:02
created

Command::notify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 3
dl 0
loc 13
ccs 10
cts 10
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelZero\Framework\Commands;
4
5
use Illuminate\Console\Scheduling\Schedule;
6
use Illuminate\Contracts\Container\Container;
7
use Illuminate\Console\Command as BaseCommand;
8
use NunoMaduro\LaravelDesktopNotifier\Contracts\Notifier;
9
use NunoMaduro\LaravelDesktopNotifier\Contracts\Notification;
10
11
/**
12
 * This is the Laravel Zero Framework abstract command class.
13
 *
14
 * @author Nuno Maduro <[email protected]>
15
 */
16
abstract class Command extends BaseCommand
17
{
18
    /**
19
     * Execute the console command. Here goes the command
20
     * code.
21
     *
22
     * @return void
23
     */
24
    abstract public function handle(): void;
25
26
    /**
27
     * Returns the application container.
28
     *
29
     * @return \Illuminate\Contracts\Container\Container
30
     */
31 2
    public function getContainer(): Container
32
    {
33 2
        return $this->getLaravel();
34
    }
35
36
    /**
37
     * Define the command's schedule.
38
     *
39
     * @param  \Illuminate\Console\Scheduling\Schedule $schedule
40
     *
41
     * @return void
42
     */
43 47
    public function schedule(Schedule $schedule): void
0 ignored issues
show
Unused Code introduced by
The parameter $schedule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45 47
    }
46
47
    /**
48
     * Gets the concrete implementation of the notifier. Then
49
     * creates a new notification and send it through the notifier.
50
     *
51
     * @param string $title
52
     * @param string $body
53
     * @param string|null $icon
54
     */
55 1
    public function notify(string $title, string $body, $icon = null): void
56
    {
57 1
        $notifier = $this->getContainer()
58 1
            ->make(Notifier::class);
59
60 1
        $notification = $this->getContainer()
61 1
            ->make(Notification::class)
62 1
            ->setTitle($title)
63 1
            ->setBody($body)
64 1
            ->setIcon($icon);
65
66 1
        $notifier->send($notification);
67 1
    }
68
69
    /**
70
     * Performs the given task and outputs
71
     * the result.
72
     *
73
     * @param string $title
74
     * @param callable $task
75
     *
76
     * @return bool
77
     */
78
    public function task(string $title, callable $task)
79
    {
80 6
        return tap($task(), function ($result) use ($title) {
81 6
            $this->output->writeln(
82 6
                "$title: ".($result ? '<info>✔</info>' : '<error>failed</error>')
83
            );
84 6
        });
85
    }
86
}
87