2023-03-01 05:28:18 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */
|
|
|
|
|
|
|
|
|
|
namespace Icinga\Module\Icingadb\Command\Object;
|
|
|
|
|
|
2023-08-10 07:43:43 -04:00
|
|
|
use ArrayIterator;
|
2024-03-26 11:15:06 -04:00
|
|
|
use Generator;
|
2023-03-01 05:28:18 -05:00
|
|
|
use Icinga\Module\Icingadb\Command\IcingaCommand;
|
2024-03-26 11:15:06 -04:00
|
|
|
use InvalidArgumentException;
|
2023-03-01 05:28:18 -05:00
|
|
|
use ipl\Orm\Model;
|
2024-03-26 11:15:06 -04:00
|
|
|
use LogicException;
|
2023-03-01 05:28:18 -05:00
|
|
|
use Traversable;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Base class for commands that involve monitored objects, i.e. hosts or services
|
|
|
|
|
*/
|
|
|
|
|
abstract class ObjectsCommand extends IcingaCommand
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Involved objects
|
|
|
|
|
*
|
|
|
|
|
* @var Traversable<Model>
|
|
|
|
|
*/
|
|
|
|
|
protected $objects;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the involved objects
|
|
|
|
|
*
|
2024-03-26 11:15:06 -04:00
|
|
|
* @param Traversable<Model> $objects Except generators
|
2023-03-01 05:28:18 -05:00
|
|
|
*
|
|
|
|
|
* @return $this
|
2024-03-26 11:15:06 -04:00
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException If a generator is passed
|
2023-03-01 05:28:18 -05:00
|
|
|
*/
|
|
|
|
|
public function setObjects(Traversable $objects): self
|
|
|
|
|
{
|
2024-03-26 11:15:06 -04:00
|
|
|
if ($objects instanceof Generator) {
|
|
|
|
|
throw new InvalidArgumentException('Generators are not supported');
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 05:28:18 -05:00
|
|
|
$this->objects = $objects;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 07:49:44 -04:00
|
|
|
/**
|
|
|
|
|
* Set the involved object
|
|
|
|
|
*
|
|
|
|
|
* @param Model $object
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*
|
|
|
|
|
* @deprecated Use setObjects() instead
|
|
|
|
|
*/
|
|
|
|
|
public function setObject(Model $object): self
|
|
|
|
|
{
|
2023-08-10 07:43:43 -04:00
|
|
|
return $this->setObjects(new ArrayIterator([$object]));
|
2023-07-11 07:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 05:28:18 -05:00
|
|
|
/**
|
|
|
|
|
* Get the involved objects
|
|
|
|
|
*
|
|
|
|
|
* @return Traversable
|
|
|
|
|
*/
|
|
|
|
|
public function getObjects(): Traversable
|
|
|
|
|
{
|
|
|
|
|
if ($this->objects === null) {
|
2024-03-26 11:15:06 -04:00
|
|
|
throw new LogicException(
|
2023-03-01 05:28:18 -05:00
|
|
|
'You are accessing an unset property. Please make sure to set it beforehand.'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->objects;
|
|
|
|
|
}
|
|
|
|
|
}
|