Skip to content

Commit

Permalink
Rename synchronize to refresh, don't auto-refresh after updates
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Oct 24, 2020
1 parent 08261e0 commit 62dc51d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 47 deletions.
73 changes: 41 additions & 32 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,11 +644,11 @@ public function convert(self $into)
}

/**
* Synchronizes the current models attributes with the directory values.
* Refreshes the current models attributes with the directory values.
*
* @return bool
*/
public function synchronize()
public function refresh()
{
if ($model = $this->fresh()) {
$this->setRawAttributes($model->getAttributes());
Expand Down Expand Up @@ -907,7 +907,9 @@ public function save(array $attributes = [])

$this->fireModelEvent(new Events\Saving($this));

$saved = $this->exists ? $this->performUpdate() : $this->performInsert();
$saved = $this->exists
? $this->performUpdate()
: $this->performInsert();

if ($saved) {
$this->fireModelEvent(new Events\Saved($this));
Expand Down Expand Up @@ -946,15 +948,17 @@ protected function performInsert()
// Here we perform the insert of the object in the directory.
// We will also filter out any empty attribute values here,
// otherwise the LDAP server will return an error message.
if ($query->insert($this->getDn(), array_filter($this->getAttributes()))) {
$saved = $query->insert($this->getDn(), array_filter($this->getAttributes()));

if ($saved) {
$this->fireModelEvent(new Events\Created($this));

$this->exists = true;
$this->syncOriginal();

return $this->synchronize();
$this->exists = true;
}

return false;
return $saved;
}

/**
Expand All @@ -974,17 +978,17 @@ protected function performUpdate()

$this->fireModelEvent(new Events\Updating($this));

if ($this->newQuery()->update($this->dn, $modifications)) {
$saved = $this->newQuery()->update($this->dn, $modifications);

if ($saved) {
$this->fireModelEvent(new Events\Updated($this));

// Re-set the models modifications.
$this->modifications = [];
$this->syncOriginal();

// Re-sync the models attributes.
return $this->synchronize();
$this->modifications = [];
}

return false;
return $saved;
}

/**
Expand All @@ -1010,21 +1014,22 @@ public static function create(array $attributes = [])
*
* @param string $attribute The attribute to create
* @param mixed $value The value of the new attribute
* @param bool $sync Whether to re-sync all attributes
*
* @throws ModelDoesNotExistException
*
* @return bool
*/
public function createAttribute($attribute, $value, $sync = true)
public function createAttribute($attribute, $value)
{
$this->validateExistence();

if ($this->newQuery()->insertAttributes($this->dn, [$attribute => (array) $value])) {
return $sync ? $this->synchronize() : true;
$inserted = $this->newQuery()->insertAttributes($this->dn, [$attribute => (array) $value]);

if ($inserted) {
$this->addAttributeValue($attribute, $value);
}

return false;
return $inserted;
}

/**
Expand All @@ -1048,21 +1053,22 @@ public function update(array $attributes = [])
*
* @param string $attribute The attribute to modify
* @param mixed $value The new value for the attribute
* @param bool $sync Whether to re-sync all attributes
*
* @throws ModelDoesNotExistException
*
* @return bool
*/
public function updateAttribute($attribute, $value, $sync = true)
public function updateAttribute($attribute, $value)
{
$this->validateExistence();

if ($this->newQuery()->updateAttributes($this->dn, [$attribute => (array) $value])) {
return $sync ? $this->synchronize() : true;
$updated = $this->newQuery()->updateAttributes($this->dn, [$attribute => (array) $value]);

if ($updated) {
$this->addAttributeValue($attribute, $value);
}

return false;
return $updated;
}

/**
Expand Down Expand Up @@ -1144,7 +1150,6 @@ protected function deleteLeafNodes()
* Delete an attribute on the model.
*
* @param string|array $attributes The attribute(s) to delete
* @param bool $sync Whether to re-sync all attributes
*
* Delete specific values in attributes:
*
Expand All @@ -1158,7 +1163,7 @@ protected function deleteLeafNodes()
*
* @return bool
*/
public function deleteAttribute($attributes, $sync = true)
public function deleteAttribute($attributes)
{
$this->validateExistence();

Expand All @@ -1167,11 +1172,15 @@ public function deleteAttribute($attributes, $sync = true)
// assume it's an array of attributes to remove.
$attributes = is_string($attributes) ? [$attributes => []] : $attributes;

if ($this->newQuery()->deleteAttributes($this->dn, $attributes)) {
return $sync ? $this->synchronize() : true;
$deleted = $this->newQuery()->deleteAttributes($this->dn, $attributes);

if ($deleted) {
array_map([$this, 'offsetUnset'], array_keys($attributes));

$this->syncOriginal();
}

return false;
return $deleted;
}

/**
Expand Down Expand Up @@ -1222,7 +1231,9 @@ public function rename($rdn, $newParentDn = null, $deleteOldRdn = true)

$this->fireModelEvent(new Renaming($this, $rdn, $newParentDn));

if ($this->newQuery()->rename($this->dn, $rdn, $newParentDn, $deleteOldRdn)) {
$renamed = $this->newQuery()->rename($this->dn, $rdn, $newParentDn, $deleteOldRdn);

if ($renamed) {
// If the model was successfully renamed, we will set
// its new DN so any further updates to the model
// can be performed without any issues.
Expand All @@ -1240,11 +1251,9 @@ public function rename($rdn, $newParentDn = null, $deleteOldRdn = true)
= [reset($map[$modelNameAttribute])];

$this->fireModelEvent(new Renamed($this));

return true;
}

return false;
return $renamed;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/ActiveDirectory/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function test_restore()

class TestModelRestoreStub extends Entry
{
public function synchronize()
public function refresh()
{
return true;
}
Expand Down
7 changes: 3 additions & 4 deletions tests/Models/ModelEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public function test_create_fires_events()
$query = new Builder(new Connection([], $ldap));

$model = m::mock(Entry::class)->makePartial();
$model->shouldReceive('getConnectionContainer')->andReturn(Container::getInstance());
$model->shouldReceive('newQuery')->once()->andReturn($query);
$model->shouldReceive('synchronize')->once()->andReturnTrue();

$model->setDn('cn=foo,dc=bar,dc=baz');
$model->cn = 'foo';
Expand All @@ -82,8 +82,8 @@ public function test_updating_model_fires_events()
$query = new Builder(new Connection([], $ldap));

$model = m::mock(Entry::class)->makePartial();
$model->shouldReceive('getConnectionContainer')->andReturn(Container::getInstance());
$model->shouldReceive('newQuery')->once()->andReturn($query);
$model->shouldReceive('synchronize')->once()->andReturnTrue();

$model->setRawAttributes(['dn' => 'cn=foo,dc=bar,dc=baz']);
$model->cn = 'foo';
Expand All @@ -104,7 +104,6 @@ public function test_deleting_model_fires_events()

$model = m::mock(Entry::class)->makePartial();
$model->shouldReceive('newQuery')->once()->andReturn($query);
$model->shouldNotReceive('synchronize');

$model->setRawAttributes(['dn' => 'cn=foo,dc=bar,dc=baz']);
$model->cn = 'foo';
Expand All @@ -119,7 +118,7 @@ public function newQueryWithoutScopes()
return (new ModelQueryBuilderSaveStub(new Connection()))->setModel($this);
}

public function synchronize()
public function refresh()
{
return true;
}
Expand Down
25 changes: 15 additions & 10 deletions tests/Models/ModelQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ public function test_create()

$model = m::mock(Entry::class)->makePartial();
$model->shouldReceive('newQuery')->once()->andReturn($query);
$model->shouldReceive('synchronize')->once()->andReturnTrue();

$model->setDn('cn=foo,dc=bar,dc=baz');
$model->fill(['cn' => 'foo', 'objectclass' => 'bar']);
$model->fill($attributes = ['cn' => 'foo', 'objectclass' => 'bar']);

$this->assertTrue($model->save());
$this->assertEquals($model->getOriginal(), $model->getAttributes());
}

public function test_create_without_connection()
Expand All @@ -134,7 +135,6 @@ public function test_create_attribute()

$model = m::mock(Entry::class)->makePartial();
$model->shouldReceive('newQuery')->once()->andReturn($query);
$model->shouldReceive('synchronize')->once()->andReturnTrue();

$model->setRawAttributes(['dn' => 'foo']);
$this->assertTrue($model->createAttribute('bar', 'baz'));
Expand All @@ -158,7 +158,6 @@ public function test_update()

$model = m::mock(Entry::class)->makePartial();
$model->shouldReceive('newQuery')->once()->andReturn($query);
$model->shouldReceive('synchronize')->once()->andReturnTrue();

$model->setRawAttributes(['dn' => 'foo', 'cn' => 'bar']);
$model->cn = 'baz';
Expand Down Expand Up @@ -189,7 +188,6 @@ public function test_update_attribute()

$model = m::mock(Entry::class)->makePartial();
$model->shouldReceive('newQuery')->once()->andReturn($query);
$model->shouldReceive('synchronize')->once()->andReturnTrue();

$model->setRawAttributes(['dn' => 'foo']);
$this->assertTrue($model->updateAttribute('bar', 'baz'));
Expand All @@ -212,8 +210,9 @@ public function test_delete()
$model = m::mock(Entry::class)->makePartial();
$model->shouldReceive('newQuery')->once()->andReturn($query);

$model->setRawAttributes(['dn' => 'foo']);
$model->setRawAttributes(['dn' => 'foo', 'foo' => ['bar']]);
$this->assertTrue($model->delete());
$this->assertEquals(['foo' => ['bar']], $model->getAttributes());
}

public function test_delete_without_existing_model()
Expand All @@ -226,16 +225,22 @@ public function test_delete_without_existing_model()
public function test_delete_attribute()
{
$ldap = $this->newConnectedLdapMock();
$ldap->shouldReceive('modDelete')->once()->with('foo', ['bar' => []])->andReturnTrue();
$ldap->shouldReceive('modDelete')->once()->with('dn', ['foo' => []])->andReturnTrue();

$query = new Builder(new Connection([], $ldap));

$model = m::mock(Entry::class)->makePartial();
$model->shouldReceive('newQuery')->once()->andReturn($query);
$model->shouldReceive('synchronize')->once()->andReturnTrue();

$model->setRawAttributes(['dn' => 'foo']);
$this->assertTrue($model->deleteAttribute('bar'));
$model->setRawAttributes([
'dn' => 'dn',
'foo' => ['bar'],
'bar' => ['baz'],
]);

$this->assertTrue($model->deleteAttribute('foo'));
$this->assertEquals(['bar' => ['baz']], $model->getAttributes());
$this->assertEquals(['bar' => ['baz']], $model->getOriginal());
}

public function test_delete_attribute_without_existing_model()
Expand Down

0 comments on commit 62dc51d

Please sign in to comment.