summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Common/CommandActions.php
diff options
context:
space:
mode:
Diffstat (limited to 'library/Icingadb/Common/CommandActions.php')
-rw-r--r--library/Icingadb/Common/CommandActions.php21
1 files changed, 14 insertions, 7 deletions
diff --git a/library/Icingadb/Common/CommandActions.php b/library/Icingadb/Common/CommandActions.php
index 2cd13fe..af7d194 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 @@ use ipl\Web\Url;
*/
trait CommandActions
{
- /** @var Query $commandTargets */
+ /** @var null|Query|array<Model> $commandTargets */
protected $commandTargets;
/** @var Model $commandTargetModel */
@@ -51,14 +52,14 @@ trait CommandActions
/**
* Fetch command targets
*
- * @return Query|Model[]
+ * @return Query|array<Model>
*/
abstract protected function fetchCommandTargets();
/**
* Get command targets
*
- * @return Query|Model[]
+ * @return Query|array<Model>
*/
protected function getCommandTargets()
{
@@ -73,14 +74,19 @@ trait CommandActions
* 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();
}
}
@@ -139,7 +145,8 @@ trait CommandActions
protected function handleCommandForm($form)
{
$isXhr = $this->getRequest()->isXmlHttpRequest();
- if ($isXhr && $this->getRequest()->isApiRequest()) {
+ $isApi = $this->getRequest()->isApiRequest();
+ if ($isXhr && $isApi) {
// Prevents the framework already, this is just a fail-safe
$this->httpBadRequest('Responding with JSON during a Web request is not supported');
}
@@ -151,7 +158,7 @@ trait CommandActions
$form->setObjects($this->getCommandTargets());
- if ($isXhr) {
+ if (! $isApi || $isXhr) {
$this->handleWebRequest($form);
} else {
$this->handleApiRequest($form);