php 重构表单处理程序以更新事件营销
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 重构表单处理程序以更新事件营销相关的知识,希望对你有一定的参考价值。
<?
//.. Current Code
if (in_array('ROLE_ADMIN', $user->getRoles())) {
if ($submit == 'Approved') {
$this->eventMarketingHandler->approveEvent($eventMarketing);
$eventMarketing = $event->getParameter('eventMarketing');
$message = $this->translator->trans('event_marketing.approve.success', [], 'event-marketing');
} else if(($submit == 'Saved' && $eventMarketing->isApproved()) || ($submit == 'Saved' && !$eventMarketing->isPublished())) {
$this->eventMarketingHandler->mergeDraftToOriginal($eventMarketing);
$eventMarketing = $event->getParameter('eventMarketing');
$message = $this->translator->trans('event_marketing.update.success', [], 'event-marketing');
}
} else {
if ($submit == 'Saved' && !$eventMarketing->isPublished()) {
$this->eventMarketingHandler->mergeDraftToOriginal($eventMarketing);
$eventMarketing = $event->getParameter('eventMarketing');
$message = $this->translator->trans('event_marketing.update.success', [], 'event-marketing');
}
}
// Refactor
try {
if ($isApproveRequest) {
$this->eventMarketingHandler->approveEvent($event);
} else {
$this->eventMarketingHandler->mergeDraftToOrignal($event);
}
} catch (AuthenticationException $e) {
//.. update failed
}
//.. update success
class EventMarketingHandler
{
private $currentUser;
/**
* @return UserInterface
*/
protected function getCurrentUser()
{
return $this->currentUser = $this->tokenStorage->getToken()->getUser();
}
/**
* @throws AuthenticationException
*/
public function approveEvent(EventMarketing $event)
{
if (!$this->currentUser->hasRole('ROLE_ADMIN')) {
throw new AuthenticationException('You have no right to perform this action');
}
$this->eventMarketingManager->approve($event);
}
/**
* @throws AuthenticationException
*/
public function mergeDraftToOriginal(EventMarketing $event)
{
if ($event->isApproved() && !$this->currentUser->hasRole('ROLE_ADMIN')) {
throw new AuthenticationException('You have no right to perform this action');
}
$this->eventMarketingManager->mergeDraft($event);
}
}
以上是关于php 重构表单处理程序以更新事件营销的主要内容,如果未能解决你的问题,请参考以下文章
更新 Access 表单中的事件处理
获取 Qt 小部件以更新使用 Qt Designer 制作的 Qt 表单中的鼠标事件
对于简单的增量表单更新来说,事件溯源是不是过大?
如何将前端 JS 重构为 Angular 2 以与 PHP MVC 后端完美配合? [关闭]
使用 AJAX 和 PHP 更新表单字段
谷歌网络应用程序 - 使用 html 表单提交更新事件参数?