| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Charcoal\App\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | // Dependencies from `PHP` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use \InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | // PSR-3 logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use \Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use \Psr\Log\LoggerAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | // PSR-7 (http messaging) dependencies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use \Psr\Http\Message\RequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use \Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | // From `charcoal-config` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use \Charcoal\Config\ConfigurableInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use \Charcoal\Config\ConfigurableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | // Intra-module (`charcoal-app`) dependencies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use \Charcoal\App\Template\TemplateFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | // Local namespace dependencies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use \Charcoal\App\Route\RouteInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use \Charcoal\App\Route\TemplateRouteConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class TemplateRoute implements | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     ConfigurableInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     LoggerAwareInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     RouteInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     use ConfigurableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     * @var \Slim\App $app | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private $app; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @var LoggerInterface $logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     * Dependencies: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     * - `config` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     * - `app` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     * @param array $data Dependencies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 52 |  | View Code Duplication |     public function __construct(array $data) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->set_config($data['config']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->app = $data['app']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if (!($this->app instanceof \Slim\App)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             throw new InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 'App requires a Slim App object in its dependency container.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         // Reuse app logger, if it's not directly set in data dependencies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $logger = isset($data['logger']) ? $data['logger'] : $this->app->logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->set_logger($logger); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     * > LoggerAwareInterface > setLogger() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     * Fulfills the PSR-1 style LoggerAwareInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     * @param LoggerInterface $logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     * @return AbstractEngine Chainable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function setLogger(LoggerInterface $logger) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return $this->set_logger($logger); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     * @param LoggerInterface $logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     * @return AbstractEngine Chainable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function set_logger(LoggerInterface $logger = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->logger = $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     * @erturn LoggerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function logger() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return $this->logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     * ConfigurableTrait > create_config() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function create_config($data = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return new TemplateRouteConfig($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  |     */ | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |     public function __invoke(RequestInterface $request, ResponseInterface $response) | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |         unset($request); | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |         $config = $this->config(); | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |         $template_ident = $config['template'] ?: $config['ident']; | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |         $template = TemplateFactory::instance()->create($template_ident, [ | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |             'app' => $this->app | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |         ]); | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |         $response->write($template->render($template_ident)); | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |         return $response; | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |  | 
            
                        
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.