Completed
Pull Request — erdiko2 (#29)
by
unknown
01:47
created

Web::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
/**
3
 * Web Controller
4
 * Simple convention based web controller to auto route actions
5
 *
6
 * @package     erdiko/controllers
7
 * @copyright   2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com
8
 * @author      John Arroyo <[email protected]>
9
 */
10
namespace erdiko\controllers;
11
12
13
class Web extends \erdiko\Controller
14
{
15
    /**
16
     * Invoke 
17
     * @param Request $request
18
     * @param Response $response
19
     * @param array $args
20
     * @return string $action method name 
21
     */
22
    public function __invoke($request, $response, $args) 
23
    {
24
        $action = $this->determineAction($request, $args);
25
        // $this->container->logger->debug("Controller action: ".print_r($action, true));
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
        $this->$action($request, $response, $args);
27
    }
28
29
    /**
30
     * Determine action
31
     * 
32
     * @param Request $request
33
     * @param array $args
34
     * @return string $action method name 
35
     */
36
    protected function determineAction($request, $args) : string
37
    {
38
        // Request method
39
        $action = strtolower($request->getMethod());
40
        // Action
41
        $action .= empty($args['action']) ? "" : ucfirst($args['action']);
42
43
         // @todo trigger 404 instead of throw exception
44
        if(!method_exists($this, $action))
45
            throw new \Exception("action does not exist");
46
47
        return $action;
48
    }
49
}
50