Test Failed
Push — main ( ed6f86...72154c )
by Marc
03:36
created

get_homepage()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 6
nop 2
dl 0
loc 19
rs 9.2222
c 0
b 0
f 0
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 === 1) {
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
    switch (true) {
67
        case \str_starts_with($uri, POST_INDEX_KEY.FWD_SLASH):
68
            $list = get_posts($pageNum, $perPage);
69
            break;
70
        case \str_starts_with($uri, CAT_INDEX_KEY.FWD_SLASH):
71
            $list = get_categories($pageNum, $perPage);
72
            break;
73
        case \str_starts_with($uri, TAG_INDEX_KEY.FWD_SLASH):
74
            $list = get_tags($pageNum, $perPage);
75
            break;
76
        default: // home page or static page
77
            $list = [];
78
            $template = SINGLE_TEMPLATE;
79
    }
80
81
    if ($uri === HOME_INDEX_KEY) {
82
        $content = get_homepage($pageNum, $perPage);
83
    } else {
84
        $content = get_content_object($uri, $list);
85
    }
86
87
    if (!empty($content) && empty($content->template)) {
88
        $content->template = $template;
89
    }
90
91
    return $content;
92
}
93
94
function get_homepage(int $pageNum, int $perPage): \stdClass {
95
    if (get_config()->getBool(Config::KEY_STATIC_INDEX)) {
96
        $content = get_content_object(HOME_INDEX_KEY);
97
        if ($content === null) {
98
            throw new InternalException("Unable to find the home index page!");
99
        }
100
        if (empty($content->template)) {
101
            $content->template = SINGLE_TEMPLATE;
102
        }
103
    } else {
104
        $content = get_content_object(HOME_INDEX_KEY, get_posts($pageNum, $perPage));
105
        if ($content === null) {
106
            throw new InternalException("Unable to find the home index page!");
107
        }
108
        if (empty($content->template)) {
109
            $content->template = LIST_TEMPLATE;
110
        }
111
    }
112
    return $content;
113
}
114
115
/**
116
 * Strips parameters from the given URL.
117
 * @param string $url
118
 * @return string returns the URL without the parameters.
119
 */
120
function strip_url_parameters(string $url): string {
121
    if (($pos = \strpos($url, '?')) === false) {
122
        return $url;
123
    }
124
    parse_query(\substr($url, $pos + 1));
125
    return \substr($url, 0, $pos);
126
}
127
128
/**
129
 * @param string $query (Optional). Calling with this parameter set will populate the returned data array.
130
 * @return array<string, string>
131
 */
132
function parse_query(string $query = null): array {
133
    static $data = [];
134
    if (empty($data) && $query !== null) {
135
        \parse_str($query, $data);
136
    }
137
    return $data;
138
}
139
140
/**
141
 * Return the query string value for the given key.
142
 * @param string $key
143
 * @return string|NULL <code>null</code> if the key is not found
144
 */
145
function get_query_parameter(string $key): ?string {
146
    $params = parse_query();
147
    if (isset($params[$key])) {
148
        return $params[$key];
149
    }
150
    return null;
151
}
152