FlowdockHandler.php 3.2 KB

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