Passed
Push — master ( f380be...f04938 )
by Michiel
11:54
created

NotifySendTask::setIcon()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 6
rs 10
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
    public function setIcon(PhingFile $icon)
38
    {
39
        if ($icon->isFile()) {
40
            $this->log(sprintf('Using "%s" as icon.', $icon), Project::MSG_VERBOSE);
41
            $this->icon = $icon->getAbsoluteFile();
42
            return;
43
        }
44
45
        $this->log(sprintf('"%s" is not a file. Assuming it is a stock icon name.', $icon->getName()), Project::MSG_WARN);
46
        $this->icon = $icon->getName();
47
    }
48
49
    /**
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
     * 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 1
    public function setSilent($silent)
67
    {
68 1
        $this->silent = StringHelper::booleanValue($silent);
69 1
    }
70
71
    /**
72
     * Set title attribute
73
     *
74
     * @param string $title Title to display
75
     *
76
     * @return void
77
     */
78 1
    public function setTitle($title)
79
    {
80 1
        $this->title = $title;
81 1
    }
82
83
    /**
84
     * Get Title
85
     *
86
     * @return string
87
     */
88 1
    public function getTitle()
89
    {
90 1
        return $this->title;
91
    }
92
93
    /**
94
     * Set msg attribute
95
     *
96
     * @param string $msg Message
97
     *
98
     * @return void
99
     */
100 1
    public function setMsg($msg)
101
    {
102 1
        $this->msg = $msg;
103 1
    }
104
105
    /**
106
     * Get message.
107
     *
108
     * @return string
109
     */
110 1
    public function getMsg()
111
    {
112 1
        return $this->msg;
113
    }
114
115
    /**
116
     * The main entry point method.
117
     *
118
     * @throws BuildException
119
     * @return void
120
     */
121 1
    public function main()
122
    {
123 1
        $msg = '';
124 1
        $title = 'Phing';
125 1
        $executable = 'notify-send';
126
127 1
        if ($this->title != '') {
128
            $title = "'" . $this->title . "'";
129
        }
130
131 1
        if ($this->msg != '') {
132
            $msg = "'" . $this->msg . "'";
133
        }
134
135 1
        $cmd = $executable . ' -i ' . $this->icon . ' ' . $title . ' ' . $msg;
136
137 1
        $this->log(sprintf("Title: '%s'", $title), Project::MSG_DEBUG);
138 1
        $this->log(sprintf("Message: '%s'", $msg), Project::MSG_DEBUG);
139 1
        $this->log($msg, Project::MSG_INFO);
140
141 1
        $this->log(sprintf("cmd: %s", $cmd), Project::MSG_DEBUG);
142 1
        if (!$this->silent) {
143
            $fs = FileSystem::getFileSystem();
144
            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
                }
149
            } else {
150
                $this->log("Executable ($executable) not found", Project::MSG_DEBUG);
151
            }
152
        } else {
153 1
            $this->log("Silent flag set; not executing", Project::MSG_DEBUG);
154
        }
155 1
    }
156
}
157
158
// vim:set et ts=4 sw=4:
159