Completed
Push — master ( 3df2e9...04f83a )
by Alex
07:47
created

Presenter::getRequestParamsFetcher()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Mezon\Application;
3
4
use Mezon\Transport\RequestParamsInterface;
5
6
/**
7
 * Class Presenter
8
 *
9
 * @package Mezon
10
 * @subpackage Presenter
11
 * @author Dodonov A.A.
12
 * @version v.1.0 (2020/01/12)
13
 * @copyright Copyright (c) 2020, aeon.org
14
 */
15
16
/**
17
 * Base class for all views
18
 *
19
 * @deprecated since 2020-06-26
20
 */
21
class Presenter extends \Mezon\Application\AbstractPresenter
22
{
23
24
    /**
25
     * Router
26
     *
27
     * @var \Mezon\Transport\RequestParams
28
     */
29
    private $requestParams = null;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param string $presenterName
35
     *            Presenter name to be executed
36
     * @param ?\Mezon\Transport\RequestParams $requestParams
37
     *            request params fetcher
38
     */
39
    public function __construct(string $presenterName = '', ?\Mezon\Transport\RequestParams $requestParams = null)
40
    {
41
        $this->setPresenterName($presenterName);
42
43
        $this->requestParams = $requestParams;
44
    }
45
46
    /**
47
     * Method return $requestParams and thrown exception if it was not set
48
     *
49
     * @return RequestParamsInterface request params fetcher
50
     * @deprecated since 2020-07-06 use getRequestParamsFetcher
51
     * @codeCoverageIgnore
52
     */
53
    public function getParamsFetcher(): RequestParamsInterface
54
    {
55
        return $this->getRequestParamsFetcher();
56
    }
57
58
    /**
59
     * Method returns $this->requestParams and creates this object if necessery
60
     *
61
     * @return RequestParamsInterface
62
     */
63
    public function getRequestParamsFetcher(): RequestParamsInterface
64
    {
65
        if ($this->requestParams === null) {
66
            throw (new \Exception('Param fetcher was not setup'));
67
        }
68
69
        return $this->requestParams;
70
    }
71
72
    /**
73
     * Method runs controller
74
     *
75
     * @param
76
     *            string PresenterName
77
     *            Presenter name to be run
78
     * @return mixed result of the controller
79
     */
80
    public function run(string $presenterName = '')
81
    {
82
        if ($presenterName === '') {
83
            $presenterName = $this->getPresenterName();
84
        }
85
86
        if ($presenterName === '') {
87
            $presenterName = 'Default';
88
        }
89
90
        if (method_exists($this, 'presenter' . $presenterName)) {
91
            return call_user_func([
92
                $this,
93
                'presenter' . $presenterName
94
            ]);
95
        }
96
97
        throw (new \Exception('Presenter ' . $presenterName . ' was not found'));
98
    }
99
100
    /**
101
     * May be these functions should be excluded to base class common with View
102
     */
103
104
    /**
105
     * Method redirects user to another page
106
     *
107
     * @param string $url
108
     * @codeCoverageIgnore
109
     */
110
    public function redirectTo(string $url): void
111
    {
112
        header("Location: $url");
113
        exit(0);
114
    }
115
116
    /**
117
     * Method builds route data
118
     *
119
     * @param string $route
120
     *            route
121
     * @param string $method
122
     *            HTTP method
123
     * @param string $function
124
     *            controller's function name
125
     * @return array built route data
126
     */
127
    public function buildRoute(string $route, string $method, string $function): array
128
    {
129
        return [
130
            'route' => $route,
131
            'method' => $method,
132
            'callback' => [
133
                $this,
134
                $function
135
            ]
136
        ];
137
    }
138
}
139