LegacyUrlPlugin   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 3
c 2
b 1
f 1
lcom 1
cbo 2
dl 0
loc 46
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A onRequestBeforeSend() 0 14 1
A getSubscribedEvents() 0 6 1
1
<?php
2
3
namespace Crummy\Phlack\Bridge\Guzzle;
4
5
use Guzzle\Common\Event;
6
use Guzzle\Http\Message\RequestInterface;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
9
class LegacyUrlPlugin implements EventSubscriberInterface
10
{
11
    const BASE_URL = '%s.slack.com';
12
    const WEBHOOK_PATH = '/services/hooks/incoming-webhook';
13
14
    private $username;
15
    private $token;
16
17
    /**
18
     * @param $username
19
     * @param $token
20
     */
21 6
    public function __construct($username, $token)
22
    {
23 6
        $this->username = $username;
24 6
        $this->token = $token;
25 6
    }
26
27
    /**
28
     * @see EventSubscriberInterface
29
     */
30 3
    public static function getSubscribedEvents()
31
    {
32
        return [
33 3
            'request.before_send' => ['onRequestBeforeSend', -1000],
34 3
        ];
35
    }
36
37
    /**
38
     * @param Event $e
39
     */
40 1
    public function onRequestBeforeSend(Event $e)
41
    {
42
        /** @var RequestInterface $request */
43 1
        $request = $e['request'];
44
45 1
        $url = $request->getUrl(true)
46 1
            ->setScheme('https')
47 1
            ->setHost(sprintf(self::BASE_URL, $this->username))
48 1
            ->setPath(self::WEBHOOK_PATH);
49
50 1
        $request->setUrl($url)
51 1
            ->getQuery()
52 1
            ->add('token', $this->token);
53 1
    }
54
}
55