diff --git a/src/Command/CheckCommand.php b/src/Command/CheckCommand.php index 6e9f76d..ae7b57e 100644 --- a/src/Command/CheckCommand.php +++ b/src/Command/CheckCommand.php @@ -10,6 +10,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use whatwedo\MonitorBundle\Manager\MonitoringManager; +use whatwedo\MonitorBundle\Monitoring\AttributeInterface; use whatwedo\MonitorBundle\Monitoring\Metric\AbstractMetric; use whatwedo\MonitorBundle\Monitoring\Sensor\AbstractSensor; use whatwedo\MonitorBundle\Util\StatusCodeDecider; @@ -43,20 +44,28 @@ private function printResult(SymfonyStyle $io, $result, $previousGroup = null, $ $rows = []; $subs = []; foreach ($items as $subGroup => $row) { - if ($row instanceof AbstractSensor) { - $rows[] = [ - sprintf('%s', $row->getState()->getCliColor(), $row->getState()->getIcon()), - $row->getName(), - $row->getDetails() ? json_encode($row->getDetails(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) : '', - ]; - } elseif ($row instanceof AbstractMetric) { - $rows[] = [ - sprintf('%s', $row->getState()->getCliColor(), $row->getState()->getIcon()), - $row->getName(), - $row->getValue(), - ]; - } elseif (is_array($row)) { + if (is_array($row)) { $subs[$subGroup] = $row; + + continue; + } + + try { + if ($row instanceof AbstractSensor) { + $rows[] = [ + sprintf('%s', $row->getState()->getCliColor(), $row->getState()->getIcon()), + $row->getName(), + $row->getDetails() ? json_encode($row->getDetails(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) : '', + ]; + } elseif ($row instanceof AbstractMetric) { + $rows[] = [ + sprintf('%s', $row->getState()->getCliColor(), $row->getState()->getIcon()), + $row->getName(), + $row->getValue(), + ]; + } + } catch (\LogicException) { + continue; } } diff --git a/src/Manager/MonitoringManager.php b/src/Manager/MonitoringManager.php index 7545a5b..ac31ffb 100644 --- a/src/Manager/MonitoringManager.php +++ b/src/Manager/MonitoringManager.php @@ -7,7 +7,9 @@ use whatwedo\MonitorBundle\Enums\MetricStateEnum; use whatwedo\MonitorBundle\Enums\SensorStateEnum; use whatwedo\MonitorBundle\Monitoring\AttributeInterface; +use whatwedo\MonitorBundle\Monitoring\Metric\MetricStateInterface; use whatwedo\MonitorBundle\Monitoring\Sensor\AbstractSensor; +use whatwedo\MonitorBundle\Monitoring\Sensor\SensorStateInterface; class MonitoringManager { @@ -106,15 +108,27 @@ public function getAttribute(string $className): AttributeInterface private function wasSuccessful(AttributeInterface $attribute): bool { - return $attribute instanceof AbstractSensor - ? $attribute->getState() === SensorStateEnum::SUCCESSFUL - : $attribute->getState() === MetricStateEnum::OK; + if ($attribute instanceof SensorStateInterface) { + return $attribute->getState() === SensorStateEnum::SUCCESSFUL; + } + + if ($attribute instanceof MetricStateInterface) { + return $attribute->getState() === MetricStateEnum::OK; + } + + return false; } - private function wasWarning(AttributeInterface $abstract): bool + private function wasWarning(AttributeInterface $attribute): bool { - return $abstract instanceof AbstractSensor - ? $abstract->getState() === SensorStateEnum::WARNING - : $abstract->getState() === MetricStateEnum::WARNING; + if ($attribute instanceof SensorStateInterface) { + return $attribute->getState() === SensorStateEnum::WARNING; + } + + if ($attribute instanceof MetricStateInterface) { + return $attribute->getState() === MetricStateEnum::WARNING; + } + + return false; } } diff --git a/src/Monitoring/Metric/AbstractMetric.php b/src/Monitoring/Metric/AbstractMetric.php index edcfbb4..ffdb522 100644 --- a/src/Monitoring/Metric/AbstractMetric.php +++ b/src/Monitoring/Metric/AbstractMetric.php @@ -7,7 +7,7 @@ use whatwedo\MonitorBundle\Enums\MetricStateEnum; use whatwedo\MonitorBundle\Monitoring\AttributeInterface; -abstract class AbstractMetric implements AttributeInterface +abstract class AbstractMetric implements AttributeInterface, MetricStateInterface { public null|int|float $value = null; @@ -16,7 +16,7 @@ abstract class AbstractMetric implements AttributeInterface public function getState(): MetricStateEnum { if ($this->state === null) { - throw new \RuntimeException(static::class.'::$state is not set.'); + throw new \LogicException(static::class.'::$state is not set.'); } return $this->state; diff --git a/src/Monitoring/Metric/MetricStateInterface.php b/src/Monitoring/Metric/MetricStateInterface.php new file mode 100644 index 0000000..d2e3eae --- /dev/null +++ b/src/Monitoring/Metric/MetricStateInterface.php @@ -0,0 +1,13 @@ +state === null) { - throw new \RuntimeException(__CLASS__.'::$state is not set.'); + throw new \LogicException(static::class.'::$state is not set.'); } return $this->state; diff --git a/src/Monitoring/Sensor/SensorStateInterface.php b/src/Monitoring/Sensor/SensorStateInterface.php new file mode 100644 index 0000000..77fc0b6 --- /dev/null +++ b/src/Monitoring/Sensor/SensorStateInterface.php @@ -0,0 +1,13 @@ + $object->getName(), ]; + try { + if ($object instanceof MetricStateInterface || $object instanceof SensorStateInterface) { + $data['state'] = $object->getState(); + } + } catch (\LogicException) { + } + if ($object instanceof AbstractSensor) { - $data['state'] = $object->getState(); $data['details'] = $object->getDetails(); } if ($object instanceof AbstractMetric) { - $data['state'] = $object->getState(); $data['value'] = $object->getValue(); }