i'm developping a media manager. This manager can process various media types such as audio, video and image. The main functionnality is to resize medias and obviously the process is different for each media type.
Here is what i have for now :
- An entity
Mediawhich contains various data about it (filename, path...) - A utility class
MediaResizerwhich can process various types of media files.
Media entity :
class Media {
public $originalFile;
public $resizedFile;
public function resize($size) {
$resizer = new MediaResizer($this->originalFile);
$this->resizedFile = $resizer->resize($size)
}
}
The media resizer :
class MediaResizer {
public $file;
public function getFileType(){/**/} // returns audio, video or image
function resize($size) {
$type = $this->getFileType();
$method = 'resize' . ucfirst($type);
return $this->$method($size)
}
private function resizeVideo(){/**/}
private function resizeImage(){/**/}
private function resizeAudio(){/**/}
}
As you can see, the media does not know which method is really called to resize the file.
Here is what i want :
Everything is working fine with this code. However, my mediaResizer class is way too big so I decided to split it in 4 classes :
- an abstract media resizer which contains all the common code
- a VideoResizer, an ImageResizer and an AudioResizer, ann three extends the abstract class.
The problem i have now is how to keep the process of choosing the resizer class based on the media type in the resizer class ?
Here is what I thought but i don't like the idea of a class relying on his children :
class Media{
function resize($size){
$resizerClass = MediaResizer::getResizerClass($filetype); // returns ImageResizer, VideoResizer or AudioResizer
$resizer = new $resizerClass($file);
$resizer->resize($size);
}
}
Is it a good idea to use something like this in the entity or is there a better way to do the same thing ?
Aucun commentaire:
Enregistrer un commentaire