Move duplicate logic into its own function

This commit is contained in:
Jonny Barnes 2017-10-07 17:20:22 +01:00
parent 55c4159c87
commit 9e8325b437

View file

@ -49,13 +49,7 @@ class Media extends Model
*/ */
public function getMediumurlAttribute() public function getMediumurlAttribute()
{ {
$filenameParts = explode('.', $this->path); $basename = $this->getBasename($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;
}, ''), '.');
return config('filesystems.disks.s3.url') . '/' . $basename . '-medium.' . $extension; return config('filesystems.disks.s3.url') . '/' . $basename . '-medium.' . $extension;
} }
@ -67,14 +61,19 @@ class Media extends Model
*/ */
public function getSmallurlAttribute() 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); $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 // foo.bar.png => ['foo', 'bar', 'png'] => ['foo', 'bar'] => foo.bar
$basename = ltrim(array_reduce($filenameParts, function ($carry, $item) { $basename = ltrim(array_reduce($filenameParts, function ($carry, $item) {
return $carry . '.' . $item; return $carry . '.' . $item;
}, ''), '.'); }, ''), '.');
return config('filesystems.disks.s3.url') . '/' . $basename . '-small.' . $extension;
} }
} }