RepositoryByObjectHelper: throw a nice exception

...when no Db is given
This commit is contained in:
Thomas Gelf 2017-08-18 16:42:21 +02:00
parent c51642a673
commit 2bf1b3f429

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Repository; namespace Icinga\Module\Director\Repository;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Db; use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
@ -37,7 +38,9 @@ trait RepositoryByObjectHelper
/** /**
* @param IcingaObject $object * @param IcingaObject $object
* @param Db|null $connection
* @return static * @return static
* @throws ProgrammingError
*/ */
public static function instanceByObject(IcingaObject $object, Db $connection = null) public static function instanceByObject(IcingaObject $object, Db $connection = null)
{ {
@ -46,12 +49,13 @@ trait RepositoryByObjectHelper
} }
if (! $connection) { if (! $connection) {
var_dump($object->hasBeenLoadedFromDb()); exit; throw new ProgrammingError(
echo '<pre>'; 'Cannot use repository for %s "%s" as it has no DB connection',
debug_print_backtrace(); $object->getShortTableName(),
echo '</pre>'; $object->getObjectName()
throw new \Exception('SDFA'); );
} }
return static::instanceByType( return static::instanceByType(
$object->getShortTableName(), $object->getShortTableName(),
$connection $connection