Normalise the setting was created event

parent 875b402d
......@@ -2,30 +2,17 @@
namespace Modules\Setting\Events;
use Modules\Setting\Entities\Setting;
class SettingWasCreated
{
/**
* @var bool
*/
public $isTranslatable;
/**
* @var string Setting name
* @var Setting
*/
public $name;
/**
* @var string|array
*/
public $values;
public $setting;
/**
* @param $name
* @param $isTranslatable
* @param $values
*/
public function __construct($name, $isTranslatable, $values)
public function __construct(Setting $setting)
{
$this->isTranslatable = $isTranslatable;
$this->name = $name;
$this->values = $values;
$this->setting = $setting;
}
}
......@@ -4,6 +4,7 @@ namespace Modules\Setting\Repositories\Eloquent;
use Illuminate\Support\Facades\Config;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Setting\Entities\Setting;
use Modules\Setting\Events\SettingWasCreated;
use Modules\Setting\Events\SettingWasUpdated;
use Modules\Setting\Repositories\SettingRepository;
......@@ -77,6 +78,7 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
* Create a setting with the given name
* @param string $settingName
* @param $settingValues
* @return Setting
*/
private function createForName($settingName, $settingValues)
{
......@@ -86,14 +88,16 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
if ($this->isTranslatableSetting($settingName)) {
$setting->isTranslatable = true;
$this->setTranslatedAttributes($settingValues, $setting);
event(new SettingWasCreated($settingName, true, $settingValues));
} else {
$setting->isTranslatable = false;
$setting->plainValue = $this->getSettingPlainValue($settingValues);
event(new SettingWasCreated($settingName, false, $settingValues));
}
return $setting->save();
$setting->save();
event(new SettingWasCreated($setting));
return $setting;
}
/**
......
......@@ -2,6 +2,9 @@
namespace Modules\Setting\Tests;
use Illuminate\Support\Facades\Event;
use Modules\Setting\Events\SettingWasCreated;
class EloquentSettingRepositoryTest extends BaseSettingTest
{
public function setUp()
......@@ -111,4 +114,23 @@ class EloquentSettingRepositoryTest extends BaseSettingTest
$this->assertEquals('core::locales', $setting->name);
$this->assertEquals('["su","bi","bs"]', $setting->plainValue);
}
/** @test */
public function it_triggers_event_when_setting_was_created()
{
Event::fake();
$data = [
'core::template' => 'asgard',
'core::site-name' => [
'en' => 'AsgardCMS_en',
'fr' => 'AsgardCMS_fr',
],
];
$this->settingRepository->createOrUpdate($data);
Event::assertDispatched(SettingWasCreated::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