From c1581c1fd4eff85a2bad5881e02dbd1a02ddca48 Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Fri, 21 Jan 2022 08:52:28 +0000
Subject: [PATCH] refactor: rename audio_file urls to audio urls

---
 app/Entities/Episode.php                      | 29 +++++++------------
 app/Helpers/id3_helper.php                    |  1 -
 app/Helpers/rss_helper.php                    |  2 +-
 app/Helpers/seo_helper.php                    |  2 +-
 modules/Analytics/Config/Analytics.php        |  6 ++--
 .../EpisodeAnalyticsController.php            |  4 +--
 .../Analytics/Helpers/analytics_helper.php    | 12 ++++----
 themes/cp_app/embed.php                       |  2 +-
 themes/cp_app/episode/_layout.php             |  2 +-
 themes/cp_app/episode/_partials/card.php      |  2 +-
 .../cp_app/episode/_partials/preview_card.php |  2 +-
 11 files changed, 27 insertions(+), 37 deletions(-)

diff --git a/app/Entities/Episode.php b/app/Entities/Episode.php
index 80286775bf..1b1c0e0f21 100644
--- a/app/Entities/Episode.php
+++ b/app/Entities/Episode.php
@@ -39,9 +39,9 @@ use RuntimeException;
  * @property string $title
  * @property int $audio_id
  * @property Audio $audio
- * @property string $audio_file_analytics_url
- * @property string $audio_file_web_url
- * @property string $audio_file_opengraph_url
+ * @property string $audio_analytics_url
+ * @property string $audio_web_url
+ * @property string $audio_opengraph_url
  * @property string|null $description Holds text only description, striped of any markdown or html special characters
  * @property string $description_markdown
  * @property string $description_html
@@ -86,8 +86,6 @@ class Episode extends Entity
 
     protected ?Audio $audio = null;
 
-    protected string $audio_url;
-
     protected string $audio_analytics_url;
 
     protected string $audio_web_url;
@@ -328,24 +326,17 @@ class Episode extends Entity
         return $this->chapters;
     }
 
-    public function getAudioFileUrl(): string
-    {
-        helper('media');
-
-        return media_base_url($this->audio->file_path);
-    }
-
-    public function getAudioFileAnalyticsUrl(): string
+    public function getAudioAnalyticsUrl(): string
     {
         helper('analytics');
 
         // remove 'podcasts/' from audio file path
-        $strippedAudioFilePath = substr($this->getAudio()->file_path, 9);
+        $strippedAudioPath = substr($this->getAudio()->file_path, 9);
 
         return generate_episode_analytics_url(
             $this->podcast_id,
             $this->id,
-            $strippedAudioFilePath,
+            $strippedAudioPath,
             $this->audio->duration,
             $this->audio->file_size,
             $this->audio->header_size,
@@ -353,14 +344,14 @@ class Episode extends Entity
         );
     }
 
-    public function getAudioFileWebUrl(): string
+    public function getAudioWebUrl(): string
     {
-        return $this->getAudioFileAnalyticsUrl() . '?_from=-+Website+-';
+        return $this->getAudioAnalyticsUrl() . '?_from=-+Website+-';
     }
 
-    public function getAudioFileOpengraphUrl(): string
+    public function getAudioOpengraphUrl(): string
     {
-        return $this->getAudioFileAnalyticsUrl() . '?_from=-+Open+Graph+-';
+        return $this->getAudioAnalyticsUrl() . '?_from=-+Open+Graph+-';
     }
 
     /**
diff --git a/app/Helpers/id3_helper.php b/app/Helpers/id3_helper.php
index be6ae69fd6..8b36879ff3 100644
--- a/app/Helpers/id3_helper.php
+++ b/app/Helpers/id3_helper.php
@@ -9,7 +9,6 @@ declare(strict_types=1);
  */
 
 use App\Entities\Episode;
-use CodeIgniter\Files\File;
 use JamesHeinrich\GetID3\WriteTags;
 
 if (! function_exists('write_audio_file_tags')) {
diff --git a/app/Helpers/rss_helper.php b/app/Helpers/rss_helper.php
index 1a85bb7fb6..d26cf756bd 100644
--- a/app/Helpers/rss_helper.php
+++ b/app/Helpers/rss_helper.php
@@ -202,7 +202,7 @@ if (! function_exists('get_rss_feed')) {
 
             $enclosure->addAttribute(
                 'url',
-                $episode->audio_file_analytics_url .
+                $episode->audio_analytics_url .
                     ($serviceSlug === ''
                         ? ''
                         : '?_from=' . urlencode($serviceSlug)),
diff --git a/app/Helpers/seo_helper.php b/app/Helpers/seo_helper.php
index ea6f4993f4..33e4c2ffc8 100644
--- a/app/Helpers/seo_helper.php
+++ b/app/Helpers/seo_helper.php
@@ -104,7 +104,7 @@ if (! function_exists('get_episode_metatags')) {
             ->og('image:width', (string) config('Images')->podcastCoverSizes['large']['width'])
             ->og('image:height', (string) config('Images')->podcastCoverSizes['large']['height'])
             ->og('locale', $episode->podcast->language_code)
-            ->og('audio', $episode->audio_file_opengraph_url)
+            ->og('audio', $episode->audio_opengraph_url)
             ->og('audio:type', $episode->audio->file_mimetype)
             ->meta('article:published_time', $episode->published_at->format(DATE_ISO8601))
             ->meta('article:modified_time', $episode->updated_at->format(DATE_ISO8601))
diff --git a/modules/Analytics/Config/Analytics.php b/modules/Analytics/Config/Analytics.php
index b4ffd7952e..578d826294 100644
--- a/modules/Analytics/Config/Analytics.php
+++ b/modules/Analytics/Config/Analytics.php
@@ -28,12 +28,12 @@ class Analytics extends BaseConfig
     /**
      * get the full audio file url
      *
-     * @param string|string[] $audioFilePath
+     * @param string|string[] $audioPath
      */
-    public function getAudioFileUrl(string | array $audioFilePath): string
+    public function getAudioUrl(string | array $audioPath): string
     {
         helper('media');
 
-        return media_base_url($audioFilePath);
+        return media_base_url($audioPath);
     }
 }
diff --git a/modules/Analytics/Controllers/EpisodeAnalyticsController.php b/modules/Analytics/Controllers/EpisodeAnalyticsController.php
index 167c5d84e3..3a97bd8296 100644
--- a/modules/Analytics/Controllers/EpisodeAnalyticsController.php
+++ b/modules/Analytics/Controllers/EpisodeAnalyticsController.php
@@ -48,7 +48,7 @@ class EpisodeAnalyticsController extends Controller
         $this->config = config('Analytics');
     }
 
-    public function hit(string $base64EpisodeData, string ...$audioFilePath): RedirectResponse
+    public function hit(string $base64EpisodeData, string ...$audioPath): RedirectResponse
     {
         $session = Services::session();
         $session->start();
@@ -77,6 +77,6 @@ class EpisodeAnalyticsController extends Controller
             $serviceName,
         );
 
-        return redirect()->to($this->config->getAudioFileUrl(['podcasts', ...$audioFilePath]));
+        return redirect()->to($this->config->getAudioUrl(['podcasts', ...$audioPath]));
     }
 }
diff --git a/modules/Analytics/Helpers/analytics_helper.php b/modules/Analytics/Helpers/analytics_helper.php
index 026e6554de..d6433d8727 100644
--- a/modules/Analytics/Helpers/analytics_helper.php
+++ b/modules/Analytics/Helpers/analytics_helper.php
@@ -45,8 +45,8 @@ if (! function_exists('generate_episode_analytics_url')) {
     function generate_episode_analytics_url(
         int $podcastId,
         int $episodeId,
-        string $audioFilePath,
-        float $audioFileDuration,
+        string $audioPath,
+        float $audioDuration,
         int $audioFileSize,
         int $audioFileHeaderSize,
         \CodeIgniter\I18n\Time $publicationDate
@@ -61,16 +61,16 @@ if (! function_exists('generate_episode_analytics_url')) {
                     // bytes_threshold: number of bytes that must be downloaded for an episode to be counted in download analytics
                     // - if audio is less than or equal to 60s, then take the audio file_size
                     // - if audio is more than 60s, then take the audio file_header_size + 60s
-                    $audioFileDuration <= 60
+                    $audioDuration <= 60
                         ? $audioFileSize
                         : $audioFileHeaderSize +
-                            floor((($audioFileSize - $audioFileHeaderSize) / $audioFileDuration) * 60),
+                            floor((($audioFileSize - $audioFileHeaderSize) / $audioDuration) * 60),
                     $audioFileSize,
-                    $audioFileDuration,
+                    $audioDuration,
                     $publicationDate->getTimestamp(),
                 ),
             ),
-            $audioFilePath,
+            $audioPath,
         );
     }
 }
diff --git a/themes/cp_app/embed.php b/themes/cp_app/embed.php
index fcc375d2e2..7a3e38363d 100644
--- a/themes/cp_app/embed.php
+++ b/themes/cp_app/embed.php
@@ -41,7 +41,7 @@
                 style="--vm-player-box-shadow:0; --vm-player-theme: hsl(var(--color-accent-base)); --vm-control-focus-color: hsl(var(--color-accent-contrast)); --vm-control-spacing: 4px; --vm-menu-item-focus-bg: hsl(var(--color-background-highlight)); --vm-control-icon-size: 24px; <?= str_ends_with($theme, 'transparent') ? '--vm-controls-bg: transparent;' : '' ?>"
             >
             <vm-audio preload="none">
-                <?php $source = logged_in() ? $episode->audio->file_url : $episode->audio_file_analytics_url .
+                <?php $source = logged_in() ? $episode->audio->file_url : $episode->audio_analytics_url .
                     (isset($_SERVER['HTTP_REFERER'])
                         ? '?_from=' .
                             parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST)
diff --git a/themes/cp_app/episode/_layout.php b/themes/cp_app/episode/_layout.php
index 631082e61e..0f81cd593c 100644
--- a/themes/cp_app/episode/_layout.php
+++ b/themes/cp_app/episode/_layout.php
@@ -114,7 +114,7 @@
                 imageSrc="<?= $episode->cover->thumbnail_url ?>"
                 title="<?= $episode->title ?>"
                 podcast="<?= $episode->podcast->title ?>"
-                src="<?= $episode->audio_file_web_url ?>"
+                src="<?= $episode->audio_web_url ?>"
                 mediaType="<?= $episode->audio->file_mimetype ?>"
                 playLabel="<?= lang('Common.play_episode_button.play') ?>"
                 playingLabel="<?= lang('Common.play_episode_button.playing') ?>"></play-episode-button>
diff --git a/themes/cp_app/episode/_partials/card.php b/themes/cp_app/episode/_partials/card.php
index 3b29f8ec6f..f024d232a0 100644
--- a/themes/cp_app/episode/_partials/card.php
+++ b/themes/cp_app/episode/_partials/card.php
@@ -19,7 +19,7 @@
             imageSrc="<?= $episode->cover->thumbnail_url ?>"
             title="<?= $episode->title ?>"
             podcast="<?= $episode->podcast->title ?>"
-            src="<?= $episode->audio_file_web_url ?>"
+            src="<?= $episode->audio_web_url ?>"
             mediaType="<?= $episode->audio->file_mimetype ?>"
             playLabel="<?= lang('Common.play_episode_button.play') ?>"
             playingLabel="<?= lang('Common.play_episode_button.playing') ?>"></play-episode-button>
diff --git a/themes/cp_app/episode/_partials/preview_card.php b/themes/cp_app/episode/_partials/preview_card.php
index 9e69012d9d..bba7ab1b8e 100644
--- a/themes/cp_app/episode/_partials/preview_card.php
+++ b/themes/cp_app/episode/_partials/preview_card.php
@@ -20,7 +20,7 @@
         imageSrc="<?= $episode->cover->thumbnail_url ?>"
         title="<?= $episode->title ?>"
         podcast="<?= $episode->podcast->title ?>"
-        src="<?= $episode->audio_file_web_url ?>"
+        src="<?= $episode->audio_web_url ?>"
         mediaType="<?= $episode->audio->file_mimetype ?>"
         playLabel="<?= lang('Common.play_episode_button.play') ?>"
         playingLabel="<?= lang('Common.play_episode_button.playing') ?>"></play-episode-button>
-- 
GitLab