暫無描述

FlowdockHandler.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. use Monolog\Formatter\FormatterInterface;
  14. /**
  15. * Sends notifications through the Flowdock push API
  16. *
  17. * This must be configured with a FlowdockFormatter instance via setFormatter()
  18. *
  19. * Notes:
  20. * API token - Flowdock API token
  21. *
  22. * @author Dominik Liebler <liebler.dominik@gmail.com>
  23. * @see https://www.flowdock.com/api/push
  24. */
  25. class FlowdockHandler extends SocketHandler
  26. {
  27. /**
  28. * @var string
  29. */
  30. protected $apiToken;
  31. /**
  32. * @param string $apiToken
  33. * @param bool|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($apiToken, $level = Logger::DEBUG, $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)
  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. * @return FormatterInterface
  60. */
  61. protected function getDefaultFormatter()
  62. {
  63. throw new \InvalidArgumentException('The FlowdockHandler must be configured (via setFormatter) with an instance of Monolog\Formatter\FlowdockFormatter to function correctly');
  64. }
  65. /**
  66. * {@inheritdoc}
  67. *
  68. * @param array $record
  69. */
  70. protected function write(array $record)
  71. {
  72. parent::write($record);
  73. $this->closeSocket();
  74. }
  75. /**
  76. * {@inheritdoc}
  77. *
  78. * @param array $record
  79. * @return string
  80. */
  81. protected function generateDataStream($record)
  82. {
  83. $content = $this->buildContent($record);
  84. return $this->buildHeader($content) . $content;
  85. }
  86. /**
  87. * Builds the body of API call
  88. *
  89. * @param array $record
  90. * @return string
  91. */
  92. private function buildContent($record)
  93. {
  94. return json_encode($record['formatted']['flowdock']);
  95. }
  96. /**
  97. * Builds the header of the API Call
  98. *
  99. * @param string $content
  100. * @return string
  101. */
  102. private function buildHeader($content)
  103. {
  104. $header = "POST /v1/messages/team_inbox/" . $this->apiToken . " HTTP/1.1\r\n";
  105. $header .= "Host: api.flowdock.com\r\n";
  106. $header .= "Content-Type: application/json\r\n";
  107. $header .= "Content-Length: " . strlen($content) . "\r\n";
  108. $header .= "\r\n";
  109. return $header;
  110. }
  111. }