diff --git a/application/forms/Command/CommandForm.php b/application/forms/Command/CommandForm.php index a535c6d69..49a05657f 100644 --- a/application/forms/Command/CommandForm.php +++ b/application/forms/Command/CommandForm.php @@ -5,8 +5,8 @@ namespace Icinga\Module\Icingadb\Forms\Command; use ArrayIterator; +use Countable; use Exception; -use Generator; use Icinga\Application\Logger; use Icinga\Module\Icingadb\Command\IcingaCommand; use Icinga\Module\Icingadb\Command\Transport\CommandTransport; @@ -16,6 +16,8 @@ use ipl\Html\Form; use ipl\Orm\Model; use ipl\Web\Common\CsrfCounterMeasure; +use Iterator; +use IteratorIterator; use Traversable; abstract class CommandForm extends Form @@ -25,7 +27,7 @@ abstract class CommandForm extends Form protected $defaultAttributes = ['class' => 'icinga-form icinga-controls']; - /** @var mixed */ + /** @var (Traversable&Countable)|array */ protected $objects; /** @var bool */ @@ -43,7 +45,7 @@ abstract class CommandForm extends Form /** * Set the objects to issue the command for * - * @param mixed $objects A traversable that is also countable + * @param (Traversable&Countable)|array $objects A traversable that is also countable * * @return $this */ @@ -57,7 +59,7 @@ public function setObjects($objects): self /** * Get the objects to issue the command for * - * @return mixed + * @return (Traversable&Countable)|array */ public function getObjects() { @@ -105,11 +107,11 @@ abstract protected function assembleSubmitButton(); /** * Get the commands to issue for the given objects * - * @param Traversable $objects + * @param Iterator $objects * * @return Traversable */ - abstract protected function getCommands(Traversable $objects): Traversable; + abstract protected function getCommands(Iterator $objects): Traversable; protected function assemble() { @@ -123,10 +125,15 @@ protected function assemble() protected function onSuccess() { - $errors = []; $objects = $this->getObjects(); + if (is_array($objects)) { + $objects = new ArrayIterator($objects); + } else { + $objects = new IteratorIterator($objects); + } - foreach ($this->getCommands(is_array($objects) ? new ArrayIterator($objects) : $objects) as $command) { + $errors = []; + foreach ($this->getCommands($objects) as $command) { try { $this->sendCommand($command); } catch (Exception $e) { @@ -159,21 +166,4 @@ protected function sendCommand(IcingaCommand $command) { (new CommandTransport())->send($command); } - - /** - * Yield the $objects the currently logged in user has the permission $permission for - * - * @param string $permission - * @param Traversable $objects - * - * @return Generator - */ - protected function filterGrantedOn(string $permission, Traversable $objects): Generator - { - foreach ($objects as $object) { - if ($this->isGrantedOn($permission, $object)) { - yield $object; - } - } - } } diff --git a/application/forms/Command/Instance/ToggleInstanceFeaturesForm.php b/application/forms/Command/Instance/ToggleInstanceFeaturesForm.php index cf14db8a4..86c0bd195 100644 --- a/application/forms/Command/Instance/ToggleInstanceFeaturesForm.php +++ b/application/forms/Command/Instance/ToggleInstanceFeaturesForm.php @@ -8,6 +8,7 @@ use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Web\Notification; use ipl\Web\FormDecorator\IcingaFormDecorator; +use Iterator; use Traversable; class ToggleInstanceFeaturesForm extends CommandForm @@ -133,7 +134,7 @@ protected function assembleSubmitButton() { } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { foreach ($this->features as $feature => $spec) { $featureState = $this->getElement($feature)->isChecked(); diff --git a/application/forms/Command/Object/AcknowledgeProblemForm.php b/application/forms/Command/Object/AcknowledgeProblemForm.php index 81b93e2e7..f5e792324 100644 --- a/application/forms/Command/Object/AcknowledgeProblemForm.php +++ b/application/forms/Command/Object/AcknowledgeProblemForm.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use DateInterval; use DateTime; use Icinga\Application\Config; @@ -14,9 +15,11 @@ use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; +use ipl\Orm\Model; use ipl\Validator\CallbackValidator; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; use function ipl\Stdlib\iterable_value_first; @@ -186,10 +189,13 @@ protected function assembleSubmitButton() (new IcingaFormDecorator())->decorate($this->getElement('btn_submit')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = $this->filterGrantedOn('icingadb/command/acknowledge-problem', $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/acknowledge-problem', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new AcknowledgeProblemCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/AddCommentForm.php b/application/forms/Command/Object/AddCommentForm.php index 9cd0754b7..e991e84ff 100644 --- a/application/forms/Command/Object/AddCommentForm.php +++ b/application/forms/Command/Object/AddCommentForm.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use DateInterval; use DateTime; use Icinga\Application\Config; @@ -14,9 +15,11 @@ use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; +use ipl\Orm\Model; use ipl\Validator\CallbackValidator; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; use function ipl\Stdlib\iterable_value_first; @@ -141,10 +144,13 @@ protected function assembleSubmitButton() (new IcingaFormDecorator())->decorate($this->getElement('btn_submit')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = $this->filterGrantedOn('icingadb/command/comment/add', $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/comment/add', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new AddCommentCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/CheckNowForm.php b/application/forms/Command/Object/CheckNowForm.php index b7a506ce7..eb1e03ba0 100644 --- a/application/forms/Command/Object/CheckNowForm.php +++ b/application/forms/Command/Object/CheckNowForm.php @@ -4,11 +4,13 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; -use Generator; +use CallbackFilterIterator; use Icinga\Module\Icingadb\Command\Object\ScheduleCheckCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Web\Notification; +use ipl\Orm\Model; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; class CheckNowForm extends CommandForm @@ -44,22 +46,17 @@ protected function assembleSubmitButton() ); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = (function () use ($objects): Generator { - foreach ($objects as $object) { - if ( - $this->isGrantedOn('icingadb/command/schedule-check', $object) - || ( - $object->active_checks_enabled - && $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object) - ) - ) { - yield $object; - } - } - })(); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/schedule-check', $object) + || ( + $object->active_checks_enabled + && $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object) + ); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new ScheduleCheckCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/DeleteCommentForm.php b/application/forms/Command/Object/DeleteCommentForm.php index 25275baf2..a35fc2e5f 100644 --- a/application/forms/Command/Object/DeleteCommentForm.php +++ b/application/forms/Command/Object/DeleteCommentForm.php @@ -4,12 +4,14 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; -use Generator; +use CallbackFilterIterator; use Icinga\Module\Icingadb\Command\Object\DeleteCommentCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Web\Notification; +use ipl\Orm\Model; use ipl\Web\Common\RedirectOption; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; class DeleteCommentForm extends CommandForm @@ -54,16 +56,13 @@ protected function assembleSubmitButton() ); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = (function () use ($objects): Generator { - foreach ($objects as $object) { - if ($this->isGrantedOn('icingadb/command/comment/delete', $object->{$object->object_type})) { - yield $object; - } - } - })(); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/comment/delete', $object->{$object->object_type}); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new DeleteCommentCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/DeleteDowntimeForm.php b/application/forms/Command/Object/DeleteDowntimeForm.php index 5f695b962..b13bcc524 100644 --- a/application/forms/Command/Object/DeleteDowntimeForm.php +++ b/application/forms/Command/Object/DeleteDowntimeForm.php @@ -4,12 +4,14 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; -use Generator; +use CallbackFilterIterator; use Icinga\Module\Icingadb\Command\Object\DeleteDowntimeCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Web\Notification; +use ipl\Orm\Model; use ipl\Web\Common\RedirectOption; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; class DeleteDowntimeForm extends CommandForm @@ -66,19 +68,14 @@ protected function assembleSubmitButton() ); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = (function () use ($objects): Generator { - foreach ($objects as $object) { - if ( - $this->isGrantedOn('icingadb/command/downtime/delete', $object->{$object->object_type}) - && $object->scheduled_by === null - ) { - yield $object; - } - } - })(); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $object->scheduled_by === null + && $this->isGrantedOn('icingadb/command/downtime/delete', $object->{$object->object_type}); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new DeleteDowntimeCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/ProcessCheckResultForm.php b/application/forms/Command/Object/ProcessCheckResultForm.php index 5764bf846..7c612bfaa 100644 --- a/application/forms/Command/Object/ProcessCheckResultForm.php +++ b/application/forms/Command/Object/ProcessCheckResultForm.php @@ -4,7 +4,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; -use Generator; +use CallbackFilterIterator; use Icinga\Module\Icingadb\Command\Object\ProcessCheckResultCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Module\Icingadb\Model\Host; @@ -15,6 +15,7 @@ use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; use function ipl\Stdlib\iterable_value_first; @@ -133,16 +134,14 @@ protected function assembleSubmitButton() (new IcingaFormDecorator())->decorate($this->getElement('btn_submit')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = (function () use ($objects): Generator { - foreach ($this->filterGrantedOn('icingadb/command/process-check-result', $objects) as $object) { - if ($object->passive_checks_enabled) { - yield $object; - } - } - })(); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $object->passive_checks_enabled + && $this->isGrantedOn('icingadb/command/process-check-result', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new ProcessCheckResultCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/RemoveAcknowledgementForm.php b/application/forms/Command/Object/RemoveAcknowledgementForm.php index 8697985ff..d81fe7f93 100644 --- a/application/forms/Command/Object/RemoveAcknowledgementForm.php +++ b/application/forms/Command/Object/RemoveAcknowledgementForm.php @@ -4,11 +4,14 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use Icinga\Module\Icingadb\Command\Object\RemoveAcknowledgementCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Module\Icingadb\Model\Host; use Icinga\Web\Notification; +use ipl\Orm\Model; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; use function ipl\Stdlib\iterable_value_first; @@ -62,10 +65,13 @@ protected function assembleSubmitButton() ); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = $this->filterGrantedOn('icingadb/command/remove-acknowledgement', $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/remove-acknowledgement', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new RemoveAcknowledgementCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/ScheduleCheckForm.php b/application/forms/Command/Object/ScheduleCheckForm.php index 9b32ea1a0..bdeba53cb 100644 --- a/application/forms/Command/Object/ScheduleCheckForm.php +++ b/application/forms/Command/Object/ScheduleCheckForm.php @@ -4,9 +4,9 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use DateInterval; use DateTime; -use Generator; use Icinga\Module\Icingadb\Command\Object\ScheduleCheckCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Module\Icingadb\Model\Host; @@ -14,8 +14,10 @@ use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; +use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; use function ipl\Stdlib\iterable_value_first; @@ -109,22 +111,17 @@ protected function assembleSubmitButton() (new IcingaFormDecorator())->decorate($this->getElement('btn_submit')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = (function () use ($objects): Generator { - foreach ($objects as $object) { - if ( - $this->isGrantedOn('icingadb/command/schedule-check', $object) - || ( - $object->active_checks_enabled - && $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object) - ) - ) { - yield $object; - } - } - })(); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/schedule-check', $object) + || ( + $object->active_checks_enabled + && $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object) + ); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new ScheduleCheckCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/ScheduleHostDowntimeForm.php b/application/forms/Command/Object/ScheduleHostDowntimeForm.php index bc211143f..a09b00d82 100644 --- a/application/forms/Command/Object/ScheduleHostDowntimeForm.php +++ b/application/forms/Command/Object/ScheduleHostDowntimeForm.php @@ -4,13 +4,16 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use DateInterval; use DateTime; use Icinga\Application\Config; use Icinga\Module\Icingadb\Command\Object\PropagateHostDowntimeCommand; use Icinga\Module\Icingadb\Command\Object\ScheduleHostDowntimeCommand; use Icinga\Web\Notification; +use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; +use Iterator; use Traversable; class ScheduleHostDowntimeForm extends ScheduleServiceDowntimeForm @@ -87,10 +90,13 @@ protected function assembleElements() $decorator->decorate($this->getElement('child_options')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = $this->filterGrantedOn('icingadb/command/downtime/schedule', $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/downtime/schedule', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { if (($childOptions = (int) $this->getValue('child_options'))) { $command = new PropagateHostDowntimeCommand(); diff --git a/application/forms/Command/Object/ScheduleServiceDowntimeForm.php b/application/forms/Command/Object/ScheduleServiceDowntimeForm.php index 184a4e8bf..b237408ae 100644 --- a/application/forms/Command/Object/ScheduleServiceDowntimeForm.php +++ b/application/forms/Command/Object/ScheduleServiceDowntimeForm.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use DateInterval; use DateTime; use Icinga\Application\Config; @@ -13,9 +14,11 @@ use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; +use ipl\Orm\Model; use ipl\Validator\CallbackValidator; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; class ScheduleServiceDowntimeForm extends CommandForm @@ -242,10 +245,13 @@ protected function assembleSubmitButton() (new IcingaFormDecorator())->decorate($this->getElement('btn_submit')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = $this->filterGrantedOn('icingadb/command/downtime/schedule', $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/downtime/schedule', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new ScheduleServiceDowntimeCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/SendCustomNotificationForm.php b/application/forms/Command/Object/SendCustomNotificationForm.php index dfb1e9630..4b0539e03 100644 --- a/application/forms/Command/Object/SendCustomNotificationForm.php +++ b/application/forms/Command/Object/SendCustomNotificationForm.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use Icinga\Application\Config; use Icinga\Module\Icingadb\Command\Object\SendCustomNotificationCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; @@ -12,8 +13,10 @@ use ipl\Html\Attributes; use ipl\Html\HtmlElement; use ipl\Html\Text; +use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; use ipl\Web\Widget\Icon; +use Iterator; use Traversable; use function ipl\Stdlib\iterable_value_first; @@ -108,10 +111,13 @@ protected function assembleSubmitButton() (new IcingaFormDecorator())->decorate($this->getElement('btn_submit')); } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { - $granted = $this->filterGrantedOn('icingadb/command/send-custom-notification', $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object): bool { + return $this->isGrantedOn('icingadb/command/send-custom-notification', $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new SendCustomNotificationCommand(); $command->setObjects($granted); diff --git a/application/forms/Command/Object/ToggleObjectFeaturesForm.php b/application/forms/Command/Object/ToggleObjectFeaturesForm.php index 50767da70..63530f6a2 100644 --- a/application/forms/Command/Object/ToggleObjectFeaturesForm.php +++ b/application/forms/Command/Object/ToggleObjectFeaturesForm.php @@ -4,12 +4,14 @@ namespace Icinga\Module\Icingadb\Forms\Command\Object; +use CallbackFilterIterator; use Icinga\Module\Icingadb\Command\Object\ToggleObjectFeatureCommand; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Web\Notification; use ipl\Html\FormElement\CheckboxElement; use ipl\Orm\Model; use ipl\Web\FormDecorator\IcingaFormDecorator; +use Iterator; use Traversable; class ToggleObjectFeaturesForm extends CommandForm @@ -156,7 +158,7 @@ protected function assembleSubmitButton() { } - protected function getCommands(Traversable $objects): Traversable + protected function getCommands(Iterator $objects): Traversable { foreach ($this->features as $feature => $spec) { if ($this->getElement($feature) instanceof CheckboxElement) { @@ -169,8 +171,11 @@ protected function getCommands(Traversable $objects): Traversable continue; } - $granted = $this->filterGrantedOn($spec['permission'], $objects); + $granted = new CallbackFilterIterator($objects, function (Model $object) use ($spec): bool { + return $this->isGrantedOn($spec['permission'], $object); + }); + $granted->rewind(); // Forwards the pointer to the first element if ($granted->valid()) { $command = new ToggleObjectFeatureCommand(); $command->setObjects($granted); diff --git a/library/Icingadb/Command/Object/ObjectsCommand.php b/library/Icingadb/Command/Object/ObjectsCommand.php index 3de6c83b6..f1a50d7ec 100644 --- a/library/Icingadb/Command/Object/ObjectsCommand.php +++ b/library/Icingadb/Command/Object/ObjectsCommand.php @@ -5,8 +5,11 @@ namespace Icinga\Module\Icingadb\Command\Object; use ArrayIterator; +use Generator; use Icinga\Module\Icingadb\Command\IcingaCommand; +use InvalidArgumentException; use ipl\Orm\Model; +use LogicException; use Traversable; /** @@ -24,12 +27,18 @@ abstract class ObjectsCommand extends IcingaCommand /** * Set the involved objects * - * @param Traversable $objects + * @param Traversable $objects Except generators * * @return $this + * + * @throws InvalidArgumentException If a generator is passed */ public function setObjects(Traversable $objects): self { + if ($objects instanceof Generator) { + throw new InvalidArgumentException('Generators are not supported'); + } + $this->objects = $objects; return $this; @@ -57,7 +66,7 @@ public function setObject(Model $object): self public function getObjects(): Traversable { if ($this->objects === null) { - throw new \LogicException( + throw new LogicException( 'You are accessing an unset property. Please make sure to set it beforehand.' ); } diff --git a/library/Icingadb/Common/CommandActions.php b/library/Icingadb/Common/CommandActions.php index ea5c2daf7..af7d19428 100644 --- a/library/Icingadb/Common/CommandActions.php +++ b/library/Icingadb/Common/CommandActions.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Icingadb\Common; +use Icinga\Exception\Http\HttpNotFoundException; use Icinga\Module\Icingadb\Forms\Command\CommandForm; use Icinga\Module\Icingadb\Forms\Command\Object\AcknowledgeProblemForm; use Icinga\Module\Icingadb\Forms\Command\Object\AddCommentForm; @@ -26,7 +27,7 @@ */ trait CommandActions { - /** @var Query $commandTargets */ + /** @var null|Query|array $commandTargets */ protected $commandTargets; /** @var Model $commandTargetModel */ @@ -51,14 +52,14 @@ protected function getFeatureStatus() /** * Fetch command targets * - * @return Query|Model[] + * @return Query|array */ abstract protected function fetchCommandTargets(); /** * Get command targets * - * @return Query|Model[] + * @return Query|array */ protected function getCommandTargets() { @@ -73,14 +74,19 @@ protected function getCommandTargets() * Get the model of the command targets * * @return Model + * @throws HttpNotFoundException If no command targets were found */ protected function getCommandTargetModel(): Model { if (! isset($this->commandTargetModel)) { $commandTargets = $this->getCommandTargets(); - if (is_array($commandTargets) && !empty($commandTargets)) { + if (empty($commandTargets) || count($commandTargets) === 0) { + throw new HttpNotFoundException('No command targets found'); + } + + if (is_array($commandTargets)) { $this->commandTargetModel = $commandTargets[0]; - } else { + } elseif ($commandTargets->count() > 0) { $this->commandTargetModel = $commandTargets->getModel(); } } diff --git a/library/Icingadb/Widget/Detail/MultiselectQuickActions.php b/library/Icingadb/Widget/Detail/MultiselectQuickActions.php index f398d80e5..b80ec9df2 100644 --- a/library/Icingadb/Widget/Detail/MultiselectQuickActions.php +++ b/library/Icingadb/Widget/Detail/MultiselectQuickActions.php @@ -67,6 +67,7 @@ protected function assemble() ) { $removeAckForm = (new RemoveAcknowledgementForm()) ->setAction($this->getLink('removeAcknowledgement')) + // TODO: This is a hack as for the button label the count of objects is used. setCount? setMultiple? ->setObjects(array_fill(0, $this->summary->$acks, null)); $this->add(Html::tag('li', $removeAckForm)); diff --git a/phpstan-baseline-7x.neon b/phpstan-baseline-7x.neon index e8d2562f2..6da352556 100644 --- a/phpstan-baseline-7x.neon +++ b/phpstan-baseline-7x.neon @@ -5,61 +5,6 @@ parameters: count: 1 path: application/controllers/EventController.php - - - message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/AcknowledgeProblemForm.php - - - - message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/AddCommentForm.php - - - - message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#" - count: 1 - path: application/forms/Command/Object/CheckNowForm.php - - - - message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/DeleteCommentForm.php - - - - message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/DeleteDowntimeForm.php - - - - message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/ProcessCheckResultForm.php - - - - message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/RemoveAcknowledgementForm.php - - - - message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/ScheduleCheckForm.php - - - - message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#" - count: 1 - path: application/forms/Command/Object/ScheduleHostDowntimeForm.php - - - - message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/ScheduleServiceDowntimeForm.php - - - - message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/SendCustomNotificationForm.php - - message: "#^Parameter \\#1 \\$str of function md5 expects string, mixed given\\.$#" count: 1 @@ -164,3 +109,8 @@ parameters: message: "#^Parameter \\#2 \\$str of function explode expects string, mixed given\\.$#" count: 1 path: library/Icingadb/Web/Controller.php + + - + message: "#^Parameter \\#1 \\$objects of method Icinga\\\\Module\\\\Icingadb\\\\Forms\\\\Command\\\\CommandForm\\:\\:setObjects\\(\\) expects array\\\\|\\(Countable&Traversable\\\\), array\\\\|false given\\.$#" + count: 1 + path: library/Icingadb/Widget/Detail/MultiselectQuickActions.php diff --git a/phpstan-baseline-8x.neon b/phpstan-baseline-8x.neon index 2cea59701..b5b85f7f9 100644 --- a/phpstan-baseline-8x.neon +++ b/phpstan-baseline-8x.neon @@ -5,61 +5,6 @@ parameters: count: 1 path: application/controllers/EventController.php - - - message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/AcknowledgeProblemForm.php - - - - message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/AddCommentForm.php - - - - message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#" - count: 1 - path: application/forms/Command/Object/CheckNowForm.php - - - - message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/DeleteCommentForm.php - - - - message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/DeleteDowntimeForm.php - - - - message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/ProcessCheckResultForm.php - - - - message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/RemoveAcknowledgementForm.php - - - - message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/ScheduleCheckForm.php - - - - message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#" - count: 1 - path: application/forms/Command/Object/ScheduleHostDowntimeForm.php - - - - message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/ScheduleServiceDowntimeForm.php - - - - message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/SendCustomNotificationForm.php - - message: "#^Parameter \\#1 \\$string of function md5 expects string, mixed given\\.$#" count: 1 @@ -159,3 +104,8 @@ parameters: message: "#^Parameter \\#2 \\$string of function explode expects string, mixed given\\.$#" count: 1 path: library/Icingadb/Web/Controller.php + + - + message: "#^Parameter \\#1 \\$objects of method Icinga\\\\Module\\\\Icingadb\\\\Forms\\\\Command\\\\CommandForm\\:\\:setObjects\\(\\) expects array\\\\|\\(Countable&Traversable\\\\), array\\ given\\.$#" + count: 1 + path: library/Icingadb/Widget/Detail/MultiselectQuickActions.php diff --git a/phpstan-baseline-standard.neon b/phpstan-baseline-standard.neon index 70fd385d2..5d23e8296 100644 --- a/phpstan-baseline-standard.neon +++ b/phpstan-baseline-standard.neon @@ -120,16 +120,6 @@ parameters: count: 1 path: application/controllers/CommentController.php - - - message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\CommentController\\:\\:\\$commandTargets \\(ipl\\\\Orm\\\\Query\\) does not accept array\\.$#" - count: 1 - path: application/controllers/CommentController.php - - - - message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\CommentController\\:\\:\\$commandTargets \\(ipl\\\\Orm\\\\Query\\) in isset\\(\\) is not nullable\\.$#" - count: 1 - path: application/controllers/CommentController.php - - message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\CommentController\\:\\:\\$comment \\(Icinga\\\\Module\\\\Icingadb\\\\Model\\\\Comment\\) does not accept ipl\\\\Orm\\\\Model\\.$#" count: 1 @@ -260,16 +250,6 @@ parameters: count: 1 path: application/controllers/DowntimeController.php - - - message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\DowntimeController\\:\\:\\$commandTargets \\(ipl\\\\Orm\\\\Query\\) does not accept array\\.$#" - count: 1 - path: application/controllers/DowntimeController.php - - - - message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\DowntimeController\\:\\:\\$commandTargets \\(ipl\\\\Orm\\\\Query\\) in isset\\(\\) is not nullable\\.$#" - count: 1 - path: application/controllers/DowntimeController.php - - message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\DowntimeController\\:\\:\\$downtime \\(Icinga\\\\Module\\\\Icingadb\\\\Model\\\\Downtime\\) does not accept ipl\\\\Orm\\\\Model\\.$#" count: 1 @@ -525,16 +505,6 @@ parameters: count: 1 path: application/controllers/HostController.php - - - message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\HostController\\:\\:\\$commandTargets \\(ipl\\\\Orm\\\\Query\\) does not accept array\\.$#" - count: 1 - path: application/controllers/HostController.php - - - - message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\HostController\\:\\:\\$commandTargets \\(ipl\\\\Orm\\\\Query\\) in isset\\(\\) is not nullable\\.$#" - count: 1 - path: application/controllers/HostController.php - - message: "#^Method Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\HostgroupController\\:\\:indexAction\\(\\) has no return type specified\\.$#" count: 1 @@ -695,11 +665,6 @@ parameters: count: 1 path: application/controllers/HostsController.php - - - message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\HostsController\\:\\:\\$commandTargets \\(ipl\\\\Orm\\\\Query\\) in isset\\(\\) is not nullable\\.$#" - count: 1 - path: application/controllers/HostsController.php - - message: "#^Method Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\MigrateController\\:\\:backendSupportAction\\(\\) has no return type specified\\.$#" count: 1 @@ -920,16 +885,6 @@ parameters: count: 1 path: application/controllers/ServiceController.php - - - message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\ServiceController\\:\\:\\$commandTargets \\(ipl\\\\Orm\\\\Query\\) does not accept array\\.$#" - count: 1 - path: application/controllers/ServiceController.php - - - - message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\ServiceController\\:\\:\\$commandTargets \\(ipl\\\\Orm\\\\Query\\) in isset\\(\\) is not nullable\\.$#" - count: 1 - path: application/controllers/ServiceController.php - - message: "#^Method Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\ServicegroupController\\:\\:indexAction\\(\\) has no return type specified\\.$#" count: 1 @@ -1130,11 +1085,6 @@ parameters: count: 1 path: application/controllers/ServicesController.php - - - message: "#^Property Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\ServicesController\\:\\:\\$commandTargets \\(ipl\\\\Orm\\\\Query\\) in isset\\(\\) is not nullable\\.$#" - count: 1 - path: application/controllers/ServicesController.php - - message: "#^Method Icinga\\\\Module\\\\Icingadb\\\\Controllers\\\\TacticalController\\:\\:completeAction\\(\\) has no return type specified\\.$#" count: 1 @@ -1260,11 +1210,6 @@ parameters: count: 1 path: application/forms/Command/CommandForm.php - - - message: "#^Method Icinga\\\\Module\\\\Icingadb\\\\Forms\\\\Command\\\\CommandForm\\:\\:filterGrantedOn\\(\\) has parameter \\$objects with no value type specified in iterable type Traversable\\.$#" - count: 1 - path: application/forms/Command/CommandForm.php - - message: "#^Method Icinga\\\\Module\\\\Icingadb\\\\Forms\\\\Command\\\\CommandForm\\:\\:onSuccess\\(\\) has no return type specified\\.$#" count: 1 @@ -1285,11 +1230,6 @@ parameters: count: 2 path: application/forms/Command/CommandForm.php - - - message: "#^Parameter \\#1 \\$objects of method Icinga\\\\Module\\\\Icingadb\\\\Forms\\\\Command\\\\CommandForm\\:\\:getCommands\\(\\) expects Traversable\\, mixed given\\.$#" - count: 1 - path: application/forms/Command/CommandForm.php - - message: "#^Parameter \\#1 \\$queryString of method Icinga\\\\Module\\\\Icingadb\\\\Forms\\\\Command\\\\CommandForm\\:\\:parseRestriction\\(\\) expects string, array\\ given\\.$#" count: 3 @@ -1350,11 +1290,6 @@ parameters: count: 1 path: application/forms/Command/Object/AcknowledgeProblemForm.php - - - message: "#^Parameter \\#1 \\$iterable of function ipl\\\\Stdlib\\\\iterable_value_first expects iterable, mixed given\\.$#" - count: 1 - path: application/forms/Command/Object/AcknowledgeProblemForm.php - - message: "#^Call to an undefined method ipl\\\\Html\\\\Contract\\\\FormElement\\:\\:isChecked\\(\\)\\.$#" count: 1 @@ -1375,46 +1310,16 @@ parameters: count: 1 path: application/forms/Command/Object/AddCommentForm.php - - - message: "#^Parameter \\#1 \\$iterable of function ipl\\\\Stdlib\\\\iterable_value_first expects iterable, mixed given\\.$#" - count: 1 - path: application/forms/Command/Object/AddCommentForm.php - - message: "#^Cannot call method getUsername\\(\\) on Icinga\\\\User\\|null\\.$#" count: 1 path: application/forms/Command/Object/DeleteCommentForm.php - - - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" - count: 1 - path: application/forms/Command/Object/DeleteDowntimeForm.php - - - - message: "#^Cannot access property \\$scheduled_by on mixed\\.$#" - count: 1 - path: application/forms/Command/Object/DeleteDowntimeForm.php - - message: "#^Cannot call method getUsername\\(\\) on Icinga\\\\User\\|null\\.$#" count: 1 path: application/forms/Command/Object/DeleteDowntimeForm.php - - - message: "#^Cannot access property \\$passive_checks_enabled on mixed\\.$#" - count: 1 - path: application/forms/Command/Object/ProcessCheckResultForm.php - - - - message: "#^Parameter \\#1 \\$iterable of function ipl\\\\Stdlib\\\\iterable_value_first expects iterable, mixed given\\.$#" - count: 2 - path: application/forms/Command/Object/ProcessCheckResultForm.php - - - - message: "#^Parameter \\#1 \\$objects of method Icinga\\\\Module\\\\Icingadb\\\\Command\\\\Object\\\\ObjectsCommand\\:\\:setObjects\\(\\) expects Traversable\\, Generator\\ given\\.$#" - count: 1 - path: application/forms/Command/Object/ProcessCheckResultForm.php - - message: "#^Parameter \\#1 \\$output of method Icinga\\\\Module\\\\Icingadb\\\\Command\\\\Object\\\\ProcessCheckResultCommand\\:\\:setOutput\\(\\) expects string, mixed given\\.$#" count: 1 @@ -1435,11 +1340,6 @@ parameters: count: 1 path: application/forms/Command/Object/RemoveAcknowledgementForm.php - - - message: "#^Parameter \\#1 \\$iterable of function ipl\\\\Stdlib\\\\iterable_value_first expects iterable, mixed given\\.$#" - count: 1 - path: application/forms/Command/Object/RemoveAcknowledgementForm.php - - message: "#^Call to an undefined method ipl\\\\Html\\\\Contract\\\\FormElement\\:\\:isChecked\\(\\)\\.$#" count: 1 @@ -1450,11 +1350,6 @@ parameters: count: 1 path: application/forms/Command/Object/ScheduleCheckForm.php - - - message: "#^Parameter \\#1 \\$iterable of function ipl\\\\Stdlib\\\\iterable_value_first expects iterable, mixed given\\.$#" - count: 1 - path: application/forms/Command/Object/ScheduleCheckForm.php - - message: "#^Call to an undefined method ipl\\\\Html\\\\Contract\\\\FormElement\\:\\:isChecked\\(\\)\\.$#" count: 2 @@ -1535,11 +1430,6 @@ parameters: count: 1 path: application/forms/Command/Object/SendCustomNotificationForm.php - - - message: "#^Parameter \\#1 \\$iterable of function ipl\\\\Stdlib\\\\iterable_value_first expects iterable, mixed given\\.$#" - count: 1 - path: application/forms/Command/Object/SendCustomNotificationForm.php - - message: "#^Cannot call method getAttributes\\(\\) on ipl\\\\Html\\\\Contract\\\\Wrappable\\|null\\.$#" count: 1 diff --git a/test/php/Lib/StrikingCommandTransport.php b/test/php/Lib/StrikingCommandTransport.php new file mode 100644 index 000000000..5e14ef9dd --- /dev/null +++ b/test/php/Lib/StrikingCommandTransport.php @@ -0,0 +1,28 @@ + ['host' => 'endpointA'], 'endpoint2' => ['host' => 'endpointB']]); + } + + public static function createTransport(ConfigObject $config): ApiCommandTransport + { + return (new class extends ApiCommandTransport { + protected function sendCommand(IcingaApiCommand $command) + { + throw new CommandTransportException(sprintf('%s strikes!', $this->getHost())); + } + })->setHost($config->host); + } +} diff --git a/test/php/library/Icingadb/Command/Transport/CommandTransportTest.php b/test/php/library/Icingadb/Command/Transport/CommandTransportTest.php new file mode 100644 index 000000000..63a1b6689 --- /dev/null +++ b/test/php/library/Icingadb/Command/Transport/CommandTransportTest.php @@ -0,0 +1,48 @@ +expectException(CommandTransportException::class); + $this->expectExceptionMessage('endpointB strikes!'); + + (new StrikingCommandTransport())->send( + (new AddCommentCommand()) + ->setExpireTime(42) + ->setAuthor('GLaDOS') + ->setComment('The cake is a lie') + ->setObjects(new \CallbackFilterIterator(new \ArrayIterator([ + (new Host())->setProperties(['name' => 'host1']), + (new Host())->setProperties(['name' => 'host2']), + ]), function ($host) { + return $host->name === 'host2'; + })) + ); + } + + public function testGeneratorsAreNotSupported() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Generators are not supported'); + + (new StrikingCommandTransport())->send( + (new AddCommentCommand()) + ->setExpireTime(42) + ->setAuthor('GLaDOS') + ->setComment('The cake is a lie') + ->setObjects((function () { + yield (new Host())->setProperties(['name' => 'host1']); + yield (new Host())->setProperties(['name' => 'host2']); + })()) + ); + } +}