Normalise the setting was created event

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