Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

RichController::action()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 40
Code Lines 22

Duplication

Lines 7
Ratio 17.5 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 7
loc 40
rs 5.3846
c 1
b 1
f 0
cc 8
eloc 22
nc 10
nop 1
1
<?php /** MicroRichController */
2
3
namespace Micro\Mvc\Controllers;
4
5
use Micro\Base\IContainer;
6
7
/**
8
 * Class RichController
9
 *
10
 * @author Oleg Lunegov <[email protected]>
11
 * @link https://github.com/lugnsk/micro
12
 * @copyright Copyright &copy; 2013 Oleg Lunegov
13
 * @license /LICENSE
14
 * @package Micro
15
 * @subpackage Mvc\Controllers
16
 * @version 1.0
17
 * @since 1.0
18
 * @abstract
19
 */
20
abstract class RichController extends Controller
21
{
22
    /** @var string $format Format for response */
23
    public $format = 'application/json';
24
    /** @var string $methodType */
25
    protected $methodType = 'get';
26
27
28
    /**
29
     * Construct RICH controller
30
     *
31
     * @access public
32
     *
33
     * @param IContainer $container
34
     * @param string $modules
35
     *
36
     * @result void
37
     */
38
    public function __construct(IContainer $container, $modules = '')
39
    {
40
        parent::__construct($container, $modules);
41
42
        $this->methodType = $this->container->request->getMethod() ?: 'GET';
0 ignored issues
show
Bug introduced by
Accessing request on the interface Micro\Base\IContainer suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function action($name = 'index')
49
    {
50
        $actionClass = false;
51
        if (!method_exists($this, 'action' . ucfirst($name)) && !$actionClass = $this->getActionClassByName($name)) {
52
            $this->response->setStatus(500, 'Action "' . $name . '" not found into ' . get_class($this));
53
54
            return $this->response;
55
        }
56
57
        $types = $this->actionsTypes();
58
59
        if (!empty($types[$name]) && $this->methodType !== $types[$name]) {
60
            $this->response->setStatus(500,
61
                'Action "' . $name . '" not run with method "' . $this->methodType . '" into ' . get_class($this)
62
            );
63
64
            return $this->response;
65
        }
66
67
        $filters = method_exists($this, 'filters') ? $this->filters() : [];
0 ignored issues
show
Bug introduced by
The method filters() does not exist on Micro\Mvc\Controllers\RichController. Did you maybe mean applyFilters()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
68
69
        $this->applyFilters($name, true, $filters, null);
70
71
        $view = null;
0 ignored issues
show
Unused Code introduced by
$view is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72 View Code Duplication
        if ($actionClass) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
73
            /** @var \Micro\Mvc\Action $cl */
74
            $cl = new $actionClass ($this->container);
75
            $view = $cl->run();
76
        } else {
77
            $view = $this->{'action' . ucfirst($name)}();
78
        }
79
80
        if ($this->response->getContentType() !== $this->format) {
81
            $this->response->setContentType($this->format);
82
        }
83
84
        $this->response->setBody($this->switchContentType($this->applyFilters($name, false, $filters, $view)));
85
86
        return $this->response;
87
    }
88
89
    /**
90
     * Define types for actions
91
     *
92
     * <code>
93
     *  // DELETE, GET, HEAD, OPTIONS, POST, PUT
94
     * public function actionsTypes() {
95
     *  return [
96
     *     'create' => 'POST',
97
     *     'read'   => 'GET',
98
     *     'update' => 'UPDATE'
99
     *     'delete' => 'DELETE'
100
     *  ];
101
     * }
102
     * </code>
103
     *
104
     * @access public
105
     *
106
     * @return array
107
     * @abstract
108
     */
109
    abstract public function actionsTypes();
110
111
    /**
112
     * Switch content type
113
     *
114
     * @access protected
115
     *
116
     * @param mixed $data Any content
117
     *
118
     * @return string
119
     */
120
    protected function switchContentType($data)
121
    {
122
        switch ($this->format) {
123
            case 'application/json':
124
                $data = json_encode(is_object($data) ? (array)$data : $data);
125
                break;
126
127
            case 'application/xml':
128
                $data = is_object($data) ? (string)$data : $data;
129
                break;
130
131
            default:
132
                $data = (string)$data;
133
        }
134
135
        return $data;
136
    }
137
}
138