alexdodonov /
mezon-application
| 1 | <?php |
||
| 2 | namespace Mezon\Application; |
||
| 3 | |||
| 4 | use Mezon\Transport\RequestParamsInterface; |
||
| 5 | use Mezon\Application\ControllerInterface; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Class Controller |
||
| 9 | * |
||
| 10 | * @package Mezon |
||
| 11 | * @subpackage Controller |
||
| 12 | * @author Dodonov A.A. |
||
| 13 | * @version v.1.0 (2020/01/12) |
||
| 14 | * @copyright Copyright (c) 2020, aeon.org |
||
| 15 | */ |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Base class for all views |
||
| 19 | * |
||
| 20 | * @deprecated since 2020-06-26 |
||
| 21 | */ |
||
| 22 | class Controller extends ControllerInterface |
||
|
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Router |
||
| 27 | * |
||
| 28 | * @var RequestParamsInterface |
||
| 29 | */ |
||
| 30 | private $requestParams = null; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Constructor |
||
| 34 | * |
||
| 35 | * @param string $controllerName |
||
| 36 | * Controller name to be executed |
||
| 37 | * @param ?RequestParamsInterface $requestParams |
||
| 38 | * request params fetcher |
||
| 39 | */ |
||
| 40 | public function __construct(string $controllerName = '', ?RequestParamsInterface $requestParams = null) |
||
| 41 | { |
||
| 42 | $this->setControllerName($controllerName); |
||
| 43 | |||
| 44 | $this->requestParams = $requestParams; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Method return $requestParams and thrown exception if it was not set |
||
| 49 | * |
||
| 50 | * @return RequestParamsInterface request params fetcher |
||
| 51 | */ |
||
| 52 | public function getParamsFetcher(): RequestParamsInterface |
||
| 53 | { |
||
| 54 | if ($this->requestParams === null) { |
||
| 55 | throw (new \Exception('Param fetcher was not setup')); |
||
| 56 | } |
||
| 57 | |||
| 58 | return $this->requestParams; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Method runs controller |
||
| 63 | * |
||
| 64 | * @param |
||
| 65 | * string ControllerName |
||
| 66 | * Controller name to be run |
||
| 67 | * @return mixed result of the controller |
||
| 68 | */ |
||
| 69 | public function run(string $controllerName = '') |
||
| 70 | { |
||
| 71 | if ($controllerName === '') { |
||
| 72 | $controllerName = $this->getControllerName(); |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($controllerName === '') { |
||
| 76 | $controllerName = 'Default'; |
||
| 77 | } |
||
| 78 | |||
| 79 | if (method_exists($this, 'controller' . $controllerName)) { |
||
| 80 | return call_user_func([ |
||
| 81 | $this, |
||
| 82 | 'controller' . $controllerName |
||
| 83 | ]); |
||
| 84 | } |
||
| 85 | |||
| 86 | throw (new \Exception('Controller ' . $controllerName . ' was not found')); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * May be these functions should be excluded to base class common with View |
||
| 91 | */ |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Method redirects user to another page |
||
| 95 | * |
||
| 96 | * @param string $url |
||
| 97 | * @codeCoverageIgnore |
||
| 98 | */ |
||
| 99 | public function redirectTo(string $url): void |
||
| 100 | { |
||
| 101 | header("Location: $url"); |
||
| 102 | exit(0); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Method builds route data |
||
| 107 | * |
||
| 108 | * @param string $route |
||
| 109 | * route |
||
| 110 | * @param string $method |
||
| 111 | * HTTP method |
||
| 112 | * @param string $function |
||
| 113 | * controller's function name |
||
| 114 | * @return array built route data |
||
| 115 | */ |
||
| 116 | public function buildRoute(string $route, string $method, string $function): array |
||
| 117 | { |
||
| 118 | return [ |
||
| 119 | 'route' => $route, |
||
| 120 | 'method' => $method, |
||
| 121 | 'callback' => [ |
||
| 122 | $this, |
||
| 123 | $function |
||
| 124 | ] |
||
| 125 | ]; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Method returns code of the last error |
||
| 130 | * |
||
| 131 | * @return int code of the last error |
||
| 132 | * @codeCoverageIgnore |
||
| 133 | */ |
||
| 134 | public function getErrorCode(): int |
||
| 135 | { |
||
| 136 | return $this->errorCode; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Method sets code of the last error |
||
| 141 | * |
||
| 142 | * @param int $code |
||
| 143 | * code of the last error |
||
| 144 | * @codeCoverageIgnore |
||
| 145 | */ |
||
| 146 | public function setErrorCode(int $errorCode): void |
||
| 147 | { |
||
| 148 | $this->errorCode = $errorCode; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Method return last error description |
||
| 153 | * |
||
| 154 | * @return string last error description |
||
| 155 | * @codeCoverageIgnore |
||
| 156 | */ |
||
| 157 | public function getErrorMessage(): string |
||
| 158 | { |
||
| 159 | return $this->errorMessage; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Method sets last error description |
||
| 164 | * |
||
| 165 | * @param |
||
| 166 | * string last error description |
||
| 167 | * @codeCoverageIgnore |
||
| 168 | */ |
||
| 169 | public function setErrorMessage(string $errorMessage): void |
||
| 170 | { |
||
| 171 | $this->errorMessage = $errorMessage; |
||
| 172 | } |
||
| 173 | } |
||
| 174 |