Skip to content

Commit

Permalink
Merge pull request #510 from DirectoryTree/bug-508
Browse files Browse the repository at this point in the history
Bug 508 - Events not firing when updating group
  • Loading branch information
stevebauman authored Oct 7, 2022
2 parents 3e5af55 + 7897cab commit 6d8d242
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 19 deletions.
37 changes: 35 additions & 2 deletions src/Models/Concerns/HasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Closure;
use LdapRecord\Events\NullDispatcher;
use LdapRecord\Models\Events;
use LdapRecord\Models\Events\Event;
use LdapRecord\Support\Arr;

/** @mixin \LdapRecord\Models\Model */
trait HasEvents
Expand Down Expand Up @@ -38,7 +40,38 @@ protected static function withoutEvents(Closure $callback)
}

/**
* Fires the specified model event.
* Dispatch the given model events.
*
* @param string|array $events
* @param array $args
*
* @return void
*/
protected function dispatch($events, array $args = [])
{
foreach (Arr::wrap($events) as $name) {
$this->fireCustomModelEvent($name, $args);
}
}

/**
* Fire a custom model event.
*
* @param string $name
* @param array $args
*
* @return mixed
*/
protected function fireCustomModelEvent($name, array $args = [])
{
/** @psalm-suppress UndefinedClass */
$event = implode('\\', [Events::class, ucfirst($name)]);

return $this->fireModelEvent(new $event($this, ...$args));
}

/**
* Fire a model event.
*
* @param Event $event
*
Expand All @@ -50,7 +83,7 @@ protected function fireModelEvent(Event $event)
}

/**
* Listens to a model event.
* Listen to a model event.
*
* @param string $event
* @param Closure $listener
Expand Down
60 changes: 43 additions & 17 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1127,11 +1127,11 @@ public function save(array $attributes = [])
{
$this->fill($attributes);

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

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

$this->fireModelEvent(new Events\Saved($this));
$this->dispatch('saved');

$this->modifications = [];

Expand Down Expand Up @@ -1163,15 +1163,15 @@ protected function performInsert()
$this->setDn($this->getCreatableDn());
}

$this->fireModelEvent(new Events\Creating($this));
$this->dispatch('creating');

// Here we perform the insert of new object in the directory,
// but filter out any empty attributes before sending them
// to the server. LDAP servers will throw an exception if
// attributes have been given empty or null values.
$query->insert($this->getDn(), array_filter($this->getAttributes()));

$this->fireModelEvent(new Events\Created($this));
$this->dispatch('created');

$this->syncOriginal();

Expand All @@ -1193,11 +1193,11 @@ protected function performUpdate()
return;
}

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

$this->newQuery()->update($this->dn, $modifications);

$this->fireModelEvent(new Events\Updated($this));
$this->dispatch('updated');

$this->syncOriginal();
}
Expand Down Expand Up @@ -1233,11 +1233,15 @@ public static function create(array $attributes = [])
*/
public function createAttribute($attribute, $value)
{
$this->requireExistence();
$this->assertExists();

$this->dispatch(['saving', 'updating']);

$this->newQuery()->insertAttributes($this->dn, [$attribute => (array) $value]);

$this->addAttributeValue($attribute, $value);

$this->dispatch(['updated', 'saved']);
}

/**
Expand All @@ -1252,7 +1256,7 @@ public function createAttribute($attribute, $value)
*/
public function update(array $attributes = [])
{
$this->requireExistence();
$this->assertExists();

$this->save($attributes);
}
Expand All @@ -1270,11 +1274,15 @@ public function update(array $attributes = [])
*/
public function updateAttribute($attribute, $value)
{
$this->requireExistence();
$this->assertExists();

$this->dispatch(['saving', 'updating']);

$this->newQuery()->updateAttributes($this->dn, [$attribute => (array) $value]);

$this->addAttributeValue($attribute, $value);

$this->dispatch(['updated', 'saved']);
}

/**
Expand Down Expand Up @@ -1323,9 +1331,9 @@ public static function destroy($dns, $recursive = false)
*/
public function delete($recursive = false)
{
$this->requireExistence();
$this->assertExists();

$this->fireModelEvent(new Events\Deleting($this));
$this->dispatch('deleting');

if ($recursive) {
$this->deleteLeafNodes();
Expand All @@ -1338,7 +1346,7 @@ public function delete($recursive = false)
// developers can hook in and run further operations.
$this->exists = false;

$this->fireModelEvent(new Events\Deleted($this));
$this->dispatch('deleted');
}

/**
Expand Down Expand Up @@ -1378,10 +1386,12 @@ protected function deleteLeafNodes()
*/
public function deleteAttribute($attributes)
{
$this->requireExistence();
$this->assertExists();

$attributes = $this->makeDeletableAttributes($attributes);

$this->dispatch(['saving', 'updating']);

$this->newQuery()->deleteAttributes($this->dn, $attributes);

foreach ($attributes as $attribute => $value) {
Expand All @@ -1401,6 +1411,8 @@ public function deleteAttribute($attributes)
}
}

$this->dispatch(['updated', 'saved']);

$this->syncOriginal();
}

Expand Down Expand Up @@ -1440,7 +1452,7 @@ protected function makeDeletableAttributes($attributes)
*/
public function move($newParentDn, $deleteOldRdn = true)
{
$this->requireExistence();
$this->assertExists();

if (! $rdn = $this->getRdn()) {
throw new UnexpectedValueException('Current model does not contain an RDN to move.');
Expand All @@ -1463,7 +1475,7 @@ public function move($newParentDn, $deleteOldRdn = true)
*/
public function rename($rdn, $newParentDn = null, $deleteOldRdn = true)
{
$this->requireExistence();
$this->assertExists();

if ($newParentDn instanceof self) {
$newParentDn = $newParentDn->getDn();
Expand All @@ -1490,7 +1502,7 @@ public function rename($rdn, $newParentDn = null, $deleteOldRdn = true)
$rdn = $this->getUpdateableRdn($rdn);
}

$this->fireModelEvent(new Renaming($this, $rdn, $newParentDn));
$this->dispatch('renaming', [$rdn, $newParentDn]);

$this->newQuery()->rename($this->dn, $rdn, $newParentDn, $deleteOldRdn);

Expand All @@ -1510,7 +1522,7 @@ public function rename($rdn, $newParentDn = null, $deleteOldRdn = true)
= $this->original[$modelNameAttribute]
= [reset($map[$modelNameAttribute])];

$this->fireModelEvent(new Renamed($this));
$this->dispatch('renamed');

$this->wasRecentlyRenamed = true;
}
Expand Down Expand Up @@ -1618,11 +1630,25 @@ protected function buildModificationsFromDirty()
/**
* Throw an exception if the model does not exist.
*
* @deprecated
*
* @return void
*
* @throws ModelDoesNotExistException
*/
protected function requireExistence()
{
return $this->assertExists();
}

/**
* Throw an exception if the model does not exist.
*
* @return void
*
* @throws ModelDoesNotExistException
*/
protected function assertExists()
{
if (! $this->exists || is_null($this->dn)) {
throw ModelDoesNotExistException::forModel($this);
Expand Down

0 comments on commit 6d8d242

Please sign in to comment.