Passed
Push — develop ( 8daac1...a2b8ae )
by Mykola
04:44
created

Action::execute()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 25
rs 9.4888
c 0
b 0
f 0
cc 5
nc 4
nop 2
1
<?php
2
/* 	Divine CMS - Open source CMS for widespread use.
3
    Copyright (c) 2019 Mykola Burakov ([email protected])
4
5
    See SOURCE.txt for other and additional information.
6
7
    This file is part of Divine CMS.
8
9
    This program is free software: you can redistribute it and/or modify
10
    it under the terms of the GNU General Public License as published by
11
    the Free Software Foundation, either version 3 of the License, or
12
    (at your option) any later version.
13
14
    This program is distributed in the hope that it will be useful,
15
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
    GNU General Public License for more details.
18
19
    You should have received a copy of the GNU General Public License
20
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22
namespace Divine\Engine\Core;
23
24
class Action
25
{
26
    private $id;
27
    private $route;
28
    private $method = 'index';
29
30
    public function __construct($route)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
31
    {
32
        $this->id = $route;
33
34
        $parts = explode('/', preg_replace('/[^a-zA-Z0-9_\/]/', '', (string) $route));
35
36
        // Break apart the route
37
        while ($parts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parts of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
38
            $file = SR_APPLICATION . 'controller/' . implode('/', $parts) . '.php';
39
40
            if (is_file($file)) {
41
                $this->route = implode('/', $parts);
42
43
                break;
44
            } else {
45
                $this->method = array_pop($parts);
46
            }
47
        }
48
    }
49
50
    public function getId()
51
    {
52
        return $this->id;
53
    }
54
55
    public function execute($registry, array $args = array())
56
    {
57
        // Stop any magical methods being called
58
        if (substr($this->method, 0, 2) == '__') {
59
            return new \Exception('Error: Calls to magic methods are not allowed!');
60
        }
61
62
        $file = SR_APPLICATION . 'controller/' . $this->route . '.php';
63
        $class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $this->route);
64
65
        // Initialize the class
66
        if (is_file($file)) {
67
            include_once($file);
68
69
            $controller = new $class($registry);
70
        } else {
71
            return new \Exception('Error: Could not call ' . $this->route . '/' . $this->method . '!');
72
        }
73
74
        $reflection = new \ReflectionClass($class);
75
76
        if ($reflection->hasMethod($this->method) && $reflection->getMethod($this->method)->getNumberOfRequiredParameters() <= count($args)) {
77
            return call_user_func_array(array($controller, $this->method), $args);
78
        } else {
79
            return new \Exception('Error: Could not call ' . $this->route . '/' . $this->method . '!');
80
        }
81
    }
82
}
83