diff options
Diffstat (limited to 'library/Director/Repository')
-rw-r--r-- | library/Director/Repository/IcingaTemplateRepository.php | 122 | ||||
-rw-r--r-- | library/Director/Repository/RepositoryByObjectHelper.php | 99 |
2 files changed, 221 insertions, 0 deletions
diff --git a/library/Director/Repository/IcingaTemplateRepository.php b/library/Director/Repository/IcingaTemplateRepository.php new file mode 100644 index 0000000..ed3b1d0 --- /dev/null +++ b/library/Director/Repository/IcingaTemplateRepository.php @@ -0,0 +1,122 @@ +<?php + +namespace Icinga\Module\Director\Repository; + +use Icinga\Module\Director\Db; +use Icinga\Module\Director\Objects\IcingaObject; +use Icinga\Module\Director\Resolver\TemplateTree; + +class IcingaTemplateRepository +{ + use RepositoryByObjectHelper; + + /** @var TemplateTree */ + protected $tree; + + protected $loadedById = []; + + /** + * @return TemplateTree + */ + public function tree() + { + if ($this->tree === null) { + $this->tree = new TemplateTree($this->type, $this->connection); + } + + return $this->tree; + } + + /** + * @param IcingaObject $object + * @param bool $recursive + * @return IcingaObject[] + * @throws \Icinga\Exception\NotFoundError + */ + public function getTemplatesFor(IcingaObject $object, $recursive = false) + { + if ($recursive) { + $ids = $this->tree()->listAncestorIdsFor($object); + } else { + $ids = $this->tree()->listParentIdsFor($object); + } + + return $this->getTemplatesForIds($ids, $object); + } + + /** + * @param array $ids + * @param IcingaObject $object + * @return IcingaObject[] + * @throws \Icinga\Exception\NotFoundError + */ + public function getTemplatesForIds(array $ids, IcingaObject $object) + { + $templates = []; + foreach ($ids as $id) { + if (! array_key_exists($id, $this->loadedById)) { + // TODO: load only missing ones at once + $this->loadedById[$id] = $object::loadWithAutoIncId( + $id, + $this->connection + ); + } + + $templates[$id] = $this->loadedById[$id]; + } + + return $templates; + } + + /** + * @param IcingaObject $object + * @param bool $recursive + * @return IcingaObject[] + * @throws \Icinga\Exception\NotFoundError + */ + public function getTemplatesIndexedByNameFor( + IcingaObject $object, + $recursive = false + ) { + $templates = []; + foreach ($this->getTemplatesFor($object, $recursive) as $template) { + $templates[$template->getObjectName()] = $template; + } + + return $templates; + } + + public function persistImportNames() + { + } + + public function storeChances(Db $db) + { + } + + public function listAllowedTemplateNames() + { + $type = $this->type; + $db = $this->connection->getDbAdapter(); + $table = 'icinga_' . $this->type; + + $query = $db->select() + ->from($table, 'object_name') + ->order('object_name'); + + if ($type !== 'command') { + $query->where('object_type = ?', 'template'); + } + + if (in_array($type, ['host', 'service'])) { + $query->where('template_choice_id IS NULL'); + } + + return $db->fetchCol($query); + } + + public static function clear() + { + static::clearInstances(); + } +} diff --git a/library/Director/Repository/RepositoryByObjectHelper.php b/library/Director/Repository/RepositoryByObjectHelper.php new file mode 100644 index 0000000..0d1dda3 --- /dev/null +++ b/library/Director/Repository/RepositoryByObjectHelper.php @@ -0,0 +1,99 @@ +<?php + +namespace Icinga\Module\Director\Repository; + +use Icinga\Authentication\Auth; +use Icinga\Module\Director\Db; +use Icinga\Module\Director\Objects\IcingaObject; +use RuntimeException; + +trait RepositoryByObjectHelper +{ + protected $type; + + /** @var Db */ + protected $connection; + + /** @var Auth */ + protected static $auth; + + /** @var static[] */ + protected static $instances = []; + + protected function __construct($type, Db $connection) + { + $this->type = $type; + $this->connection = $connection; + } + + /** + * @param string $type + * @return bool + */ + public static function hasInstanceForType($type) + { + return array_key_exists($type, self::$instances); + } + + /** + * @param string $type + * @param Db $connection + * @return static + */ + public static function instanceByType($type, Db $connection) + { + if (! static::hasInstanceForType($type)) { + self::$instances[$type] = new static($type, $connection); + } + + return self::$instances[$type]; + } + + /** + * @param IcingaObject $object + * @return bool + */ + public static function hasInstanceForObject(IcingaObject $object) + { + return static::hasInstanceForType($object->getShortTableName()); + } + + /** + * @param IcingaObject $object + * @param Db|null $connection + * @return static + */ + public static function instanceByObject(IcingaObject $object, Db $connection = null) + { + if (null === $connection) { + $connection = $object->getConnection(); + } + + if (! $connection) { + throw new RuntimeException(sprintf( + 'Cannot use repository for %s "%s" as it has no DB connection', + $object->getShortTableName(), + $object->getObjectName() + )); + } + + return static::instanceByType( + $object->getShortTableName(), + $connection + ); + } + + protected static function auth() + { + if (self::$auth === null) { + self::$auth = Auth::getInstance(); + } + + return self::$auth; + } + + protected static function clearInstances() + { + self::$instances = []; + } +} |