aboutsummaryrefslogtreecommitdiff
path: root/src/Controller
diff options
context:
space:
mode:
authorChristian Cleberg <hello@cleberg.net>2023-06-14 22:08:34 -0500
committerChristian Cleberg <hello@cleberg.net>2023-06-14 22:08:34 -0500
commit49efb238b879bce764d04dad99c7a169e80f93dd (patch)
tree7b1d26dbe86710ee7830b3fe6ff82afff3712e2d /src/Controller
parent53488151f29e3afbd1b1348597ff2123b97ad2d7 (diff)
downloadhn-49efb238b879bce764d04dad99c7a169e80f93dd.tar.gz
hn-49efb238b879bce764d04dad99c7a169e80f93dd.tar.bz2
hn-49efb238b879bce764d04dad99c7a169e80f93dd.zip
massive overhaul to implement proper MVC
Diffstat (limited to 'src/Controller')
-rw-r--r--src/Controller/FeedController.php47
-rw-r--r--src/Controller/RouteController.php171
2 files changed, 218 insertions, 0 deletions
diff --git a/src/Controller/FeedController.php b/src/Controller/FeedController.php
new file mode 100644
index 0000000..0e3e3b4
--- /dev/null
+++ b/src/Controller/FeedController.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace HN\Controllers;
+
+class FeedController
+{
+ /**
+ * @var string
+ */
+ private string $canonical_url;
+ /**
+ * @var string
+ */
+ private string $description;
+ /**
+ * @var string
+ */
+ private string $title;
+ /**
+ * @var string
+ */
+ private string $content;
+ /**
+ * @var false|string
+ */
+ private mixed $current_year;
+
+ public function __construct(string $canonical_url, string $description, string $title, string $content)
+ {
+ $this->canonical_url = $canonical_url;
+ $this->description = $description;
+ $this->title = $title;
+ $this->content = $content;
+ $this->current_year = date("Y");
+ }
+
+ /**
+ * Request template to be presented to the user
+ *
+ * @access public
+ * @author cmc <hello@cleberg.net>
+ */
+ public function render(): void
+ {
+ include_once 'src/View/BaseTemplate.php';
+ }
+}
diff --git a/src/Controller/RouteController.php b/src/Controller/RouteController.php
new file mode 100644
index 0000000..0d9befa
--- /dev/null
+++ b/src/Controller/RouteController.php
@@ -0,0 +1,171 @@
+<?php
+
+namespace HN\Controllers;
+
+require_once 'src/Controller/FeedController.php';
+
+use function HN\Models\GetApiResults;
+use function HN\Models\ParseItem;
+use function HN\Models\ParseStories;
+use function HN\Models\ParseUser;
+
+class RouteController
+{
+ /**
+ * @var string
+ */
+ private string $request;
+
+ public function __construct(string $request)
+ {
+ $this->request = $request;
+ }
+
+ /**
+ * Route the user to the appropriate function, based on the URL
+ *
+ * @access public
+ * @return void No return type; send user to FeedController->render() or a 404 error
+ * @author cmc <hello@cleberg.net>
+ */
+ public function routeUser(): void
+ {
+ include_once 'src/Model/ApiService.php';
+ $path = ltrim($this->request, '/');
+ $elements = explode('/', $path);
+
+ switch (array_shift($elements)) {
+ case '':
+ case 'top':
+ $feed = new FeedController(
+ $GLOBALS['full_domain'],
+ 'The top stories from Hacker News, proxied by hn.',
+ 'hn',
+ ParseStories(
+ GetApiResults(
+ 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=10&orderBy="$key"'
+ ),
+ 'Top'
+ )
+ );
+
+ $feed->render();
+ break;
+
+ case 'best':
+ $feed = new FeedController(
+ $GLOBALS['full_domain'] . '/Best/',
+ 'The best stories from Hacker News, proxied by hn.',
+ 'hn ~ best',
+ ParseStories(
+ GetApiResults(
+ 'https://hacker-news.firebaseio.com/v0/beststories.json?limitToFirst=10&orderBy="$key"'
+ ),
+ 'Best'
+ )
+ );
+
+ $feed->render();
+ break;
+
+ case 'new':
+ $feed = new FeedController(
+ $GLOBALS['full_domain'] . '/new/',
+ 'The newest stories from Hacker News, proxied by hn.',
+ 'hn ~ new',
+ ParseStories(
+ GetApiResults(
+ 'https://hacker-news.firebaseio.com/v0/newstories.json?limitToFirst=10&orderBy="$key"'
+ ),
+ 'New'
+ )
+ );
+
+ $feed->render();
+ break;
+
+ case 'ask':
+ $feed = new FeedController(
+ $GLOBALS['full_domain'] . '/ask/',
+ 'The top asks from Hacker News, proxied by hn.',
+ 'hn ~ ask',
+ ParseStories(
+ GetApiResults(
+ 'https://hacker-news.firebaseio.com/v0/askstories.json?limitToFirst=10&orderBy="$key"'
+ ),
+ 'Ask'
+ )
+ );
+
+ $feed->render();
+ break;
+
+ case 'show':
+ $feed = new FeedController(
+ $GLOBALS['full_domain'] . '/show/',
+ 'The latest showcases from Hacker News, proxied by hn.',
+ 'hn ~ show',
+ ParseStories(
+ GetApiResults(
+ 'https://hacker-news.firebaseio.com/v0/showstories.json?limitToFirst=10&orderBy="$key"'
+ ),
+ 'Show'
+ )
+ );
+
+ $feed->render();
+ break;
+
+ case 'job':
+ $feed = new FeedController(
+ $GLOBALS['full_domain'] . '/job/',
+ 'The latest jobs from Hacker News, proxied by hn.',
+ 'hn ~ jobs',
+ ParseStories(
+ GetApiResults(
+ 'https://hacker-news.firebaseio.com/v0/jobstories.json?limitToFirst=10&orderBy="$key"'
+ ),
+ 'Job'
+ )
+ );
+
+ $feed->render();
+ break;
+
+ case 'user':
+ $feed = new FeedController(
+ $GLOBALS['full_domain'] . '/user/' . $elements[0],
+ 'The Hacker News profile for ' . $elements[0] . ', proxied by hn.',
+ 'hn ~ ' . $elements[0],
+ ParseUser(
+ GetApiResults(
+ 'https://hacker-news.firebaseio.com/v0/user/' . $elements[0] . '.json'
+ ),
+ 'User: ' . $elements[0]
+ )
+ );
+
+ $feed->render();
+ break;
+
+ case 'item':
+ $feed = new FeedController(
+ $GLOBALS['full_domain'] . '/item/' . $elements[0],
+ 'Hacker News story ' . $elements[0] . ', proxied by hn.',
+ 'hn ~ ' . $elements[0],
+ ParseItem(
+ GetApiResults(
+ 'https://hacker-news.firebaseio.com/v0/item/' . $elements[0] . '.json'
+ ),
+ 'Item: ' . $elements[0]
+ )
+ );
+
+ $feed->render();
+ break;
+
+ default:
+ header('HTTP/1.1 404 Not Found');
+ }
+ }
+} \ No newline at end of file