Skip to content

Commit

Permalink
Dispatch Events (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
rawilk authored Sep 28, 2023
1 parent 49a222c commit b325786
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/Events/SettingWasDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Rawilk\Settings\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Rawilk\Settings\Support\Context;

final class SettingWasDeleted
{
use Dispatchable;
use SerializesModels;

public function __construct(
public string $key,
public string $storageKey,
public string $cacheKey,
public mixed $teamId,
public bool|Context|null $context,
) {
}
}
25 changes: 25 additions & 0 deletions src/Events/SettingWasStored.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Rawilk\Settings\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Rawilk\Settings\Support\Context;

final class SettingWasStored
{
use Dispatchable;
use SerializesModels;

public function __construct(
public string $key,
public string $storageKey,
public string $cacheKey,
public mixed $value,
public mixed $teamId,
public bool|Context|null $context,
) {
}
}
23 changes: 23 additions & 0 deletions src/Events/SettingsFlushed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Rawilk\Settings\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use Rawilk\Settings\Support\Context;

final class SettingsFlushed
{
use Dispatchable;
use SerializesModels;

public function __construct(
public bool|Collection|string $keys,
public mixed $teamId,
public bool|Context|null $context,
) {
}
}
28 changes: 27 additions & 1 deletion src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Rawilk\Settings\Contracts\Driver;
use Rawilk\Settings\Contracts\KeyGenerator;
use Rawilk\Settings\Contracts\ValueSerializer;
use Rawilk\Settings\Events\SettingsFlushed;
use Rawilk\Settings\Events\SettingWasDeleted;
use Rawilk\Settings\Events\SettingWasStored;
use Rawilk\Settings\Exceptions\InvalidBulkValueResult;
use Rawilk\Settings\Exceptions\InvalidKeyGenerator;
use Rawilk\Settings\Support\Context;
Expand Down Expand Up @@ -116,6 +119,14 @@ public function forget($key)
teamId: $this->teams ? $this->teamId : false,
);

SettingWasDeleted::dispatch(
$key,
$generatedKey,
$this->getCacheKey($generatedKey),
$this->teams ? $this->teamId : false,
$this->context,
);

if ($this->temporarilyDisableCache || $this->cacheIsEnabled()) {
$this->cache->forget($this->getCacheKey($generatedKey));
}
Expand Down Expand Up @@ -218,7 +229,7 @@ public function has($key): bool
return $has;
}

public function set(string $key, $value = null)
public function set(string $key, $value = null): mixed
{
$key = $this->normalizeKey($key);

Expand All @@ -239,6 +250,15 @@ public function set(string $key, $value = null)
teamId: $this->teams ? $this->teamId : false,
);

SettingWasStored::dispatch(
$key,
$generatedKey,
$this->getCacheKey($generatedKey),
$value,
$this->teams ? $this->teamId : false,
$this->context,
);

if ($this->temporarilyDisableCache || $this->cacheIsEnabled()) {
$this->cache->forget($this->getCacheKey($generatedKey));
}
Expand Down Expand Up @@ -272,6 +292,12 @@ public function flush($keys = null): mixed
keys: $keys,
);

SettingsFlushed::dispatch(
$keys,
$this->teams ? $this->teamId : false,
$this->context,
);

// Flush the cache for all deleted keys.
// Note: Only works when a subset of keys is specified.
if ($keys instanceof Collection && ($this->temporarilyDisableCache || $this->cacheIsEnabled())) {
Expand Down
13 changes: 13 additions & 0 deletions tests/ArchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
declare(strict_types=1);

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Facade;
use Rawilk\Settings\Contracts\ContextSerializer;
use Rawilk\Settings\Contracts\Driver;
Expand Down Expand Up @@ -54,3 +56,14 @@
->toImplement(KeyGenerator::class)
->toExtendNothing()
->toHaveSuffix('Generator');

test('events are configured correctly')
->expect('Rawilk\Settings\Events')
->toBeClasses()
->classes()
->toExtendNothing()
->toBeFinal()
->toUse([
Dispatchable::class,
SerializesModels::class,
]);
54 changes: 54 additions & 0 deletions tests/Feature/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
declare(strict_types=1);

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Rawilk\Settings\Contracts\Setting;
use Rawilk\Settings\Drivers\EloquentDriver;
use Rawilk\Settings\Events\SettingsFlushed;
use Rawilk\Settings\Events\SettingWasDeleted;
use Rawilk\Settings\Events\SettingWasStored;
use Rawilk\Settings\Exceptions\InvalidKeyGenerator;
use Rawilk\Settings\Facades\Settings as SettingsFacade;
use Rawilk\Settings\Support\Context;
Expand Down Expand Up @@ -431,6 +435,56 @@
$this->assertDatabaseCount('settings', 1);
});

it('dispatches an event when settings are flushed', function () {
$settings = settings();
(fn () => $this->keyGenerator = (new ReadableKeyGenerator)->setContextSerializer(new DotNotationContextSerializer))->call($settings);

Event::fake();

$settings->set('one', 'value 1');
$settings->set('two', 'value 2');

$settings->flush();

Event::assertDispatched(SettingsFlushed::class);
});

it('dispatches an event when a setting is deleted', function () {
Event::fake();

SettingsFacade::set('foo', 'bar');
SettingsFacade::forget('foo');

Event::assertDispatched(function (SettingWasDeleted $event) {
return $event->key === 'foo'
&& $event->teamId === false
&& is_null($event->context);
});
});

it('dispatches an event when a setting is saved', function () {
Event::fake();

SettingsFacade::set('foo', 'bar');

Event::assertDispatched(function (SettingWasStored $event) {
return $event->key === 'foo'
&& $event->value === 'bar';
});
});

it('does not dispatch the stored event if the setting value has not changed', function () {
Event::fake();

// This only works when caching is enabled.
SettingsFacade::enableCache();

SettingsFacade::set('foo', 'bar');
SettingsFacade::set('foo', 'bar');

Event::assertDispatchedTimes(SettingWasStored::class, 1);
});

// Helpers...

function assertQueryCount(int $expected): void
Expand Down

0 comments on commit b325786

Please sign in to comment.