Completed
Pull Request — develop (#33)
by Michael
02:17
created

LegacyUrlPlugin::onRequestBeforeSend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
crap 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