scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 1 <?php
2  
3 /*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Monolog\Handler;
13  
14 use Monolog\Logger;
15 use Monolog\Formatter\FlowdockFormatter;
16 use Monolog\Formatter\FormatterInterface;
17  
18 /**
19 * Sends notifications through the Flowdock push API
20 *
21 * This must be configured with a FlowdockFormatter instance via setFormatter()
22 *
23 * Notes:
24 * API token - Flowdock API token
25 *
26 * @author Dominik Liebler <liebler.dominik@gmail.com>
27 * @see https://www.flowdock.com/api/push
28 */
29 class FlowdockHandler extends SocketHandler
30 {
31 /**
32 * @var string
33 */
34 protected $apiToken;
35  
36 /**
37 * @param string $apiToken
38 * @param bool|int $level The minimum logging level at which this handler will be triggered
39 * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
40 *
41 * @throws MissingExtensionException if OpenSSL is missing
42 */
43 public function __construct($apiToken, $level = Logger::DEBUG, $bubble = true)
44 {
45 if (!extension_loaded('openssl')) {
46 throw new MissingExtensionException('The OpenSSL PHP extension is required to use the FlowdockHandler');
47 }
48  
49 parent::__construct('ssl://api.flowdock.com:443', $level, $bubble);
50 $this->apiToken = $apiToken;
51 }
52  
53 /**
54 * {@inheritdoc}
55 */
56 public function setFormatter(FormatterInterface $formatter)
57 {
58 if (!$formatter instanceof FlowdockFormatter) {
59 throw new \InvalidArgumentException('The FlowdockHandler requires an instance of Monolog\Formatter\FlowdockFormatter to function correctly');
60 }
61  
62 return parent::setFormatter($formatter);
63 }
64  
65 /**
66 * Gets the default formatter.
67 *
68 * @return FormatterInterface
69 */
70 protected function getDefaultFormatter()
71 {
72 throw new \InvalidArgumentException('The FlowdockHandler must be configured (via setFormatter) with an instance of Monolog\Formatter\FlowdockFormatter to function correctly');
73 }
74  
75 /**
76 * {@inheritdoc}
77 *
78 * @param array $record
79 */
80 protected function write(array $record)
81 {
82 parent::write($record);
83  
84 $this->closeSocket();
85 }
86  
87 /**
88 * {@inheritdoc}
89 *
90 * @param array $record
91 * @return string
92 */
93 protected function generateDataStream($record)
94 {
95 $content = $this->buildContent($record);
96  
97 return $this->buildHeader($content) . $content;
98 }
99  
100 /**
101 * Builds the body of API call
102 *
103 * @param array $record
104 * @return string
105 */
106 private function buildContent($record)
107 {
108 return json_encode($record['formatted']['flowdock']);
109 }
110  
111 /**
112 * Builds the header of the API Call
113 *
114 * @param string $content
115 * @return string
116 */
117 private function buildHeader($content)
118 {
119 $header = "POST /v1/messages/team_inbox/" . $this->apiToken . " HTTP/1.1\r\n";
120 $header .= "Host: api.flowdock.com\r\n";
121 $header .= "Content-Type: application/json\r\n";
122 $header .= "Content-Length: " . strlen($content) . "\r\n";
123 $header .= "\r\n";
124  
125 return $header;
126 }
127 }