Example #1
0
 /**
  * Define and execute the controller::action derived from the available
  * url segments
  */
 public function run()
 {
     // Get parsed segments
     $segments = $this->request->getSegmentKeys();
     // Extract controller when available
     $controllerName = 'Heartbeat';
     if (\array_key_exists(0, $segments)) {
         $controllerName = \ucfirst($segments[0]);
     }
     // Extract action when available
     $action = $this->request->getMethod();
     if (\array_key_exists(1, $segments)) {
         $action .= \ucfirst($segments[1]) . 'Action';
     } else {
         $action .= 'IndexAction';
     }
     try {
         // Assert controller class exists
         $controllerClass = '\\ITC\\Presentation\\Controllers\\' . $controllerName;
         if (!\class_exists($controllerClass)) {
             // Invalid controller
             throw new \InvalidArgumentException('Invalid controller: ' . $controllerClass);
         }
         // Init controller class
         $controller = new $controllerClass($this->request, $this->response);
         // Assert action is available
         if (!\method_exists($controller, $action)) {
             // Invalid action
             throw new \InvalidArgumentException('Invalid action:' . $controllerClass . '::' . $action);
         }
         // Call action on controller
         $controller->{$action}();
         // Send response
         return $this->response->send();
     } catch (\InvalidArgumentException $e) {
         // Invalid controller or action selected
         return $this->response->sendNotFound();
     } catch (\Exception $e) {
         // Something out of the frontcontroller scope went wrong
         return $this->response->sendInternalServerError($e);
     }
 }
Example #2
0
 /**
  * Get the available url segment keys when no url has been set
  * 
  * @covers ::getSegmentKeys
  * 
  * @test
  */
 public function getSegmentKeysWithoutUrl()
 {
     // Parse request
     $request = new Request('get', array('url' => '/'));
     // Assert season and winner key/value pairs are set
     $this->assertCount(0, $request->getSegmentKeys());
 }