| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace QualityCode\ApiFeaturesBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use FOS\RestBundle\View\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Workflow\Workflow; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Add workflow features on a controller. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author flavien-metivier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | trait ControllerWorkflowFeaturesTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * Gets a container service by its id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param string $id The service id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @return object The service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     abstract protected function get($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Creates and returns a Form instance from the type of the form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param string $type    The fully qualified class name of the form type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param mixed  $data    The initial data for the form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param array  $options Options for the form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @return Form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     abstract protected function createForm($type, $data = null, array $options = []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param string  $repositoryName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param string  $workflowName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     protected function workflowStatutAvailable(Request $request, $repositoryName, $workflowName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $element = $this->get('doctrine.orm.entity_manager') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 ->getRepository($repositoryName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 ->find($request->get('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         if (empty($element)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             return View::create(['message' => 'Element not found'], Response::HTTP_NOT_FOUND); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $workflow = $this->get('workflow.'.$workflowName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $workflow->getEnabledTransitions($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param string  $repositoryName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param string  $workflowName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     protected function changeWorkflowStatut(Request $request, $repositoryName, $workflowName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $element = $this->get('doctrine.orm.entity_manager') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 ->getRepository($repositoryName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 ->find($request->get('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $newState = $request->get('state_name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $workflow = $this->get('workflow.'.$workflowName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $can = $this->checkIfElementCanChangeStatut($newState, $workflow, $element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if ($can !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             return $can; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $workflow->apply($element, $newState); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $em = $this->get('doctrine.orm.entity_manager'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $em->persist($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $em->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return $element; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param string   $newState | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param Workflow $workflow | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param mixed    $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return bool|\FOS\RestBundle\View\View | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |     protected function checkIfElementCanChangeStatut(string $newState, Workflow $workflow, $element) | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         if (empty($element)) { | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |             return View::create(['message' => 'Element not found'], Response::HTTP_NOT_FOUND); | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         if (!$workflow->can($element, $newState)) { | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |             return View::create(['message' => 'Workflow state not available'], Response::HTTP_FORBIDDEN); | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         return true; | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |