FEATURE: move bussiness logic out of models
This commit is contained in:
@@ -22,45 +22,4 @@ class Widget extends Model
|
||||
'viewed',
|
||||
'deletable'
|
||||
];
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* Create new widget of given type
|
||||
*
|
||||
* @param WidgetType $widgetType
|
||||
* @param $data
|
||||
*
|
||||
* @return Widget
|
||||
*/
|
||||
public function createNew(WidgetType $widgetType, $data)
|
||||
{
|
||||
// convert array or whatever into object
|
||||
if (!is_object($data)) {
|
||||
$data = (object)$data;
|
||||
}
|
||||
|
||||
// create new widget
|
||||
$widget = new Widget();
|
||||
$widget->widget_type_handler = $widgetType->handler;
|
||||
$widget->language = LaravelLocalization::getCurrentLocale();
|
||||
$widget->user_id = \Auth::user()->id;
|
||||
$widget->name = $data->name;
|
||||
$widget->container_class = $data->containerClass;
|
||||
$widget->container_id = empty($data->containerId) ? null : $data->containerId;
|
||||
|
||||
// save description if available
|
||||
if (isset($data->description)) {
|
||||
$widget->description = $data->description;
|
||||
}
|
||||
|
||||
$widget = call_user_func([$widgetType->handler, 'save'], $widget, $data);
|
||||
|
||||
// save into database
|
||||
$widget->save();
|
||||
|
||||
// fire event with newly created widget
|
||||
Event::fire(new \App\Events\CreateNewWidget($widget));
|
||||
|
||||
return $widget;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user