1 | <?php declare(strict_types=1);
|
---|
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\Level;
|
---|
15 | use Monolog\Utils;
|
---|
16 | use Monolog\Formatter\FlowdockFormatter;
|
---|
17 | use Monolog\Formatter\FormatterInterface;
|
---|
18 | use Monolog\LogRecord;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Sends notifications through the Flowdock push API
|
---|
22 | *
|
---|
23 | * This must be configured with a FlowdockFormatter instance via setFormatter()
|
---|
24 | *
|
---|
25 | * Notes:
|
---|
26 | * API token - Flowdock API token
|
---|
27 | *
|
---|
28 | * @author Dominik Liebler <liebler.dominik@gmail.com>
|
---|
29 | * @see https://www.flowdock.com/api/push
|
---|
30 | * @deprecated Since 2.9.0 and 3.3.0, Flowdock was shutdown we will thus drop this handler in Monolog 4
|
---|
31 | */
|
---|
32 | class FlowdockHandler extends SocketHandler
|
---|
33 | {
|
---|
34 | protected string $apiToken;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @throws MissingExtensionException if OpenSSL is missing
|
---|
38 | */
|
---|
39 | public function __construct(
|
---|
40 | string $apiToken,
|
---|
41 | $level = Level::Debug,
|
---|
42 | bool $bubble = true,
|
---|
43 | bool $persistent = false,
|
---|
44 | float $timeout = 0.0,
|
---|
45 | float $writingTimeout = 10.0,
|
---|
46 | ?float $connectionTimeout = null,
|
---|
47 | ?int $chunkSize = null
|
---|
48 | ) {
|
---|
49 | if (!\extension_loaded('openssl')) {
|
---|
50 | throw new MissingExtensionException('The OpenSSL PHP extension is required to use the FlowdockHandler');
|
---|
51 | }
|
---|
52 |
|
---|
53 | parent::__construct(
|
---|
54 | 'ssl://api.flowdock.com:443',
|
---|
55 | $level,
|
---|
56 | $bubble,
|
---|
57 | $persistent,
|
---|
58 | $timeout,
|
---|
59 | $writingTimeout,
|
---|
60 | $connectionTimeout,
|
---|
61 | $chunkSize
|
---|
62 | );
|
---|
63 | $this->apiToken = $apiToken;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * @inheritDoc
|
---|
68 | */
|
---|
69 | public function setFormatter(FormatterInterface $formatter): HandlerInterface
|
---|
70 | {
|
---|
71 | if (!$formatter instanceof FlowdockFormatter) {
|
---|
72 | throw new \InvalidArgumentException('The FlowdockHandler requires an instance of Monolog\Formatter\FlowdockFormatter to function correctly');
|
---|
73 | }
|
---|
74 |
|
---|
75 | return parent::setFormatter($formatter);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Gets the default formatter.
|
---|
80 | */
|
---|
81 | protected function getDefaultFormatter(): FormatterInterface
|
---|
82 | {
|
---|
83 | throw new \InvalidArgumentException('The FlowdockHandler must be configured (via setFormatter) with an instance of Monolog\Formatter\FlowdockFormatter to function correctly');
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * @inheritDoc
|
---|
88 | */
|
---|
89 | protected function write(LogRecord $record): void
|
---|
90 | {
|
---|
91 | parent::write($record);
|
---|
92 |
|
---|
93 | $this->closeSocket();
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * @inheritDoc
|
---|
98 | */
|
---|
99 | protected function generateDataStream(LogRecord $record): string
|
---|
100 | {
|
---|
101 | $content = $this->buildContent($record);
|
---|
102 |
|
---|
103 | return $this->buildHeader($content) . $content;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Builds the body of API call
|
---|
108 | */
|
---|
109 | private function buildContent(LogRecord $record): string
|
---|
110 | {
|
---|
111 | return Utils::jsonEncode($record->formatted);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Builds the header of the API Call
|
---|
116 | */
|
---|
117 | private function buildHeader(string $content): string
|
---|
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 | }
|
---|