Skip to content

Commit

Permalink
вынос логики создания и редактирования виджетов в Forms
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed May 27, 2016
1 parent 35057a4 commit 407914a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 31 deletions.
18 changes: 9 additions & 9 deletions src/Http/Controllers/WidgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace KodiCMS\Widgets\Http\Controllers;

use KodiCMS\Widgets\Contracts\WidgetManager;
use KodiCMS\Widgets\Http\Forms\CreateWidgetForm;
use KodiCMS\Widgets\Http\Forms\UpdateWidgetForm;
use Meta;
use WYSIWYG;
use Illuminate\View\View;
Expand Down Expand Up @@ -85,14 +87,13 @@ public function getCreate(WidgetManager $widgetManager, $type = 'html')
}

/**
* @param WidgetRepository $repository
* @param CreateWidgetForm $widgetForm
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postCreate(WidgetRepository $repository)
public function postCreate(CreateWidgetForm $widgetForm)
{
$repository->validateOnCreate($this->request);
$widget = $repository->create($this->request->all());
$widget = $widgetForm->save();

return $this->smartRedirect([$widget])
->with('success', trans($this->wrapNamespace('core.messages.created'), [
Expand All @@ -118,15 +119,14 @@ public function getEdit(WidgetRepository $repository, $id)
}

/**
* @param WidgetRepository $repository
* @param int $id
* @param UpdateWidgetForm $widgetForm
* @param int $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postEdit(WidgetRepository $repository, $id)
public function postEdit(UpdateWidgetForm $widgetForm, $id)
{
$repository->validateOnUpdate($id, $this->request);
$widget = $repository->update($id, $this->request->all());
$widget = $widgetForm->save();

return $this->smartRedirect([$widget])
->with('success', trans($this->wrapNamespace('core.messages.updated'), [
Expand Down
59 changes: 59 additions & 0 deletions src/Http/Forms/CreateWidgetForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace KodiCMS\Widgets\Http\Forms;

use Illuminate\Http\Request;
use KodiCMS\Widgets\Model\Widget;
use KodiCMS\Widgets\Repository\WidgetRepository;
use KodiComponents\Support\Http\Form;

class CreateWidgetForm extends Form
{

/**
* @var WidgetRepository
*/
protected $repository;

/**
* Form constructor.
*
* @param Request|null $request
* @param WidgetRepository $repository
*/
public function __construct(Request $request, WidgetRepository $repository)
{
parent::__construct($request);

$this->repository = $repository;
}

/**
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:255',
'type' => 'required',
];
}

/**
* @return array
*/
public function labels()
{
return trans('widgets::core.field');
}

/**
* Persist the form.
*
* @return Widget
*/
public function persist()
{
return $this->repository->create($this->request->all());
}
}
32 changes: 32 additions & 0 deletions src/Http/Forms/UpdateWidgetForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace KodiCMS\Widgets\Http\Forms;

use KodiCMS\Widgets\Model\Widget;

class UpdateWidgetForm extends CreateWidgetForm
{

/**
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:255',
];
}

/**
* Persist the form.
*
* @return Widget
*/
public function persist()
{
return $this->repository->update(
$this->request->route('id'),
$this->request->all()
);
}
}
22 changes: 0 additions & 22 deletions src/Repository/WidgetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,6 @@ public function __construct(Widget $model)
parent::__construct($model);
}

/**
* @param Request $request
*/
public function validateOnCreate(Request $request)
{
$this->validate($request, [
'name' => 'required|max:255',
'type' => 'required',
]);
}

/**
* @param int $id
* @param Request $request
*/
public function validateOnUpdate($id, Request $request)
{
$this->validate($request, [
'name' => 'required|max:255',
]);
}

/**
* @param int $id
* @param array $data
Expand Down

0 comments on commit 407914a

Please sign in to comment.