Completed
Branch erdiko2 (81b309)
by John
02:13
created

Web   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 6 1
A determineAction() 0 13 3
A render() 0 7 2
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
     * dertermin 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
    /**
51
     * render 
52
     * render page
53
     */ 
54
    public function render($response, $view, $application) 
55
    {
56
        if(empty($view))
57
            $view = $application['theme']['defaults']['view'];
58
59
        return $this->container->theme->render($response, $view, $application);
60
    }
61
}
62