GroupMembershipResolver: Add interfaces to be able to check before updating

This commit is contained in:
Markus Frosch 2018-05-17 12:59:58 +02:00
parent 325047260c
commit e46a610b5f

View File

@ -47,6 +47,9 @@ abstract class GroupMembershipResolver
/** @var bool */
protected $deferred = false;
/** @var bool */
protected $checked = false;
/** @var bool */
protected $useTransactions = false;
@ -67,6 +70,33 @@ abstract class GroupMembershipResolver
return $this->clearGroups()->clearObjects()->refreshDb(true);
}
public function checkDb()
{
if ($this->checked) {
return $this;
}
if ($this->isDeferred()) {
// ensure we are not working with cached data
IcingaTemplateRepository::clear();
}
Benchmark::measure('Rechecking all objects');
$this->recheckAllObjects($this->getAppliedGroups());
if (empty($this->objects)) {
Benchmark::measure('Nothing to check, got no qualified object');
return $this;
}
Benchmark::measure('Recheck done, loading existing mappings');
$this->fetchStoredMappings();
Benchmark::measure('Got stored group mappings');
$this->checked = true;
return $this;
}
/**
* @param bool $force
* @return $this
@ -75,23 +105,18 @@ abstract class GroupMembershipResolver
public function refreshDb($force = false)
{
if ($force || ! $this->isDeferred()) {
if ($this->isDeferred()) {
// ensure we are not working with cached data
IcingaTemplateRepository::clear();
}
$this->checkDb();
Benchmark::measure('Rechecking all objects');
$this->recheckAllObjects($this->getAppliedGroups());
if (empty($this->objects)) {
Benchmark::measure('Nothing to check, got no qualified object');
return $this;
}
Benchmark::measure('Recheck done, loading existing mappings');
$this->fetchStoredMappings();
Benchmark::measure('Ready, going to store new mappings');
$this->storeNewMappings();
$this->removeOutdatedMappings();
Benchmark::measure('Updated group mappings in db');
}
return $this;
@ -235,6 +260,8 @@ abstract class GroupMembershipResolver
$this->assertBeenLoadedFromDb($group);
$this->groups[$group->get('id')] = $group;
$this->checked = false;
return $this;
}
@ -248,6 +275,8 @@ abstract class GroupMembershipResolver
$this->addGroup($group);
}
$this->checked = false;
return $this;
}
@ -277,15 +306,25 @@ abstract class GroupMembershipResolver
public function clearGroups()
{
$this->objects = array();
$this->checked = false;
return $this;
}
public function getNewMappings()
{
if ($this->newMappings !== null && $this->existingMappings !== null) {
return $this->getDifference($this->newMappings, $this->existingMappings);
} else {
return [];
}
}
/**
* @throws \Zend_Db_Adapter_Exception
*/
protected function storeNewMappings()
{
$diff = $this->getDifference($this->newMappings, $this->existingMappings);
$diff = $this->getNewMappings();
$count = count($diff);
if ($count === 0) {
return;
@ -327,9 +366,18 @@ abstract class GroupMembershipResolver
}
}
public function getOutdatedMappings()
{
if ($this->newMappings !== null && $this->existingMappings !== null) {
return $this->getDifference($this->existingMappings, $this->newMappings);
} else {
return [];
}
}
protected function removeOutdatedMappings()
{
$diff = $this->getDifference($this->existingMappings, $this->newMappings);
$diff = $this->getOutdatedMappings();
$count = count($diff);
if ($count === 0) {
return;