php-censor/src/Plugin/HipchatNotify.php

94 lines
2.4 KiB
PHP
Raw Normal View History

2014-05-13 17:56:05 +02:00
<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Plugin;
2014-05-13 17:56:05 +02:00
2016-07-19 20:28:11 +02:00
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
2016-05-09 08:20:26 +02:00
use HipChat\HipChat;
2014-05-13 17:56:05 +02:00
/**
* Hipchat Plugin
*
2017-03-04 16:39:56 +01:00
* @author James Inman <james@jamesinman.co.uk>
2014-05-13 17:56:05 +02:00
*/
2016-07-11 18:00:04 +02:00
class HipchatNotify extends Plugin
2014-05-13 17:56:05 +02:00
{
2015-02-12 15:11:58 +01:00
protected $authToken;
protected $color;
protected $notify;
2016-07-11 18:00:04 +02:00
protected $userAgent;
protected $cookie;
protected $message;
protected $room;
2014-05-13 17:56:05 +02:00
2017-01-11 16:15:54 +01:00
/**
* @return string
*/
public static function pluginName()
{
return 'hipchat_notify';
}
/**
2016-07-11 18:00:04 +02:00
* {@inheritdoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
2014-05-13 17:56:05 +02:00
{
parent::__construct($builder, $build, $options);
2014-05-13 17:56:05 +02:00
2016-07-19 13:05:02 +02:00
$this->userAgent = "PHP Censor/1.0";
2016-07-11 18:00:04 +02:00
$this->cookie = "php-censor-cookie";
2014-05-13 17:56:05 +02:00
if (is_array($options) && isset($options['authToken']) && isset($options['room'])) {
$this->authToken = $options['authToken'];
$this->room = $options['room'];
if (isset($options['message'])) {
$this->message = $options['message'];
} else {
$this->message = '%PROJECT_TITLE% built at %BUILD_URI%';
2014-05-13 17:56:05 +02:00
}
if (isset($options['color'])) {
$this->color = $options['color'];
} else {
$this->color = 'yellow';
}
if (isset($options['notify'])) {
$this->notify = $options['notify'];
} else {
$this->notify = false;
}
2014-05-13 17:56:05 +02:00
} else {
throw new \Exception('Please define room and authToken for hipchat_notify plugin.');
2014-05-13 17:56:05 +02:00
}
}
/**
* Run the HipChat plugin.
* @return bool
*/
2014-05-13 17:56:05 +02:00
public function execute()
{
2016-05-09 08:20:26 +02:00
$hipChat = new HipChat($this->authToken);
$message = $this->builder->interpolate($this->message);
2014-05-13 17:56:05 +02:00
$result = true;
2014-05-13 17:56:05 +02:00
if (is_array($this->room)) {
foreach ($this->room as $room) {
2016-07-19 13:05:02 +02:00
if (!$hipChat->message_room($room, 'PHP Censor', $message, $this->notify, $this->color)) {
$result = false;
}
2014-05-13 17:56:05 +02:00
}
} else {
2016-07-19 13:05:02 +02:00
if (!$hipChat->message_room($this->room, 'PHP Censor', $message, $this->notify, $this->color)) {
$result = false;
}
2014-05-13 17:56:05 +02:00
}
return $result;
2014-05-13 17:56:05 +02:00
}
}