Completed
Push — master ( 0ea0cc...2ded10 )
by Alex
02:05
created

Controller.php (1 issue)

Severity
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
    /**
112
     * Method returns code of the last error
113
     *
114
     * @return int code of the last error
115
     * @codeCoverageIgnore
116
     */
117
    public function getErrorCode(): int
118
    {
119
        return $this->errorCode;
120
    }
121
122
    /**
123
     * Method sets code of the last error
124
     *
125
     * @param int $code
126
     *            code of the last error
127
     * @codeCoverageIgnore
128
     */
129
    public function setErrorCode(int $errorCode): void
130
    {
131
        $this->errorCode = $errorCode;
132
    }
133
134
    /**
135
     * Method return last error description
136
     *
137
     * @return string last error description
138
     * @codeCoverageIgnore
139
     */
140
    public function getErrorMessage(): string
141
    {
142
        return $this->errorMessage;
143
    }
144
145
    /**
146
     * Method sets last error description
147
     *
148
     * @param
149
     *            string last error description
150
     * @codeCoverageIgnore
151
     */
152
    public function setErrorMessage(string $errorMessage): void
153
    {
154
        $this->errorMessage = $errorMessage;
155
    }
156
}
157