Normalise the setting was updated event

parent ac6bc7cd
......@@ -2,30 +2,17 @@
namespace Modules\Setting\Events;
use Modules\Setting\Entities\Setting;
class SettingWasUpdated
{
/**
* @var string The setting name
*/
public $name;
/**
* @var string|array
*/
public $values;
/**
* @var string|array Containing the old values
*/
public $oldValues;
/**
* @var bool
* @var Setting
*/
public $isTranslatable;
public $setting;
public function __construct($name, $isTranslatable, $values, $oldValues = null)
public function __construct(Setting $setting)
{
$this->name = $name;
$this->isTranslatable = $isTranslatable;
$this->values = $values;
$this->oldValues = $oldValues;
$this->setting = $setting;
}
}
......@@ -111,14 +111,14 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
if ($this->isTranslatableSetting($name)) {
$this->setTranslatedAttributes($settingValues, $setting);
event(new SettingWasUpdated($name, true, $settingValues));
} else {
$oldValues = $setting->plainValue;
$setting->plainValue = $this->getSettingPlainValue($settingValues);
event(new SettingWasUpdated($name, false, $settingValues, $oldValues));
}
$setting->save();
event(new SettingWasUpdated($setting));
return $setting->save();
return $setting;
}
/**
......
......@@ -4,6 +4,7 @@ namespace Modules\Setting\Tests;
use Illuminate\Support\Facades\Event;
use Modules\Setting\Events\SettingWasCreated;
use Modules\Setting\Events\SettingWasUpdated;
class EloquentSettingRepositoryTest extends BaseSettingTest
{
......@@ -133,4 +134,24 @@ class EloquentSettingRepositoryTest extends BaseSettingTest
return $e->setting->name === 'core::template';
});
}
/** @test */
public function it_triggers_event_when_setting_was_update()
{
Event::fake();
$data = [
'core::template' => 'asgard',
'core::site-name' => [
'en' => 'AsgardCMS_en',
'fr' => 'AsgardCMS_fr',
],
];
$this->settingRepository->createOrUpdate($data);
$this->settingRepository->createOrUpdate(['core::template' => 'flatly']);
Event::assertDispatched(SettingWasUpdated::class, function ($e) {
return $e->setting->name === 'core::template';
});
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment