for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Badger\Bundle\GameBundle\Notifier;
use Badger\Component\Game\Notifier\NotifierInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
/**
* Slack implementation of the NotifierInterface.
* It makes a CURL call to the slack api to send a message to a given channel.
*
* @author Adrien Pétremann <[email protected]>
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
*/
class SlackNotifier implements NotifierInterface
{
/** @var string */
private $webhookUrl;
/** @var LoggerInterface */
private $logger;
* @param string $webhookUrl
* @param LoggerInterface $logger
public function __construct($webhookUrl, LoggerInterface $logger)
$this->webhookUrl = $webhookUrl;
$this->logger = $logger;
}
* @param mixed $data
public function notify($data)
$client = new Client();
$request = new Request(
'POST',
$this->webhookUrl,
['Content-type' => 'application/json'],
json_encode($data)
);
$promise = $client->sendAsync($request, ['timeout' => 10]);
$promise->then(
function(ResponseInterface $res) use ($data) {
$this->logger->info(
sprintf(
'Request to SLACK webhook OK [%s] with data: %s',
$res->getStatusCode(),
)
},
function(RequestException $e) {
$this->logger->error(
'Request to SLACK webhook FAILED with message: %s',
$e->getMessage()
$promise->wait(false);