33namespace Fleetbase \Http \Controllers \Internal \v1 ;
44
55use Fleetbase \Http \Controllers \Controller ;
6+ use Fleetbase \Support \FleetbaseBlog ;
67use Fleetbase \Support \Http ;
78use Fleetbase \Types \Country ;
89use Fleetbase \Types \Currency ;
@@ -186,8 +187,8 @@ public function country($code, Request $request)
186187 */
187188 public function fleetbaseBlog (Request $ request )
188189 {
189- $ limit = $ request ->integer ('limit ' , 6 );
190- $ cacheKey = " fleetbase_blog_posts_ { $ limit}" ;
190+ $ limit = max ( 1 , min ( $ request ->integer ('limit ' , 6 ), 20 ) );
191+ $ cacheKey = $ this -> getFleetbaseBlogCacheKey ( $ limit) ;
191192 $ cacheTTL = now ()->addDays (4 ); // 4 days as requested
192193
193194 // Try to get from cache
@@ -212,7 +213,8 @@ public function fleetbaseBlog(Request $request)
212213 */
213214 protected function fetchBlogPosts (int $ limit ): array
214215 {
215- $ rssUrl = 'https://www.fleetbase.io/post/rss.xml ' ;
216+ $ limit = max (1 , min ($ limit , 20 ));
217+ $ rssUrl = $ this ->getFleetbaseBlogFeedUrl ();
216218 $ posts = [];
217219
218220 try {
@@ -230,31 +232,7 @@ protected function fetchBlogPosts(int $limit): array
230232 return [];
231233 }
232234
233- // Parse XML
234- $ rss = simplexml_load_string ($ response ->body ());
235-
236- if (!$ rss || !isset ($ rss ->channel ->item )) {
237- Log::error ('[Blog] Invalid RSS feed structure ' );
238-
239- return [];
240- }
241-
242- foreach ($ rss ->channel ->item as $ item ) {
243- $ posts [] = [
244- 'title ' => (string ) $ item ->title ,
245- 'link ' => (string ) $ item ->link ,
246- 'guid ' => (string ) $ item ->guid ,
247- 'description ' => (string ) $ item ->description ,
248- 'pubDate ' => (string ) $ item ->pubDate ,
249- 'media_content ' => (string ) data_get ($ item , 'media:content.url ' ),
250- 'media_thumbnail ' => (string ) data_get ($ item , 'media:thumbnail.url ' ),
251- ];
252-
253- // Early exit if we have enough
254- if (count ($ posts ) >= $ limit ) {
255- break ;
256- }
257- }
235+ $ posts = $ this ->parseBlogPostsFromRss ($ response ->body (), $ limit );
258236
259237 Log::info ('[Blog] Successfully fetched blog posts ' , ['count ' => count ($ posts )]);
260238 } catch (\Exception $ e ) {
@@ -267,6 +245,48 @@ protected function fetchBlogPosts(int $limit): array
267245 return array_slice ($ posts , 0 , $ limit );
268246 }
269247
248+ /**
249+ * Parse blog posts from an RSS payload.
250+ */
251+ protected function parseBlogPostsFromRss (string $ rssXml , int $ limit ): array
252+ {
253+ return FleetbaseBlog::parseRss ($ rssXml , $ limit , $ this ->getFleetbaseBlogUrl ());
254+ }
255+
256+ /**
257+ * Get the cache key for the Fleetbase blog feed.
258+ */
259+ protected function getFleetbaseBlogCacheKey (int $ limit ): string
260+ {
261+ $ sourceHash = md5 ($ this ->getFleetbaseBlogFeedUrl () . '| ' . $ this ->getFleetbaseBlogUrl ());
262+
263+ return "fleetbase_blog_posts_ {$ limit }_ {$ sourceHash }" ;
264+ }
265+
266+ /**
267+ * Get the public Fleetbase blog RSS feed URL.
268+ */
269+ protected function getFleetbaseBlogFeedUrl (): string
270+ {
271+ return FleetbaseBlog::getFeedUrl ();
272+ }
273+
274+ /**
275+ * Get the canonical Fleetbase blog URL.
276+ */
277+ protected function getFleetbaseBlogUrl (): string
278+ {
279+ return FleetbaseBlog::getBlogUrl ();
280+ }
281+
282+ /**
283+ * Rewrite Ghost publication links to the canonical Fleetbase.io blog URL.
284+ */
285+ protected function normalizeFleetbaseBlogLink (?string $ link ): string
286+ {
287+ return FleetbaseBlog::normalizeLink ($ link , $ this ->getFleetbaseBlogUrl ());
288+ }
289+
270290 /**
271291 * Manually refresh blog cache (can be called via webhook or admin panel).
272292 *
@@ -275,13 +295,13 @@ protected function fetchBlogPosts(int $limit): array
275295 public function refreshBlogCache ()
276296 {
277297 // Clear all blog caches
278- Cache::forget (' fleetbase_blog_posts_6 ' );
279- Cache::forget (' fleetbase_blog_posts_10 ' );
280- Cache::forget (' fleetbase_blog_posts_20 ' );
298+ Cache::forget ($ this -> getFleetbaseBlogCacheKey ( 6 ) );
299+ Cache::forget ($ this -> getFleetbaseBlogCacheKey ( 10 ) );
300+ Cache::forget ($ this -> getFleetbaseBlogCacheKey ( 20 ) );
281301
282302 // Warm up cache with default limit
283303 $ posts = $ this ->fetchBlogPosts (6 );
284- Cache::put (' fleetbase_blog_posts_6 ' , $ posts , now ()->addDays (4 ));
304+ Cache::put ($ this -> getFleetbaseBlogCacheKey ( 6 ) , $ posts , now ()->addDays (4 ));
285305
286306 return response ()->json ([
287307 'status ' => 'success ' ,
0 commit comments