From 11c07edfbe28fee43f8f59fad205df90d4b3b0b1 Mon Sep 17 00:00:00 2001 From: Sebastian Goettschkes Date: Mon, 24 Sep 2012 13:14:51 +0200 Subject: [PATCH] Overwriting the generateDataStream method to format the Data sent to Pushover This way, the write method does not have to be overwritten. --- src/Monolog/Handler/PushoverHandler.php | 44 +++++++++---------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/src/Monolog/Handler/PushoverHandler.php b/src/Monolog/Handler/PushoverHandler.php index 886ae4bc..9334c4b7 100644 --- a/src/Monolog/Handler/PushoverHandler.php +++ b/src/Monolog/Handler/PushoverHandler.php @@ -42,37 +42,13 @@ class PushoverHandler extends SocketHandler $this->title = $title; } - /** - * Build the content to be sent through the socket, then connect - * (if necessary) and write to the socket - * - * @param array $record - * - * @throws \UnexpectedValueException - * @throws \RuntimeException - */ - public function write(array $record) + protected function generateDataStream($record) { - $data = $this->buildDataString($record); - $content = $this->buildContent($data); - - $this->connectIfNotConnected(); - $this->writeToSocket($content); + $content = $this->buildContentString($record); + return $this->buildHeaderAndAddContent($content); } - private function buildContent($data) - { - $content = "POST /1/messages.json HTTP/1.1\r\n"; - $content .= "Host: api.pushover.net\r\n"; - $content .= "Content-Type: application/x-www-form-urlencoded\r\n"; - $content .= "Content-Length: " . strlen($data) . "\r\n"; - $content .= "\r\n"; - $content .= $data; - - return $content; - } - - private function buildDataString($record) + private function buildContentString($record) { // Pushover has a limit of 512 characters on title and message combined. $maxMessageLength = 512 - strlen($this->title); @@ -89,4 +65,16 @@ class PushoverHandler extends SocketHandler return http_build_query($dataArray); } + + private function buildHeaderAndAddContent($content) + { + $header = "POST /1/messages.json HTTP/1.1\r\n"; + $header .= "Host: api.pushover.net\r\n"; + $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; + $header .= "Content-Length: " . strlen($content) . "\r\n"; + $header .= "\r\n"; + $header .= $content; + + return $header; + } } \ No newline at end of file