Branch: add a bunch of properties and getters

This commit is contained in:
Thomas Gelf 2021-08-23 08:50:44 +02:00
parent 53b2167925
commit c79045e9e2

View File

@ -3,13 +3,15 @@
namespace Icinga\Module\Director\Db\Branch;
use Icinga\Application\Icinga;
use Icinga\Authentication\Auth;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Hook\BranchSupportHook;
use Icinga\Web\Hook;
use Icinga\Web\Request;
use Icinga\Web\Session\Session;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use RuntimeException;
use stdClass;
/**
* Knows whether we're in a branch
@ -19,19 +21,32 @@ class Branch
/** @var UuidInterface|null */
protected $branchUuid;
/**
* @deprecated
* @param Session $session
* @return static
*/
public static function loadForSession(Session $session)
{
$self = new static();
// TODO: Load from branch if created.
$branch = $session->get('director/branch');
/** @var string */
protected $name;
if ($branch !== null) {
$self->branchUuid = Uuid::fromString($branch);
/** @var string */
protected $owner;
/** @var @var string */
protected $description;
/** @var bool */
protected $shouldBeMerged;
/** @var int */
protected $cntActivities;
public static function fromDbRow(stdClass $row)
{
$self = new static;
$self->branchUuid = Uuid::fromBytes($row->uuid);
$self->name = $row->branch_name;
$self->owner = $row->owner;
$self->shouldBeMerged = $row->should_be_merged === 'y';
if (isset($row->cnt_activities)) {
$self->cntActivities = $row->cnt_activities;
} else {
$self->cntActivities = 0;
}
return $self;
@ -40,10 +55,10 @@ class Branch
/**
* @return Branch
*/
public static function detect()
public static function detect(BranchStore $store)
{
try {
return static::forRequest(Icinga::app()->getRequest());
return static::forRequest(Icinga::app()->getRequest(), $store, Auth::getInstance());
} catch (\Exception $e) {
return new static();
}
@ -51,12 +66,14 @@ class Branch
/**
* @param Request $request
* @param Db $db
* @param Auth $auth
* @return Branch
*/
public static function forRequest(Request $request)
public static function forRequest(Request $request, BranchStore $store, Auth $auth)
{
if ($hook = static::optionalHook()) {
return $hook->getBranchForRequest($request);
return $hook->getBranchForRequest($request, $store, $auth);
}
return new Branch;
@ -109,6 +126,30 @@ class Branch
return $this->branchUuid === null;
}
/**
* @return bool
*/
public function shouldBeMerged()
{
return $this->shouldBeMerged;
}
/**
* @return bool
*/
public function isEmpty()
{
return $this->cntActivities === 0;
}
/**
* @return int
*/
public function getActivityCount()
{
return $this->cntActivities;
}
/**
* @return UuidInterface|null
*/
@ -116,4 +157,20 @@ class Branch
{
return $this->branchUuid;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getOwner()
{
return $this->owner;
}
}