From 1593406f315bc3551e06381bafc27c545318c2ad Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 3 Sep 2013 14:50:50 +0200 Subject: [PATCH] Refactor Acknowledgement command handling #refs 4580 --- .../controllers/CommandController.php | 6 +- .../forms/Command/AcknowledgeForm.php | 28 ++-- .../Monitoring/Command/AcknowledgeCommand.php | 122 ++++++++++++++++++ 3 files changed, 140 insertions(+), 16 deletions(-) create mode 100644 modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index ce1dad859..277e9107e 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -682,11 +682,11 @@ class Monitoring_CommandController extends ActionController $form->setRequest($this->getRequest()); $form->setConfiguration(Config::app()); - $this->setForm($form); - if ($form->IsSubmittedAndValid() === true) { - $this->target->acknowledge($this->view->objects, $form->getAcknowledgement()); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } + + $this->setForm($form); } /** diff --git a/modules/monitoring/application/forms/Command/AcknowledgeForm.php b/modules/monitoring/application/forms/Command/AcknowledgeForm.php index 00b35343b..cd7201ecd 100644 --- a/modules/monitoring/application/forms/Command/AcknowledgeForm.php +++ b/modules/monitoring/application/forms/Command/AcknowledgeForm.php @@ -29,9 +29,9 @@ namespace Icinga\Module\Monitoring\Form\Command; use \Icinga\Web\Form\Element\DateTimePicker; -use \Icinga\Protocol\Commandpipe\Acknowledgement; use \Icinga\Protocol\Commandpipe\Comment; use \Icinga\Util\DateTimeFactory; +use \Monitoring\Command\AcknowledgeCommand; /** * Form for problem acknowledgements @@ -156,25 +156,27 @@ class AcknowledgeForm extends CommandForm } /** - * Create acknowledgement from request data + * Create the acknowledgement command object * - * @return \Icinga\Protocol\Commandpipe\Acknowledgement + * @return AcknowledgeCommand */ - public function getAcknowledgement() + public function createCommand() { - $expireTime = -1; - if ($this->getValue('expire')) { - $expireTime = $this->getValue('expiretime'); - } - return new Acknowledgement( + $command = new AcknowledgeCommand(); + $command->setComment( new Comment( $this->getAuthorName(), $this->getValue('comment'), $this->getValue('persistent') - ), - $this->getValue('notify'), - $expireTime, - $this->getValue('sticky') + ) ); + $command->setNotify($this->getValue('notify')); + $command->setSticky($this->getValue('sticky')); + + if ($this->getValue('expire')) { + $command->setExpireTime($expireTime); + } + + return $command; } } diff --git a/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php b/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php new file mode 100644 index 000000000..f1af9c6a3 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php @@ -0,0 +1,122 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Monitoring\Command; + +use \Icinga\Protocol\Commandpipe\Command; +use \Icinga\Protocol\Commandpipe\CommandPipe; +use \Icinga\Protocol\Commandpipe\Acknowledgement; + +class AcknowledgeCommand extends MonitoringCommand implements Command +{ + /** + * When this acknowledgement should expire + * + * @var int + */ + private $expireTime = -1; + + /** + * Whether notifications are going to be sent out + * + * @var bool + */ + private $notify; + + /** + * Whether this acknowledgement is going to be stickied + * + * @var bool + */ + private $sticky; + + /** + * Set the time when this acknowledgement should expire + * + * @param int $expireTime + * @return self + */ + public function setExpireTime($expireTime) + { + $this->expireTime = $expireTime; + return $this; + } + + /** + * Set whether notifications should be sent out + * + * @param bool $state + * @return self + */ + public function setNotify($state) + { + $this->notify = $state; + return $this; + } + + /** + * Set whether this acknowledgement should be stickied + * + * @param bool $state + * @return self + */ + public function setSticky($state) + { + $this->sticky = $state; + return $this; + } + + /** + * Create the acknowledgement object + * + * @return \Icinga\Protocol\Commandpipe\Acknowledgement + */ + public function createAcknowledgement() + { + return new Acknowledgement( + $this->comment, + $this->notify, + $this->expireTime, + $this->sticky + ); + } + + /** + * @see Command::__toString() + */ + public function __toString() + { + if (isset($this->service_description)) { + $template = $this->createAcknowledgement()->getFormatString(CommandPipe::TYPE_SERVICE); + return sprintf($template, $this->hostname, $this->service_description); + } else { + $template = $this->createAcknowledgement()->getFormatString(CommandPipe::TYPE_HOST); + return sprintf($template, $this->hostname); + } + } +} \ No newline at end of file