Passed
Push — main ( 49e508...46fa35 )
by Michiel
06:19
created

HipchatTask::setAuthToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
namespace Phing\Task\Optional;
21
22
use Phing\Exception\BuildException;
23
use Phing\Project;
24
use Phing\Task;
25
26
/**
27
 * HipchatTask
28
 * Sends a simple hipchat notification.
29
 *
30
 * (Yeah, HipChat API has lots of more awesome features than sending a lousy text notification
31
 *  but I refuse implementing more as long as the chat client lacks the most basic feature of
32
 *  sorting contacts. If you share my opinion then please upvote this feature request:
33
 *  https://jira.atlassian.com/browse/HCPUB-363 )
34
 *
35
 * <hipchat room="1337" authToken="********" color="red" notify="true" format="html">
36
 *     Hello &lt;i&gt;World&lt;/i&gt;!
37
 * </hipchat>
38
 *
39
 * @author  Suat Özgür <[email protected]>
40
 * @package phing.tasks.ext
41
 */
42
class HipchatTask extends Task
43
{
44
    private $domain = 'api.hipchat.com';
45
    private $room = null;
46
    private $authToken = null;
47
    private $color = 'yellow';
48
    private $notify = false;
49
    private $message = null;
50
    private $format = 'text';
51
52
    public function main()
53
    {
54
        if (null === $this->getRoom()) {
0 ignored issues
show
introduced by
The condition null === $this->getRoom() is always false.
Loading history...
55
            throw new BuildException('(HipChat) room is not defined');
56
        }
57
58
        if (null === $this->getAuthToken()) {
0 ignored issues
show
introduced by
The condition null === $this->getAuthToken() is always false.
Loading history...
59
            throw new BuildException('(HipChat) authToken is not defined');
60
        }
61
62
        $url =
63
            'https://' .
64
            $this->getDomain() .
65
            '/v2/room/' .
66
            $this->getRoom() .
67
            '/notification?auth_token=' .
68
            $this->getAuthToken();
69
70
        $data = array(
71
            'color' => $this->getColor(),
72
            'message' => $this->getMessage(),
73
            'notify' => $this->isNotify(),
74
            'message_format' => $this->getFormat(),
75
        );
76
77
        $result = $this->executeApiCall($url, $data);
78
        if ($result !== true) {
79
            $this->log($result, Project::MSG_WARN);
80
        } else {
81
            $this->log('HipChat notification sent.');
82
        }
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getDomain()
89
    {
90
        return $this->domain;
91
    }
92
93
    /**
94
     * @param string $domain
95
     */
96
    public function setDomain($domain)
97
    {
98
        $this->domain = $domain;
99
    }
100
101
    /**
102
     * @return String
103
     */
104
    public function getFormat()
105
    {
106
        return $this->format;
107
    }
108
109
    /**
110
     * @param $format
111
     */
112
    public function setFormat($format)
113
    {
114
        $format = ($format != 'text' && $format != 'html') ? 'text' : $format;
115
        $this->format = $format;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getRoom()
122
    {
123
        return $this->room;
124
    }
125
126
    /**
127
     * @param string $room
128
     */
129
    public function setRoom($room)
130
    {
131
        $this->room = $room;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getAuthToken()
138
    {
139
        return $this->authToken;
140
    }
141
142
    /**
143
     * @param string $authToken
144
     */
145
    public function setAuthToken($authToken)
146
    {
147
        $this->authToken = $authToken;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getColor()
154
    {
155
        return $this->color;
156
    }
157
158
    /**
159
     * @param string $color
160
     */
161
    public function setColor($color)
162
    {
163
        $this->color = $color;
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function getMessage()
170
    {
171
        return $this->message;
172
    }
173
174
    /**
175
     * @return boolean
176
     */
177
    public function isNotify()
178
    {
179
        return $this->notify;
180
    }
181
182
    /**
183
     * @param boolean $notify
184
     */
185
    public function setNotify($notify)
186
    {
187
        $this->notify = $notify;
188
    }
189
190
    /**
191
     * @param $message
192
     */
193
    public function addText($message)
194
    {
195
        $this->message = trim($message);
196
    }
197
198
    private function executeApiCall($url, $data)
199
    {
200
        $postData = json_encode($data);
201
202
        $ch = curl_init();
203
        curl_setopt($ch, CURLOPT_URL, $url);
204
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
205
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
206
        curl_setopt($ch, CURLOPT_HEADER, 0);
207
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
208
        curl_setopt($ch, CURLOPT_POST, 1);
209
        $response = curl_exec($ch);
210
        if ($response !== '') {
211
            $result = json_decode($response, 1);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

211
            $result = json_decode(/** @scrutinizer ignore-type */ $response, 1);
Loading history...
212
            return $result['error']['message'] . ' (' . $result['error']['code'] . ')';
213
        }
214
        return true;
215
    }
216
}
217