Passed
Push — main ( ffd35e...44834b )
by Marc
03:18
created

is_list_page()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 15
rs 9.8666
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
    if ($result === SINGLE_POST_REQUEST) {
44
        $content = get_content_object($uri);
45
    } else {
46
        $content = process_request($uri, get_pagination_pagenumber(), get_config()->getInt(Config::KEY_POSTS_PERPAGE));
47
    }
48
49
    if ($content === null) {
50
        return not_found();
51
    }
52
53
    return render(vars: get_template_context($content));
54
}
55
56
/**
57
 * Process a request for a single blog-post.
58
 * @param string $uri
59
 * @param int $pageNum
60
 * @param int $perPage
61
 * @return \stdClass|NULL
62
 */
63
function process_request(string $uri, int $pageNum, int $perPage): ?\stdClass {
64
    $template = LIST_TEMPLATE;
65
66
    if (HOME_INDEX_KEY === $uri) {
67
        return get_homepage($pageNum, $perPage);
68
    }
69
70
    $list = is_landing_page($uri, $pageNum, $perPage);
71
    if (empty($list)) {
72
        $list = is_list_page($uri, $pageNum, $perPage);
73
        if (empty($list)) {
74
            $template = SINGLE_TEMPLATE;
75
        }
76
    }
77
78
    return get_content_object($uri, $list, $template);
79
}
80
81
/**
82
 * Checks if the given URI is a landing page (excluding the home page), if so returns a list of the
83
 * appropriate content objects for that page.
84
 * @param string $uri
85
 * @return array<\stdClass>
86
 */
87
function is_landing_page(string $uri, int $pageNum = 1, int $perPage = 0): array {
88
    $list = [];
89
    switch (true) {
90
        case $uri === CAT_INDEX_KEY:
91
            $list = get_categories($pageNum, $perPage);
92
            break;
93
        case $uri === POST_INDEX_KEY:
94
            $list = get_posts($pageNum, $perPage);
95
            break;
96
        case $uri === TAG_INDEX_KEY:
97
            $list = get_tags($pageNum, $perPage);
98
            break;
99
        default:
100
            // Do nothing
101
    }
102
    return $list;
103
}
104
105
/**
106
 * Checks if the given URI is a list page (excluding the home page), if so returns a list of the
107
 * approriate content object for that page.
108
 * @param string $uri
109
 * @param int $pageNum
110
 * @param int $perPage
111
 * @return array<\stdClass>
112
 */
113
function is_list_page(string $uri, int $pageNum = 1, int $perPage = 0): array {
114
    $list = [];
115
    switch (true) {
116
        case \str_starts_with($uri, POST_INDEX_KEY):
117
            $list = get_posts($pageNum, $perPage);
118
            break;
119
        case \str_starts_with($uri, CAT_INDEX_KEY.FWD_SLASH):
120
            $list = get_posts_for_category($uri, $pageNum, $perPage);
121
            break;
122
        case \str_starts_with($uri, TAG_INDEX_KEY.FWD_SLASH):
123
            exit('POST for TAG');
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...
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return array. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
124
        default:
125
            // Do nothing
126
    }
127
    return $list;
128
}
129
130
/**
131
 * Returns the home page.
132
 * @param int $pageNum
133
 * @param int $perPage
134
 * @throws InternalException
135
 * @return \stdClass
136
 */
137
function get_homepage(int $pageNum, int $perPage): \stdClass {
138
    if (get_config()->getBool(Config::KEY_STATIC_INDEX)) {
139
        if (($content = get_content_object(HOME_INDEX_KEY, /** @scrutinizer ignore-type */ template: SINGLE_TEMPLATE)) === null) {
140
            throw new InternalException("Unable to find the home index page!");
141
        }
142
    } else {
143
        if (($content = get_content_object(HOME_INDEX_KEY, get_posts($pageNum, $perPage), LIST_TEMPLATE)) === null) {
144
            throw new InternalException("Unable to find the home index page!");
145
        }
146
    }
147
    return $content;
148
}
149
150
/**
151
 * Strips parameters from the given URL.
152
 * @param string $url
153
 * @return string returns the URL without the parameters.
154
 */
155
function strip_url_parameters(string $url): string {
156
    if (($pos = \strpos($url, '?')) === false) {
157
        return $url;
158
    }
159
    parse_query(\substr($url, $pos + 1));
160
    return \substr($url, 0, $pos);
161
}
162
163
/**
164
 * @param string $query (Optional). Calling with this parameter set will populate the returned data array.
165
 * @return array<string, string>
166
 */
167
function parse_query(string $query = null): array {
168
    static $data = [];
169
    if (empty($data) && $query !== null) {
170
        \parse_str($query, $data);
171
    }
172
    return $data;
173
}
174
175
/**
176
 * Return the query string value for the given key.
177
 * @param string $key
178
 * @return string|NULL <code>null</code> if the key is not found
179
 */
180
function get_query_parameter(string $key): ?string {
181
    $params = parse_query();
182
    if (isset($params[$key])) {
183
        return $params[$key];
184
    }
185
    return null;
186
}
187