Passed
Push — main ( b7649a...398f47 )
by Michiel
06:32
created

NotifySendTask   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 14
eloc 39
dl 0
loc 131
ccs 33
cts 44
cp 0.75
rs 10
c 0
b 0
f 0

8 Methods

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