I would suggest to put it in Config. It's not model's logic to store variables inside.
Where to put logic for getting image path and thumbnails?
I have a Image model, which contains uploaded images. Each image entity has an original image and a couple of thumbnails. Where is the logical place to put the paths and file names to these? In the model?
Now I have a helper class that provides these fields, but it's getting kind of big and all methods needs the Image class as an input parameter. So I am starting to wonder if maybe this information belongs in the model, but that doesn't feel quite right...
Examples of helper methods:
private static function _image(Image $image, $base)
{
return sprintf('%s/%s/%s',
Config::get('static.images.' . $base), //url or path
Config::get('static.images.original'),
$image->category->slug
);
}
private static function _thumb(Image $image, $base, $template)
{
$image_path = sprintf('%s/%s', $image->id % 100, ($image->id / 100) % 100);
return sprintf('%s/%s/%s/%s',
Config::get('static.images.' . $base), //url or path
Config::get('static.images.thumbnail'),
$template,
$image_path
);
}
As you can see some; some logic is required to get the paths. Any suggestions to how and where I might put these methods?
I would probably put these method inside a dedicated service, something like this :
interface ImagePathService {
public function getOriginalPath(Image $image);
public function getThumbnailPath(Image $image);
public function getOriginalUrl(Image $image);
public function getThumbnailUrl(Image $image);
}
This way you have the ability for example to switch between a local image folder to a cloud based, without touching your model class.
Please or to participate in this conversation.