SyncRule: add helper allowing to apply changes

This commit is contained in:
Thomas Gelf 2016-04-22 13:40:05 +02:00
parent 4cc70311f2
commit e88d490021
1 changed files with 29 additions and 2 deletions

View File

@ -28,6 +28,8 @@ class SyncRule extends DbObject
'last_attempt' => null, 'last_attempt' => null,
); );
private $sync;
private $filter; private $filter;
public function listInvolvedSourceIds() public function listInvolvedSourceIds()
@ -73,14 +75,23 @@ class SyncRule extends DbObject
return $this->filter()->matches($row); return $this->filter()->matches($row);
} }
public function checkForChanges() public function checkForChanges($apply = false)
{ {
$hadChanges = false;
Benchmark::measure('Checking sync rule ' . $this->rule_name); Benchmark::measure('Checking sync rule ' . $this->rule_name);
try { try {
$sync = new Sync($this); $sync = $this->sync();
if ($sync->hasModifications()) { if ($sync->hasModifications()) {
Benchmark::measure('Got modifications for sync rule ' . $this->rule_name); Benchmark::measure('Got modifications for sync rule ' . $this->rule_name);
$this->sync_state = 'pending-changes'; $this->sync_state = 'pending-changes';
if ($apply && $sync->apply()) {
Benchmark::measure('Successfully synced rule ' . $rule->rule_name);
$this->sync_state = 'in-sync';
}
$hadChanges = true;
} else { } else {
Benchmark::measure('No modifications for sync rule ' . $this->rule_name); Benchmark::measure('No modifications for sync rule ' . $this->rule_name);
$this->sync_state = 'in-sync'; $this->sync_state = 'in-sync';
@ -95,6 +106,22 @@ class SyncRule extends DbObject
if ($this->hasBeenModified()) { if ($this->hasBeenModified()) {
$this->store(); $this->store();
} }
return $hadChanges;
}
public function applyChanges()
{
return $this->checkForChanges(true);
}
protected function sync()
{
if ($this->sync === null) {
$this->sync = new Sync($this);
}
return $this->sync;
} }
protected function filter() protected function filter()