From 9e8325b437b921b4415ab10728dc62133f6302c9 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Sat, 7 Oct 2017 17:20:22 +0100 Subject: [PATCH] Move duplicate logic into its own function --- app/Media.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/app/Media.php b/app/Media.php index f3bf2f7a..8e30109b 100644 --- a/app/Media.php +++ b/app/Media.php @@ -49,13 +49,7 @@ class Media extends Model */ public function getMediumurlAttribute() { - $filenameParts = explode('.', $this->path); - $extension = array_pop($filenameParts); - // the following acheives this data flow - // foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar - $basename = ltrim(array_reduce($filenameParts, function ($carry, $item) { - return $carry . '.' . $item; - }, ''), '.'); + $basename = $this->getBasename($this->path); return config('filesystems.disks.s3.url') . '/' . $basename . '-medium.' . $extension; } @@ -67,14 +61,19 @@ class Media extends Model */ public function getSmallurlAttribute() { - $filenameParts = explode('.', $this->path); + $basename = $this->getBasename($this->path); + + return config('filesystems.disks.s3.url') . '/' . $basename . '-small.' . $extension; + } + + public function getBasename($path) + { + $filenameParts = explode('.', $path); $extension = array_pop($filenameParts); - // the following acheives this data flow + // the following achieves this data flow // foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar $basename = ltrim(array_reduce($filenameParts, function ($carry, $item) { return $carry . '.' . $item; }, ''), '.'); - - return config('filesystems.disks.s3.url') . '/' . $basename . '-small.' . $extension; } }