FlowdockHandler.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. * @phpstan-import-type FormattedRecord from AbstractProcessingHandler
  27. */
  28. class FlowdockHandler extends SocketHandler
  29. {
  30. /**
  31. * @var string
  32. */
  33. protected $apiToken;
  34. /**
  35. * @throws MissingExtensionException if OpenSSL is missing
  36. */
  37. public function __construct(string $apiToken, $level = Logger::DEBUG, bool $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): HandlerInterface
  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. protected function getDefaultFormatter(): FormatterInterface
  59. {
  60. throw new \InvalidArgumentException('The FlowdockHandler must be configured (via setFormatter) with an instance of Monolog\Formatter\FlowdockFormatter to function correctly');
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. protected function write(array $record): void
  66. {
  67. parent::write($record);
  68. $this->closeSocket();
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. protected function generateDataStream(array $record): string
  74. {
  75. $content = $this->buildContent($record);
  76. return $this->buildHeader($content) . $content;
  77. }
  78. /**
  79. * Builds the body of API call
  80. *
  81. * @phpstan-param FormattedRecord $record
  82. */
  83. private function buildContent(array $record): string
  84. {
  85. return Utils::jsonEncode($record['formatted']['flowdock']);
  86. }
  87. /**
  88. * Builds the header of the API Call
  89. */
  90. private function buildHeader(string $content): string
  91. {
  92. $header = "POST /v1/messages/team_inbox/" . $this->apiToken . " HTTP/1.1\r\n";
  93. $header .= "Host: api.flowdock.com\r\n";
  94. $header .= "Content-Type: application/json\r\n";
  95. $header .= "Content-Length: " . strlen($content) . "\r\n";
  96. $header .= "\r\n";
  97. return $header;
  98. }
  99. }