icingaweb2-module-director/library/Director/Web/Controller/BranchHelper.php

101 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2021-08-16 05:43:09 -04:00
<?php
namespace Icinga\Module\Director\Web\Controller;
use Icinga\Module\Director\Data\Db\DbObjectStore;
2021-08-16 05:43:09 -04:00
use Icinga\Module\Director\Db\Branch\Branch;
2021-08-23 02:54:34 -04:00
use Icinga\Module\Director\Db\Branch\BranchStore;
2022-08-25 08:42:17 -04:00
use Icinga\Module\Director\Db\Branch\BranchSupport;
use Icinga\Module\Director\Db\Branch\PreferredBranchSupport;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Widget\NotInBranchedHint;
use Ramsey\Uuid\UuidInterface;
2021-08-16 05:43:09 -04:00
trait BranchHelper
{
/** @var Branch */
protected $branch;
2021-08-23 02:54:34 -04:00
/** @var BranchStore */
protected $branchStore;
2021-08-16 05:43:09 -04:00
/** @var ?bool */
protected $hasPreferredBranch = null;
2021-08-16 05:43:09 -04:00
/**
* @return ?UuidInterface
2021-08-16 05:43:09 -04:00
*/
protected function getBranchUuid(): ?UuidInterface
2021-08-16 05:43:09 -04:00
{
return $this->getBranch()->getUuid();
}
/**
* @return Branch
*/
protected function getBranch(): Branch
2021-08-16 05:43:09 -04:00
{
if ($this->branch === null) {
/** @var ActionController $this */
2021-08-23 02:54:34 -04:00
$this->branch = Branch::forRequest($this->getRequest(), $this->getBranchStore(), $this->Auth());
2021-08-16 05:43:09 -04:00
}
return $this->branch;
}
2021-08-23 02:54:34 -04:00
/**
* @return BranchStore
*/
protected function getBranchStore(): BranchStore
2021-08-23 02:54:34 -04:00
{
if ($this->branchStore === null) {
$this->branchStore = new BranchStore($this->db());
}
return $this->branchStore;
}
/**
* @return bool
*/
protected function hasBranch(): bool
2021-08-16 05:43:09 -04:00
{
return $this->getBranchUuid() !== null;
}
protected function enableStaticObjectLoader($table): void
{
2022-08-25 08:42:17 -04:00
if (BranchSupport::existsForTableName($table)) {
IcingaObject::setDbObjectStore(new DbObjectStore($this->db(), $this->getBranch()));
}
}
/**
* @param string $subject
* @return bool
*/
protected function showNotInBranch($subject): bool
{
if ($this->getBranch()->isBranch()) {
$this->content()->add(new NotInBranchedHint($subject, $this->getBranch(), $this->Auth()));
return true;
}
return false;
}
protected function hasPreferredBranch(): bool
{
if ($this->hasPreferredBranch === null) {
$implementation = Branch::optionalHook();
if ($implementation instanceof PreferredBranchSupport) {
$this->hasPreferredBranch = $implementation->hasPreferredBranch($this->Auth());
} else {
$this->hasPreferredBranch = false;
}
}
return $this->hasPreferredBranch;
}
2021-08-16 05:43:09 -04:00
}