Passed
Push — master ( 11623a...f380be )
by Michiel
21:16
created

NotifySendTask::setIcon()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Utilise notify-send from within Phing.
4
 *
5
 * PHP Version 5
6
 *
7
 * @category Tasks
8
 * @package  phing.tasks.ext
9
 * @author   Ken Guest <[email protected]>
10
 * @license  LGPL (see http://www.gnu.org/licenses/lgpl.html)
11
 * @link     https://github.com/kenguest/Phing-NotifySendTask
12
 */
13
14
/**
15
 * NotifySendTask
16
 *
17
 * @category Tasks
18
 * @package  phing.tasks.ext
19
 * @author   Ken Guest <[email protected]>
20
 * @license  LGPL (see http://www.gnu.org/licenses/lgpl.html)
21
 * @link     NotifySendTask.php
22
 */
23
class NotifySendTask extends Task
24
{
25
    protected $msg = null;
26
    protected $title = null;
27
    protected $icon = 'info';
28
    protected $silent = false;
29
30
    /**
31
     * Set icon attribute
32
     *
33
     * @param \PhingFile $icon name/location of icon
34
     *
35
     * @return void
36
     */
37 2
    public function setIcon(PhingFile $icon)
38
    {
39 2
        if ($icon->isFile()) {
40 2
            $this->log(sprintf('Using "%s" as icon.', $icon), Project::MSG_VERBOSE);
41 2
            $this->icon = $icon->getAbsoluteFile();
42 2
            return;
43 1
        }
44 1
45
        $this->log(sprintf('"%s" is not a file. Assuming it is a stock icon name.', $icon->getName()), Project::MSG_WARN);
46 1
        $this->icon = $icon->getName();
47
    }
48
49 1
    /**
50
     * Get icon to be used (filename or generic name)
51
     *
52
     * @return string
53
     */
54
    public function getIcon()
55
    {
56
        return $this->icon;
57
    }
58
59
    /**
60 2
     * Set to a true value to not execute notifysend command.
61
     *
62
     * @param string $silent Don't execute notifysend? Truthy value.
63
     *
64
     * @return void
65
     */
66
    public function setSilent($silent)
67 2
    {
68
        $this->silent = StringHelper::booleanValue($silent);
69 2
    }
70
71
    /**
72
     * Set title attribute
73
     *
74
     * @param string $title Title to display
75
     *
76
     * @return void
77
     */
78
    public function setTitle($title)
79 1
    {
80
        $this->title = $title;
81 1
    }
82 1
83
    /**
84
     * Get Title
85
     *
86
     * @return string
87
     */
88
    public function getTitle()
89
    {
90
        return $this->title;
91 1
    }
92
93 1
    /**
94 1
     * Set msg attribute
95
     *
96
     * @param string $msg Message
97
     *
98
     * @return void
99
     */
100
    public function setMsg($msg)
101 1
    {
102
        $this->msg = $msg;
103 1
    }
104
105
    /**
106
     * Get message.
107
     *
108
     * @return string
109
     */
110
    public function getMsg()
111
    {
112
        return $this->msg;
113 1
    }
114
115 1
    /**
116 1
     * The main entry point method.
117
     *
118
     * @throws BuildException
119
     * @return void
120
     */
121
    public function main()
122
    {
123 1
        $msg = '';
124
        $title = 'Phing';
125 1
        $executable = 'notify-send';
126
127
        if ($this->title != '') {
128
            $title = "'" . $this->title . "'";
129
        }
130
131
        if ($this->msg != '') {
132
            $msg = "'" . $this->msg . "'";
133
        }
134 1
135
        $cmd = $executable . ' -i ' . $this->icon . ' ' . $title . ' ' . $msg;
136 1
137 1
        $this->log(sprintf("Title: '%s'", $title), Project::MSG_DEBUG);
138 1
        $this->log(sprintf("Message: '%s'", $msg), Project::MSG_DEBUG);
139
        $this->log($msg, Project::MSG_INFO);
140 1
141
        $this->log(sprintf("cmd: %s", $cmd), Project::MSG_DEBUG);
142
        if (!$this->silent) {
143
            $fs = FileSystem::getFileSystem();
144 1
            if ($fs->which($executable) !== false) {
0 ignored issues
show
introduced by
The condition $fs->which($executable) !== false is always true.
Loading history...
145
                exec(escapeshellcmd($cmd), $output, $return);
146
                if ($return !== 0) {
147
                    throw new BuildException("Notify task failed.");
148 1
                }
149
            } else {
150 1
                $this->log("Executable ($executable) not found", Project::MSG_DEBUG);
151 1
            }
152 1
        } else {
153
            $this->log("Silent flag set; not executing", Project::MSG_DEBUG);
154 1
        }
155 1
    }
156
}
157
158
// vim:set et ts=4 sw=4:
159