Passed
Push — main ( a4c215...efbc77 )
by Marc
03:54
created

get_home_page()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
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
    if ($result === 1) {
44
        $content = process_blog_post_request($uri, get_pagination_pagenumber(), get_config()->getInt(Config::KEY_POSTS_PERPAGE));
45
    } else {
46
        if (HOME_INDEX_KEY === $uri) {
47
            $content = get_home_page();
48
        } else {
49
            $content = get_content_object($uri);
50
        }
51
    }
52
53
    if ($content === null) {
54
        return not_found();
55
    }
56
57
    return render(vars: get_template_context($content));
58
}
59
60
/**
61
 * Process a request for the home page
62
 * @param string $uri
63
 * @return \stdClass|NULL
64
 */
65
function get_home_page(): ?\stdClass {
66
    if (get_config()->getBool(Config::KEY_STATIC_INDEX)) {
67
        return get_content_object(HOME_INDEX_KEY);
68
    }
69
    $content = get_content_object(HOME_INDEX_KEY, get_posts());
70
    $content->template = 'listing.html';
71
    return $content;
72
}
73
74
/**
75
 * Process a blog-post request
76
 * @param string $uri
77
 * @param int $pageNum
78
 * @param int $perPage
79
 * @return \stdClass|NULL
80
 */
81
function process_blog_post_request(string $uri, int $pageNum, int $perPage): ?\stdClass {
82
    switch ($uri) {
83
        case HOME_INDEX_KEY:
84
            if (get_config()->getBool(Config::KEY_STATIC_INDEX)) {
85
                $content = get_content_object($uri);
86
            } else {
87
                $content = get_content_object($uri, get_posts($pageNum, $perPage));
88
            }
89
            break;
90
        case BLOG_INDEX_KEY:
91
            $content = get_content_object($uri, get_posts($pageNum, $perPage));
92
            break;
93
        case CAT_INDEX_KEY:
94
            $content = get_content_object($uri, get_categories($pageNum, $perPage));
95
            break;
96
        case TAG_INDEX_KEY:
97
            $content = get_content_object($uri, get_tags($pageNum, $perPage));
98
            break;
99
        default:
100
            $content = get_content_object($uri);
101
    }
102
    return $content;
103
}
104
105
/**
106
 * Strips parameters from the given URL.
107
 * @param string $url
108
 * @return string returns the URL without the parameters.
109
 */
110
function strip_url_parameters(string $url): string {
111
    if (($pos = \strpos($url, '?')) === false) {
112
        return $url;
113
    }
114
    parse_query(\substr($url, $pos + 1));
115
    return \substr($url, 0, $pos);
116
}
117
118
/**
119
 * @param string $query (Optional). Calling with this parameter set will populate the returned data array.
120
 * @return array<string, string>
121
 */
122
function parse_query(string $query = null): array {
123
    static $data = [];
124
    if (empty($data) && $query !== null) {
125
        \parse_str($query, $data);
126
    }
127
    return $data;
128
}
129
130
/**
131
 * Return the query string value for the given key.
132
 * @param string $key
133
 * @return string|NULL <code>null</code> if the key is not found
134
 */
135
function get_query_parameter(string $key): ?string {
136
    $params = parse_query();
137
    if (isset($params[$key])) {
138
        return $params[$key];
139
    }
140
    return null;
141
}
142