first work on remote fork notifications with linkback (webmention/pingback)
[phorkie.git] / src / phorkie / Notificator / Webhook.php
1 <?php
2 namespace phorkie;
3
4 /**
5  * Send out webhook callbacks when something happens
6  */
7 class Notificator_Webhook
8 {
9     protected $config;
10
11     public function __construct($config)
12     {
13         $this->config = $config;
14     }
15
16     /**
17      * Call webhook URLs with our payload
18      */
19     public function send($event, Repository $repo)
20     {
21         if (count($this->config) == 0) {
22             return;
23         }
24         
25         /* slightly inspired by
26            https://help.github.com/articles/post-receive-hooks */
27         $payload = (object) array(
28             'event'  => $event,
29             'author' => array(
30                 'name'  => $_SESSION['name'],
31                 'email' => $_SESSION['email']
32             ),
33             'repository' => array(
34                 'name'        => $repo->getTitle(),
35                 'url'         => $repo->getLink('display', null, true),
36                 'description' => $repo->getDescription(),
37                 'owner'       => $repo->getOwner()
38             )
39         );
40         foreach ($this->config as $url) {
41             $req = new \HTTP_Request2($url);
42             $req->setMethod(\HTTP_Request2::METHOD_POST)
43                 ->setHeader('Content-Type: application/vnd.phorkie.webhook+json')
44                 ->setBody(json_encode($payload));
45             try {
46                 $response = $req->send();
47                 //FIXME log response codes != 200
48             } catch (HTTP_Request2_Exception $e) {
49                 //FIXME log exceptions
50             }
51         }
52     }
53 }
54 ?>
55