Selaa lähdekoodia

Refactored duplicate code into protected methods within the CRUDController

Thanos Polymeneas 13 vuotta sitten
vanhempi
commit
80f5ebfc69
1 muutettua tiedostoa jossa 47 lisäystä ja 24 poistoa
  1. 47 24
      Controller/CRUDController.php

+ 47 - 24
Controller/CRUDController.php

@@ -256,21 +256,11 @@ class CRUDController extends Controller
 
         if ($this->get('request')->getMethod() == 'POST') {
             $form->bindRequest($this->get('request'));
-
-            // preview mode was requested
-            $isPreviewRequested = $this->get('request')->get('btn_preview') !== null;
-            // preview-approved was clicked
-            $isPreviewApproved = $this->get('request')->get('btn_preview_approve') !== null;
-            // preview-decline was clicked
-            $isPreviewDeclined = $this->get('request')->get('btn_preview_decline') !== null;
-            
-            // call it preview mode if one of the preview buttons was clicked
-            $inPreviewMode = ($isPreviewRequested || $isPreviewApproved || $isPreviewDeclined);
             
             $isFormValid = $form->isValid(); 
             
              // persist if the form was valid and if in preview mode the preview was approved
-            if ($isFormValid && (!$inPreviewMode || $isPreviewApproved)) {
+            if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
                 $this->admin->update($object);
                 $this->get('session')->setFlash('sonata_flash_success', 'flash_edit_success');
 
@@ -288,7 +278,7 @@ class CRUDController extends Controller
             // show an error message if the form failed validation
             if (!$isFormValid) {
                 $this->get('session')->setFlash('sonata_flash_error', 'flash_edit_error');
-            } elseif ($isPreviewRequested) {
+            } elseif ($this->isPreviewRequested()) {
                 // enable the preview template if the form was valid and preview was requested
                 $this->admin->enablePreviewTemplate();
             }
@@ -446,20 +436,10 @@ class CRUDController extends Controller
         if ($this->get('request')->getMethod() == 'POST') {
             $form->bindRequest($this->get('request'));
             
-            // preview mode was requested
-            $isPreviewRequested = $this->get('request')->get('btn_preview') !== null;
-            // preview-approved was clicked
-            $isPreviewApproved = $this->get('request')->get('btn_preview_approve') !== null;
-            // preview-decline was clicked
-            $isPreviewDeclined = $this->get('request')->get('btn_preview_decline') !== null;
-            
-            // call it preview mode if one of the preview buttons was clicked
-            $inPreviewMode = ($isPreviewRequested || $isPreviewApproved || $isPreviewDeclined);
-            
             $isFormValid = $form->isValid(); 
             
             // persist if the form was valid and if in preview mode the preview was approved
-            if ($isFormValid && (!$inPreviewMode || $isPreviewApproved)) {
+            if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
                 $this->admin->create($object);
 
                 if ($this->isXmlHttpRequest()) {
@@ -477,7 +457,7 @@ class CRUDController extends Controller
             // show an error message if the form failed validation
             if (!$isFormValid) {
                 $this->get('session')->setFlash('sonata_flash_error', 'flash_create_error');
-            } elseif ($isPreviewRequested) {
+            } elseif ($this->isPreviewRequested()) {
                 // enable the preview template if the form was valid and preview was requested
                 $this->admin->enablePreviewTemplate();
             }
@@ -494,6 +474,49 @@ class CRUDController extends Controller
             'object' => $object,
         ));
     }
+    
+    /**
+     * Returns true if the preview is requested to be shown
+     * 
+     * @return boolean
+     */
+    protected function isPreviewRequested()
+    {
+        return ($this->get('request')->get('btn_preview') !== null);
+    }
+
+    /**
+     * Returns true if the preview has been approved
+     * 
+     * @return boolean
+     */
+    protected function isPreviewApproved()
+    {
+        return ($this->get('request')->get('btn_preview_approve') !== null);
+    }
+    
+    /**
+     * Returns true if the request is in the preview workflow
+     * 
+     * That means either a preview is requested or the preview has already been shown
+     * and it got approved/declined.
+     * 
+     * @return boolean
+     */
+    protected function isInPreviewMode()
+    {
+        return ($this->isPreviewRequested() || $this->isPreviewApproved() || $this->isPreviewDeclined());
+    }
+
+    /**
+     * Returns true if the preview has been declined
+     * 
+     * @return boolean
+     */
+    protected function isPreviewDeclined()
+    {
+        return ($this->get('request')->get('btn_preview_decline') !== null);
+    }
 
     /**
      * return the Response object associated to the view action