TextFileSender.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2009 by Ivan Y. Khvostishkov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00012     final class TextFileSender implements MessageQueueSender
00013     {
00014         private $queue  = null;
00015         private $stream = null;
00016         
00017         public static function create()
00018         {
00019             return new self;
00020         }
00021         
00022         public function setQueue(MessageQueue $queue)
00023         {
00024             Assert::isInstance($queue, 'TextFileQueue');
00025             
00026             $this->queue = $queue;
00027             
00028             return $this;
00029         }
00030         
00034         public function getQueue()
00035         {
00036             return $this->queue;
00037         }
00038         
00039         public function send(Message $message)
00040         {
00041             if (!$this->queue)
00042                 throw new WrongStateException('you must set the queue first');
00043             
00044             Assert::isInstance($message, 'TextMessage');
00045             
00046             $this->getStream()->write(
00047                 $message->getTimestamp()->toString()."\t"
00048                 .str_replace(PHP_EOL, ' ', $message->getText()).PHP_EOL
00049             );
00050         }
00051         
00052         private function getStream()
00053         {
00054             if (!$this->stream) {
00055                 Assert::isNotNull($this->queue->getFileName());
00056                 
00057                 $this->stream = FileOutputStream::create(
00058                     $this->queue->getFileName(), true
00059                 );
00060             }
00061             
00062             return $this->stream;
00063         }
00064     }
00065 ?>