From c5d05454ca4193a2c161874a0014c68a541d6789 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 4 Jun 2018 08:39:12 +0200 Subject: [PATCH] RepositoryByObjectHelper: prepare for auth, clean --- .../Repository/RepositoryByObjectHelper.php | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/library/Director/Repository/RepositoryByObjectHelper.php b/library/Director/Repository/RepositoryByObjectHelper.php index 15500d2c..99568715 100644 --- a/library/Director/Repository/RepositoryByObjectHelper.php +++ b/library/Director/Repository/RepositoryByObjectHelper.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Director\Repository; +use Icinga\Authentication\Auth; use Icinga\Exception\ProgrammingError; use Icinga\Module\Director\Db; use Icinga\Module\Director\Objects\IcingaObject; @@ -13,6 +14,9 @@ trait RepositoryByObjectHelper /** @var Db */ protected $connection; + /** @var Auth */ + protected static $auth; + /** @var static[] */ protected static $instances = []; @@ -23,19 +27,37 @@ trait RepositoryByObjectHelper } /** - * @param $type + * @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 (!array_key_exists($type, self::$instances)) { + 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 @@ -61,4 +83,13 @@ trait RepositoryByObjectHelper $connection ); } + + protected static function auth() + { + if (self::$auth === null) { + self::$auth = Auth::getInstance(); + } + + return self::$auth; + } }