aboutsummaryrefslogtreecommitdiff
path: root/src/phorkie/Notificator/Webhook.php
blob: 041efa854e274620a3a67e9c191e1cf2233d6849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace phorkie;

/**
 * Send out webhook callbacks when something happens
 */
class Notificator_Webhook
{
    protected $config;

    public function __construct($config)
    {
        $this->config = $config;
    }

    /**
     * Call webhook URLs with our payload
     */
    public function send($event, Repository $repo)
    {
        if (count($this->config) == 0) {
            return;
        }
        
        /* slightly inspired by
           https://help.github.com/articles/post-receive-hooks */
        $payload = (object) array(
            'event'  => $event,
            'author' => array(
                'name'  => $_SESSION['name'],
                'email' => $_SESSION['email']
            ),
            'repository' => array(
                'name'        => $repo->getTitle(),
                'url'         => $repo->getLink('display', null, true),
                'description' => $repo->getDescription(),
                'owner'       => $repo->getOwner()
            )
        );
        foreach ($this->config as $url) {
            $req = new \HTTP_Request2($url);
            $req->setMethod(\HTTP_Request2::METHOD_POST)
                ->setHeader('Content-Type: application/vnd.phorkie.webhook+json')
                ->setBody(json_encode($payload));
            try {
                $response = $req->send();
                //FIXME log response codes != 200
            } catch (HTTP_Request2_Exception $e) {
                //FIXME log exceptions
            }
        }
    }
}
?>