Skip to content

Commit

Permalink
add form handling result dto
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjon committed Nov 26, 2018
1 parent 97778ce commit f63f343
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 12 deletions.
16 changes: 8 additions & 8 deletions src/Core/Form/IdentifiableObject/Handler/FormHandler.php
Expand Up @@ -95,12 +95,12 @@ public function handleFor($id, FormInterface $form)
* @param FormInterface $form
* @param int|null $id
*
* @return int
* @return FormHandlerResultInterface
*/
private function handleForm(FormInterface $form, $id = null)
{
if (!$form->isSubmitted()) {
return 0;
return FormHandlerResult::createNotSubmitted();
}

if ($this->isDemoModeEnabled) {
Expand All @@ -110,11 +110,11 @@ private function handleForm(FormInterface $form, $id = null)
)
);

return 0;
return FormHandlerResult::createSubmittedButNotValid();
}

if (!$form->isValid()) {
return 0;
return FormHandlerResult::createSubmittedButNotValid();
}

if (null !== $id) {
Expand All @@ -128,7 +128,7 @@ private function handleForm(FormInterface $form, $id = null)
* @param FormInterface $form
* @param int $id
*
* @return int
* @return FormHandlerResultInterface
*/
private function handleFormUpdate(FormInterface $form, $id)
{
Expand All @@ -145,13 +145,13 @@ private function handleFormUpdate(FormInterface $form, $id)
'id' => $id,
]);

return $id;
return FormHandlerResult::createWithId($id);
}

/**
* @param FormInterface $form
*
* @return int
* @return FormHandlerResult
*/
private function handleFormCreate(FormInterface $form)
{
Expand All @@ -167,6 +167,6 @@ private function handleFormCreate(FormInterface $form)
'id' => $id,
]);

return $id;
return FormHandlerResult::createWithId($id);
}
}
Expand Up @@ -38,7 +38,7 @@ interface FormHandlerInterface
*
* @param FormInterface $form
*
* @return int Identifiable object ID
* @return FormHandlerResultInterface
*/
public function handle(FormInterface $form);

Expand All @@ -48,7 +48,7 @@ public function handle(FormInterface $form);
* @param int $id
* @param FormInterface $form
*
* @return int Identifiable object ID
* @return FormHandlerResultInterface
*/
public function handleFor($id, FormInterface $form);
}
128 changes: 128 additions & 0 deletions src/Core/Form/IdentifiableObject/Handler/FormHandlerResult.php
@@ -0,0 +1,128 @@
<?php
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Core\Form\IdentifiableObject\Handler;

/**
* Stores results for handling forms.
*/
class FormHandlerResult implements FormHandlerResultInterface
{
/**
* @var bool
*/
private $isValid;

/**
* @var bool
*/
private $isSubmitted;

/**
* @var int|null
*/
private $identifiableObjectId;

/**
* @param int|null $identifiableObjectId ID of identifiable object or null if it does not exist.
* @param bool $isSubmitted
* @param bool $isValid
*/
private function __construct($identifiableObjectId, $isSubmitted, $isValid)
{
$this->identifiableObjectId = $identifiableObjectId;
$this->isSubmitted = $isSubmitted;
$this->isValid = $isValid;
}

/**
* Creates successful form handler result with identifiable object id.
*
* @param int $identifiableObjectId
*
* @return FormHandlerResult
*/
public static function createWithId($identifiableObjectId)
{
return new self(
$identifiableObjectId,
true,
true
);
}

/**
* Creates form handler result when form which was provided form handling was not submitted
*
* @return FormHandlerResult
*/
public static function createNotSubmitted()
{
return new self(
null,
false,
false
);
}

/**
* Creates result for submitted but not valid form
*
* @return FormHandlerResult
*/
public static function createSubmittedButNotValid()
{
return new self(
null,
true,
false
);
}

/**
* @return bool
*/
public function isValid()
{
return $this->isValid;
}

/**
* @return bool
*/
public function isSubmitted()
{
return $this->isSubmitted;
}

/**
* @return int|null
*/
public function getIdentifiableObjectId()
{
return $this->identifiableObjectId;
}
}
@@ -0,0 +1,54 @@
<?php
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Core\Form\IdentifiableObject\Handler;

/**
* Defines interface for form handler result DTO
*/
interface FormHandlerResultInterface
{
/**
* Check if form is valid and does not contains any errors
*
* @return bool
*/
public function isValid();

/**
* Check if form was actually submitted
*
* @return bool
*/
public function isSubmitted();

/**
* Get identifiable object id
*
* @return int|null ID of identifiable object or null if it does not exist
*/
public function getIdentifiableObjectId();
}
Expand Up @@ -178,7 +178,9 @@ public function createAction(Request $request)
$sqlRequestForm = $this->getSqlRequestFormBuilder()->getForm($data);
$sqlRequestForm->handleRequest($request);

if ($this->getSqlRequestFormHandler()->handle($sqlRequestForm)) {
$result = $this->getSqlRequestFormHandler()->handle($sqlRequestForm);

if (null !== $result->getIdentifiableObjectId()) {
$this->addFlash('success', $this->trans('Successful creation.', 'Admin.Notifications.Success'));

return $this->redirectToRoute('admin_sql_requests_index');
Expand Down Expand Up @@ -215,7 +217,9 @@ public function editAction($sqlRequestId, Request $request)
$sqlRequestForm = $this->getSqlRequestFormBuilder()->getFormFor($sqlRequestId);
$sqlRequestForm->handleRequest($request);

if ($this->getSqlRequestFormHandler()->handleFor($sqlRequestId, $sqlRequestForm)) {
$result = $this->getSqlRequestFormHandler()->handleFor($sqlRequestId, $sqlRequestForm);

if (null !== $result->getIdentifiableObjectId()) {
$this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success'));

return $this->redirectToRoute('admin_sql_requests_index');
Expand Down

0 comments on commit f63f343

Please sign in to comment.