Test Failed
Push — develop ( 38bc20...a43681 )
by Àlex
05:29
created

Xmpp::buildMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
/**
3
 * PHPCI - Continuous Integration for PHP.
4
 *
5
 * @copyright    Copyright 2014, Block 8 Limited.
6
 * @license      https://github.com/Block8/PHPCI/blob/master/LICENSE.md
7
 *
8
 * @link         https://www.phptesting.org/
9
 */
10
11
namespace PHPCI\Plugin;
12
13
use PHPCI\Builder;
14
use PHPCI\Model\Build;
15
16
/**
17
 * XMPP Notification - Send notification for successful or failure build.
18
 *
19
 * @author       Alexandre Russo <[email protected]>
20
 */
21
class Xmpp implements \PHPCI\Plugin
22
{
23
    protected $directory;
24
    protected $phpci;
25
    protected $build;
26
27
    /**
28
     * @var string, username of sender account xmpp
29
     */
30
    protected $username;
31
32
    /**
33
     * @var string, alias server of sender account xmpp
34
     */
35
    protected $server;
36
37
    /**
38
     * @var string, password of sender account xmpp
39
     */
40
    protected $password;
41
42
    /**
43
     * @var string, alias for sender
44
     */
45
    protected $alias;
46
47
    /**
48
     * @var string, use tls
49
     */
50
    protected $tls;
51
52
    /**
53
     * @var array, list of recipients xmpp accounts
54
     */
55
    protected $recipients;
56
57
    /**
58
     * @var string, mask to format date
59
     */
60
    protected $date_format;
61
62
    /**
63
     * @param Builder $phpci
64
     * @param Build   $build
65
     * @param array   $options
66
     */
67
    public function __construct(Builder $phpci, Build $build, array $options = array())
68
    {
69
        $this->phpci = $phpci;
70
        $this->build = $build;
71
72
        $this->username = '';
73
        $this->password = '';
74
        $this->server = '';
75
        $this->alias = '';
76
        $this->recipients = array();
77
        $this->tls = false;
78
        $this->date_format = '%c';
79
80
        /*
81
         * Set recipients list
82
         */
83
        if (!empty($options['recipients'])) {
84
            if (is_string($options['recipients'])) {
85
                $this->recipients = array($options['recipients']);
86
            } elseif (is_array($options['recipients'])) {
87
                $this->recipients = $options['recipients'];
88
            }
89
        }
90
91
        $this->setOptions($options);
92
    }
93
94
    /**
95
     * Set options configuration for plugin.
96
     *
97
     * @param array $options
98
     */
99
    protected function setOptions($options)
100
    {
101
        foreach (array('username', 'password', 'alias', 'tls', 'server', 'date_format') as $key) {
102
            if (array_key_exists($key, $options)) {
103
                $this->{$key} = $options[$key];
104
            }
105
        }
106
    }
107
108
    /**
109
     * Get config format for sendxmpp config file.
110
     *
111
     * @return string
112
     */
113
    protected function getConfigFormat()
114
    {
115
        $conf = $this->username;
116
        if (!empty($this->server)) {
117
            $conf .= ';'.$this->server;
118
        }
119
120
        $conf .= ' '.$this->password;
121
122
        if (!empty($this->alias)) {
123
            $conf .= ' '.$this->alias;
124
        }
125
126
        return $conf;
127
    }
128
129
    /**
130
     * Find config file for sendxmpp binary (default is .sendxmpprc).
131
     */
132
    public function findConfigFile()
133
    {
134
        if (file_exists($this->phpci->buildPath.DIRECTORY_SEPARATOR.'.sendxmpprc')) {
135
            if (md5(file_get_contents($this->phpci->buildPath.DIRECTORY_SEPARATOR.'.sendxmpprc'))
136
                !== md5($this->getConfigFormat())) {
137
                return;
138
            }
139
140
            return true;
141
        }
142
143
        return;
144
    }
145
146
    /**
147
     * Send notification message.
148
     */
149
    public function execute()
150
    {
151
        $sendxmpp = $this->phpci->findBinary('sendxmpp');
152
153
        /*
154
         * Without recipients we can't send notification
155
         */
156
        if (count($this->recipients) == 0) {
157
            return false;
158
        }
159
160
        /*
161
         * Try to build conf file
162
         */
163
        $config_file = $this->phpci->buildPath.DIRECTORY_SEPARATOR.'.sendxmpprc';
164
        if (is_null($this->findConfigFile())) {
165
            file_put_contents($config_file, $this->getConfigFormat());
166
            chmod($config_file, 0600);
167
        }
168
169
        /*
170
         * Enabled ssl for connection
171
         */
172
        $tls = '';
173
        if ($this->tls) {
174
            $tls = ' -t';
175
        }
176
177
        $message_file = $this->phpci->buildPath.DIRECTORY_SEPARATOR.uniqid('xmppmessage');
178
        if ($this->buildMessage($message_file) === false) {
179
            return false;
180
        }
181
182
        /*
183
         * Send XMPP notification for all recipients
184
         */
185
        $cmd = $sendxmpp.'%s -f %s -m %s %s';
186
        $recipients = implode(' ', $this->recipients);
187
188
        $success = $this->phpci->executeCommand($cmd, $tls, $config_file, $message_file, $recipients);
189
190
        echo $this->phpci->getLastOutput();
191
192
        /*
193
         * Remove temp message file
194
         */
195
        $this->phpci->executeCommand('rm -rf '.$message_file);
196
197
        return $success;
198
    }
199
200
    /**
201
     * @param $message_file
202
     *
203
     * @return int
204
     */
205
    protected function buildMessage($message_file)
206
    {
207
        if ($this->build->isSuccessful()) {
208
            $message = '✔ ['.$this->build->getProjectTitle().'] Build #'.$this->build->getId().' successful';
209
        } else {
210
            $message = '✘ ['.$this->build->getProjectTitle().'] Build #'.$this->build->getId().' failure';
211
        }
212
213
        $message .= ' ('.strftime($this->date_format).')';
214
215
        return file_put_contents($message_file, $message);
216
    }
217
}
218