Passed
Push — main ( 415e55...cf1b5a )
by Marc
03:47
created

process_admin_request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
use html_go\exceptions\InternalException;
4
use html_go\model\Config;
5
6
/**
7
 * The main entry point. Called from <code>index.php</code> in the application
8
 * root. The parameters are provided for testing thus they have default values.
9
 * @param string $uri
10
 * @param string $method
11
 * @return string The html to be rendered.
12
 */
13
function dispatch(string $uri = null, string $method = HTTP_GET): string {
14
    if ($uri === null) {
15
        $uri = $_SERVER['REQUEST_URI']; // @codeCoverageIgnore
16
        $method = \strtoupper($_SERVER['REQUEST_METHOD']); // @codeCoverageIgnore
17
    }
18
    $uri = strip_url_parameters($uri);
19
    $uri = \trim($uri, FWD_SLASH);
20
    if (empty($uri)) {
21
        $uri = HOME_INDEX_KEY;
22
    }
23
    return route($uri, $method);
24
}
25
26
/**
27
 * Route the given HTTP request.
28
 * @param string $uri The requested URI
29
 * @param string $method the HTTP method
30
 * @throws InternalException
31
 * @return string
32
 */
33
function route(string $uri, string $method): string {
34
    if ($method === HTTP_POST) {
35
        return not_found();
36
    }
37
38
    $result = \preg_match(POST_REQ_REGEX, $uri);
39
    if ($result === false) {
40
        throw new InternalException("preg_match() failed checking [$uri]"); // @codeCoverageIgnore
41
    }
42
43
    $adminCtx = get_config()->getString(Config::KEY_ADMIN_CONTEXT);
44
    if ($result === SINGLE_POST_REQUEST) {
45
        $content = get_content_object($uri);
46
    } elseif (\str_starts_with($uri, $adminCtx)) {
47
        $content = process_admin_request($uri, $method);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $content is correct as process_admin_request($uri, $method) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
48
    } else {
49
        $content = process_request($uri, get_pagination_pagenumber(), get_config()->getInt(Config::KEY_POSTS_PERPAGE));
50
    }
51
52
    if ($content === null) {
53
        return not_found();
54
    }
55
56
    return render(vars: get_template_context($content));
57
}
58
59
/**
60
 * Process a request for a single blog-post.
61
 * @param string $uri
62
 * @param int $pageNum
63
 * @param int $perPage
64
 * @return \stdClass|NULL
65
 */
66
function process_request(string $uri, int $pageNum, int $perPage): ?\stdClass {
67
    $template = LIST_TEMPLATE;
68
69
    if (HOME_INDEX_KEY === $uri) {
70
        return get_homepage($pageNum, $perPage);
71
    }
72
73
    $list = is_landing_page($uri, $pageNum, $perPage);
74
    if (empty($list)) {
75
        $list = is_list_page($uri, $pageNum, $perPage);
76
        if (empty($list)) {
77
            $template = SINGLE_TEMPLATE;
78
        }
79
    }
80
81
    return get_content_object($uri, $list, $template);
82
}
83
84
/**
85
 * Process a request for an admin page.
86
 * @param string $uri
87
 * @param string $method
88
 * @return \stdClass|NULL
89
 */
90
function process_admin_request(string $uri, string $method): ?\stdClass {
0 ignored issues
show
Unused Code introduced by
The parameter $uri is not used and could be removed. ( Ignorable by Annotation )

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

90
function process_admin_request(/** @scrutinizer ignore-unused */ string $uri, string $method): ?\stdClass {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $method is not used and could be removed. ( Ignorable by Annotation )

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

90
function process_admin_request(string $uri, /** @scrutinizer ignore-unused */ string $method): ?\stdClass {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
    require_once ADMIN_SYS_ROOT.DS.'functions.php';
92
    exit('admin page');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
93
}
94
95
/**
96
 * Checks if the given URI is a landing page (excluding the home page), if so returns a list of the
97
 * appropriate content objects for that page.
98
 * @param string $uri
99
 * @return array<\stdClass>
100
 */
101
function is_landing_page(string $uri, int $pageNum = 1, int $perPage = 0): array {
102
    $list = [];
103
    switch (true) {
104
        case $uri === CAT_INDEX_KEY:
105
            $list = get_categories($pageNum, $perPage);
106
            break;
107
        case $uri === POST_INDEX_KEY:
108
            $list = get_posts($pageNum, $perPage);
109
            break;
110
        case $uri === TAG_INDEX_KEY:
111
            $list = get_tags($pageNum, $perPage);
112
            break;
113
        default:
114
            // Do nothing
115
    }
116
    return $list;
117
}
118
119
/**
120
 * Checks if the given URI is a list page (excluding the home page), if so returns a list of the
121
 * approriate content object for that page.
122
 * @param string $uri
123
 * @param int $pageNum
124
 * @param int $perPage
125
 * @return array<\stdClass>
126
 */
127
function is_list_page(string $uri, int $pageNum = 1, int $perPage = 0): array {
128
    $list = [];
129
    switch (true) {
130
        case \str_starts_with($uri, POST_INDEX_KEY):
131
            $list = get_posts($pageNum, $perPage);
132
            break;
133
        case \str_starts_with($uri, CAT_INDEX_KEY.FWD_SLASH):
134
            $list = get_posts_for_category($uri, $pageNum, $perPage);
135
            break;
136
        case \str_starts_with($uri, TAG_INDEX_KEY.FWD_SLASH):
137
            $list = get_posts_for_tag($uri, $pageNum, $perPage);
138
            break;
139
        default:
140
            // Do nothing
141
    }
142
    return $list;
143
}
144
145
/**
146
 * Returns the home page.
147
 * @param int $pageNum
148
 * @param int $perPage
149
 * @throws InternalException
150
 * @return \stdClass
151
 */
152
function get_homepage(int $pageNum, int $perPage): \stdClass {
153
    if (get_config()->getBool(Config::KEY_STATIC_INDEX)) {
154
        if (($content = get_content_object(HOME_INDEX_KEY, /** @scrutinizer ignore-type */ template: SINGLE_TEMPLATE)) === null) {
155
            throw new InternalException("Unable to find the home index page!");
156
        }
157
    } else {
158
        if (($content = get_content_object(HOME_INDEX_KEY, get_posts($pageNum, $perPage), LIST_TEMPLATE)) === null) {
159
            throw new InternalException("Unable to find the home index page!");
160
        }
161
    }
162
    return $content;
163
}
164
165
/**
166
 * Strips parameters from the given URL.
167
 * @param string $url
168
 * @return string returns the URL without the parameters.
169
 */
170
function strip_url_parameters(string $url): string {
171
    if (($pos = \strpos($url, '?')) === false) {
172
        return $url;
173
    }
174
    parse_query(\substr($url, $pos + 1));
175
    return \substr($url, 0, $pos);
176
}
177
178
/**
179
 * @param string $query (Optional). Calling with this parameter set will populate the returned data array.
180
 * @return array<string, string>
181
 */
182
function parse_query(string $query = null): array {
183
    static $data = [];
184
    if (empty($data) && $query !== null) {
185
        \parse_str($query, $data);
186
    }
187
    return $data;
188
}
189
190
/**
191
 * Return the query string value for the given key.
192
 * @param string $key
193
 * @return string|NULL <code>null</code> if the key is not found
194
 */
195
function get_query_parameter(string $key): ?string {
196
    $params = parse_query();
197
    if (isset($params[$key])) {
198
        return $params[$key];
199
    }
200
    return null;
201
}
202