FlowdockHandler.php 3.2 KB

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