SlackNotifier   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 6
dl 0
loc 56
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B notify() 0 34 1
1
<?php
2
3
namespace Badger\Bundle\GameBundle\Notifier;
4
5
use Badger\Component\Game\Notifier\NotifierInterface;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\RequestException;
8
use GuzzleHttp\Psr7\Request;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Log\LoggerInterface;
11
12
/**
13
 * Slack implementation of the NotifierInterface.
14
 * It makes a CURL call to the slack api to send a message to a given channel.
15
 *
16
 * @author  Adrien Pétremann <[email protected]>
17
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
18
 */
19
class SlackNotifier implements NotifierInterface
20
{
21
    /** @var string */
22
    private $webhookUrl;
23
24
    /** @var LoggerInterface */
25
    private $logger;
26
27
    /**
28
     * @param string          $webhookUrl
29
     * @param LoggerInterface $logger
30
     */
31
    public function __construct($webhookUrl, LoggerInterface $logger)
32
    {
33
        $this->webhookUrl = $webhookUrl;
34
        $this->logger = $logger;
35
    }
36
37
    /**
38
     * @param mixed $data
39
     */
40
    public function notify($data)
41
    {
42
        $client = new Client();
43
        $request = new Request(
44
            'POST',
45
            $this->webhookUrl,
46
            ['Content-type' => 'application/json'],
47
            json_encode($data)
48
        );
49
50
        $promise = $client->sendAsync($request, ['timeout' => 10]);
51
52
        $promise->then(
53
            function(ResponseInterface $res) use ($data) {
54
                $this->logger->info(
55
                    sprintf(
56
                        'Request to SLACK webhook OK [%s] with data: %s',
57
                        $res->getStatusCode(),
58
                        json_encode($data)
59
                    )
60
                );
61
            },
62
            function(RequestException $e) {
63
                $this->logger->error(
64
                    sprintf(
65
                        'Request to SLACK webhook FAILED with message: %s',
66
                        $e->getMessage()
67
                    )
68
                );
69
            }
70
        );
71
72
        $promise->wait(false);
73
    }
74
}
75