| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dtc\GridBundle\Grid\Column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Dtc\GridBundle\Grid\Source\GridSourceInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Routing\RouterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class ActionGridColumn extends AbstractGridColumn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     protected $actions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     protected $idField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** @var RouterInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $gridSourceId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function __construct($field, array $actions, $idField = 'id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->actions = $actions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->idField = $idField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->label = 'Actions'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->field = $field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function format($object, GridSourceInterface $gridsource) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $method = 'get'.ucfirst($this->idField); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $id = $object->$method(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $idHtml = htmlspecialchars($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $content = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         foreach ($this->actions as $action => $options) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $label = $options['label']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             if ($content) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 $content .= ' '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             switch ($options['action']) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 37 |  | View Code Duplication |                 case 'show': | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                     $route = $this->router->generate('dtc_grid_show', ['identifier' => $id, 'id' => $this->gridSourceId]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                     $route = htmlspecialchars($route); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                     $content .= "<button class=\"btn btn-primary grid-show\" data-route=\"$route\" data-id=\"$idHtml\""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     $content .= "onclick=\"dtc_grid_show(this)\">$label</button>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 43 |  | View Code Duplication |                 case 'delete': | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                     $route = $this->router->generate('dtc_grid_delete', ['identifier' => $id, 'id' => $this->gridSourceId]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     $route = htmlspecialchars($route); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                     $content .= "<button class=\"btn btn-primary grid-delete\" data-route=\"$route\" data-id=\"$idHtml\""; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     $content .= "onclick=\"dtc_grid_delete(this)\"><i class=\"fa fa-circle-o-notch fa-spin hidden\"></i> $label</button>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return $content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function setRouter(RouterInterface $router) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->router = $router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     public function setGridSourceId($gridSourceId) | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         $this->gridSourceId = $gridSourceId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.