summaryrefslogtreecommitdiffstats
path: root/library/Reporting/Database.php
diff options
context:
space:
mode:
Diffstat (limited to 'library/Reporting/Database.php')
-rw-r--r--library/Reporting/Database.php103
1 files changed, 74 insertions, 29 deletions
diff --git a/library/Reporting/Database.php b/library/Reporting/Database.php
index 3dabe17..7ea32a7 100644
--- a/library/Reporting/Database.php
+++ b/library/Reporting/Database.php
@@ -1,4 +1,5 @@
<?php
+
// Icinga Reporting | (c) 2018 Icinga GmbH | GPLv2
namespace Icinga\Module\Reporting;
@@ -6,53 +7,97 @@ namespace Icinga\Module\Reporting;
use Icinga\Application\Config;
use Icinga\Data\ResourceFactory;
use ipl\Sql;
+use PDO;
+use stdClass;
-trait Database
+final class Database
{
- protected function getDb($resource = null)
+ /** @var RetryConnection Database connection */
+ private static $instance;
+
+ private function __construct()
{
- $config = new Sql\Config(ResourceFactory::getResourceConfig(
- $resource ?: Config::module('reporting')->get('backend', 'resource', 'reporting')
- ));
+ }
- $config->options = [\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_OBJ];
- if ($config->db === 'mysql') {
- $config->options[\PDO::MYSQL_ATTR_INIT_COMMAND] = "SET SESSION SQL_MODE='STRICT_TRANS_TABLES"
- . ",NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'";
+ /**
+ * Get the database connection
+ *
+ * @return RetryConnection
+ */
+ public static function get(): RetryConnection
+ {
+ if (self::$instance === null) {
+ self::$instance = self::getDb();
}
- $conn = new RetryConnection($config);
-
- return $conn;
+ return self::$instance;
}
- protected function listTimeframes()
+ private static function getDb(): RetryConnection
{
- $select = (new Sql\Select())
- ->from('timeframe')
- ->columns(['id', 'name']);
-
- $timeframes = [];
+ $config = new Sql\Config(
+ ResourceFactory::getResourceConfig(
+ Config::module('reporting')->get('backend', 'resource', 'reporting')
+ )
+ );
- foreach ($this->getDb()->select($select) as $row) {
- $timeframes[$row->id] = $row->name;
+ $config->options = [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ];
+ if ($config->db === 'mysql') {
+ $config->options[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET SESSION SQL_MODE='STRICT_TRANS_TABLES"
+ . ",NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'";
}
- return $timeframes;
+ return new RetryConnection($config);
}
- protected function listTemplates()
+ /**
+ * List all reporting timeframes
+ *
+ * @return array<int, string>
+ */
+ public static function listTimeframes(): array
{
- $select = (new Sql\Select())
- ->from('template')
- ->columns(['id', 'name']);
+ return self::list(
+ (new Sql\Select())
+ ->from('timeframe')
+ ->columns(['id', 'name'])
+ );
+ }
- $templates = [];
+ /**
+ * List all reporting templates
+ *
+ * @return array<int, string>
+ */
+ public static function listTemplates(): array
+ {
+ return self::list(
+ (new Sql\Select())
+ ->from('template')
+ ->columns(['id', 'name'])
+ );
+ }
+
+ /**
+ * Helper method for list templates and timeframes
+ *
+ * @param Sql\Select $select
+ *
+ * @return array<int, string>
+ */
+ private static function list(Sql\Select $select): array
+ {
+ $result = [];
+ /** @var stdClass $row */
+ foreach (self::get()->select($select) as $row) {
+ /** @var int $id */
+ $id = $row->id;
+ /** @var string $name */
+ $name = $row->name;
- foreach ($this->getDb()->select($select) as $row) {
- $templates[$row->id] = $row->name;
+ $result[$id] = $name;
}
- return $templates;
+ return $result;
}
}