Completed
Push — master ( 21ef4f...df9e52 )
by Alex
04:31
created

Controller::getControllerName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
namespace Mezon\Application;
3
4
/**
5
 * Class Controller
6
 *
7
 * @package Mezon
8
 * @subpackage Controller
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2020/01/12)
11
 * @copyright Copyright (c) 2020, aeon.org
12
 */
13
14
/**
15
 * Base class for all views
16
 *
17
 * @deprecated since 2020-06-26
18
 */
19
class Controller extends \Mezon\Application\ControllerInterface
0 ignored issues
show
Deprecated Code introduced by
The class Mezon\Application\ControllerInterface has been deprecated: since 2020-06-26 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

19
class Controller extends /** @scrutinizer ignore-deprecated */ \Mezon\Application\ControllerInterface
Loading history...
20
{
21
22
    /**
23
     * Router
24
     *
25
     * @var \Mezon\Transport\RequestParams
26
     */
27
    private $requestParams = null;
28
29
    /**
30
     * Constructor
31
     *
32
     * @param string $controllerName
33
     *            Controller name to be executed
34
     * @param ?\Mezon\Transport\RequestParams $requestParams
35
     *            request params fetcher
36
     */
37
    public function __construct(string $controllerName = '', ?\Mezon\Transport\RequestParams $requestParams = null)
38
    {
39
        $this->setControllerName($controllerName);
40
41
        $this->requestParams = $requestParams;
42
    }
43
44
    /**
45
     * Method runs controller
46
     *
47
     * @param
48
     *            string ControllerName
49
     *            Controller name to be run
50
     * @return mixed result of the controller
51
     */
52
    public function run(string $controllerName = '')
53
    {
54
        if ($controllerName === '') {
55
            $controllerName = $this->getControllerName();
56
        }
57
58
        if ($controllerName === '') {
59
            $controllerName = 'Default';
60
        }
61
62
        if (method_exists($this, 'controller' . $controllerName)) {
63
            return call_user_func([
64
                $this,
65
                'controller' . $controllerName
66
            ]);
67
        }
68
69
        throw (new \Exception('Controller ' . $controllerName . ' was not found'));
70
    }
71
72
    /**
73
     * May be these functions should be excluded to base class common with View
74
     */
75
76
    /**
77
     * Method redirects user to another page
78
     *
79
     * @param string $url
80
     * @codeCoverageIgnore
81
     */
82
    public function redirectTo(string $url): void
83
    {
84
        header("Location: $url");
85
        exit(0);
86
    }
87
88
    /**
89
     * Method builds route data
90
     *
91
     * @param string $route
92
     *            route
93
     * @param string $method
94
     *            HTTP method
95
     * @param string $function
96
     *            controller's function name
97
     * @return array built route data
98
     */
99
    public function buildRoute(string $route, string $method, string $function): array
100
    {
101
        return [
102
            'route' => $route,
103
            'method' => $method,
104
            'callback' => [
105
                $this,
106
                $function
107
            ]
108
        ];
109
    }
110
}
111