Complex classes like FileProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FileProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class FileProvider extends BaseProvider |
||
| 35 | { |
||
| 36 | protected $allowedExtensions; |
||
| 37 | |||
| 38 | protected $allowedMimeTypes; |
||
| 39 | |||
| 40 | protected $metadata; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param string $name |
||
| 44 | * @param Filesystem $filesystem |
||
| 45 | * @param CDNInterface $cdn |
||
| 46 | * @param GeneratorInterface $pathGenerator |
||
| 47 | * @param ThumbnailInterface $thumbnail |
||
| 48 | * @param array $allowedExtensions |
||
| 49 | * @param array $allowedMimeTypes |
||
| 50 | * @param MetadataBuilderInterface $metadata |
||
| 51 | */ |
||
| 52 | public function __construct($name, Filesystem $filesystem, CDNInterface $cdn, GeneratorInterface $pathGenerator, ThumbnailInterface $thumbnail, array $allowedExtensions = array(), array $allowedMimeTypes = array(), MetadataBuilderInterface $metadata = null) |
||
| 53 | { |
||
| 54 | parent::__construct($name, $filesystem, $cdn, $pathGenerator, $thumbnail); |
||
| 55 | |||
| 56 | $this->allowedExtensions = $allowedExtensions; |
||
| 57 | $this->allowedMimeTypes = $allowedMimeTypes; |
||
| 58 | $this->metadata = $metadata; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | */ |
||
| 64 | public function getProviderMetadata() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | public function getReferenceImage(MediaInterface $media) |
||
| 73 | { |
||
| 74 | return sprintf('%s/%s', |
||
| 75 | $this->generatePath($media), |
||
| 76 | $media->getProviderReference() |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * {@inheritdoc} |
||
| 82 | */ |
||
| 83 | public function getReferenceFile(MediaInterface $media) |
||
| 84 | { |
||
| 85 | return $this->getFilesystem()->get($this->getReferenceImage($media), true); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritdoc} |
||
| 90 | */ |
||
| 91 | public function buildEditForm(FormMapper $formMapper) |
||
| 92 | { |
||
| 93 | $formMapper->add('name'); |
||
| 94 | $formMapper->add('enabled', null, array('required' => false)); |
||
| 95 | $formMapper->add('authorName'); |
||
| 96 | $formMapper->add('cdnIsFlushable'); |
||
| 97 | $formMapper->add('description'); |
||
| 98 | $formMapper->add('copyright'); |
||
| 99 | $formMapper->add('binaryContent', 'file', array('required' => false)); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | public function buildCreateForm(FormMapper $formMapper) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | public function buildMediaType(FormBuilder $formBuilder) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | public function postPersist(MediaInterface $media) |
||
| 135 | { |
||
| 136 | if ($media->getBinaryContent() === null) { |
||
| 137 | return; |
||
| 138 | } |
||
| 139 | |||
| 140 | $this->setFileContents($media); |
||
| 141 | |||
| 142 | $this->generateThumbnails($media); |
||
| 143 | |||
| 144 | $media->resetBinaryContent(); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | public function postUpdate(MediaInterface $media) |
||
| 151 | { |
||
| 152 | if (!$media->getBinaryContent() instanceof \SplFileInfo) { |
||
| 153 | return; |
||
| 154 | } |
||
| 155 | |||
| 156 | // Delete the current file from the FS |
||
| 157 | $oldMedia = clone $media; |
||
| 158 | // if no previous reference is provided, it prevents |
||
| 159 | // Filesystem from trying to remove a directory |
||
| 160 | if ($media->getPreviousProviderReference() !== null) { |
||
| 161 | $oldMedia->setProviderReference($media->getPreviousProviderReference()); |
||
| 162 | |||
| 163 | $path = $this->getReferenceImage($oldMedia); |
||
| 164 | |||
| 165 | if ($this->getFilesystem()->has($path)) { |
||
| 166 | $this->getFilesystem()->delete($path); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | $this->fixBinaryContent($media); |
||
| 171 | |||
| 172 | $this->setFileContents($media); |
||
| 173 | |||
| 174 | $this->generateThumbnails($media); |
||
| 175 | |||
| 176 | $media->resetBinaryContent(); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param MediaInterface $media |
||
| 181 | */ |
||
| 182 | protected function fixBinaryContent(MediaInterface $media) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @throws \RuntimeException |
||
| 206 | * |
||
| 207 | * @param MediaInterface $media |
||
| 208 | */ |
||
| 209 | protected function fixFilename(MediaInterface $media) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | protected function doTransform(MediaInterface $media) |
||
| 229 | { |
||
| 230 | $this->fixBinaryContent($media); |
||
| 231 | $this->fixFilename($media); |
||
| 232 | |||
| 233 | // this is the name used to store the file |
||
| 234 | if (!$media->getProviderReference() || |
||
| 235 | $media->getProviderReference() === MediaInterface::MISSING_BINARY_REFERENCE |
||
| 236 | ) { |
||
| 237 | $media->setProviderReference($this->generateReferenceName($media)); |
||
| 238 | } |
||
| 239 | |||
| 240 | if ($media->getBinaryContent() instanceof File) { |
||
| 241 | $media->setContentType($media->getBinaryContent()->getMimeType()); |
||
| 242 | $media->setSize($media->getBinaryContent()->getSize()); |
||
| 243 | } |
||
| 244 | |||
| 245 | $media->setProviderStatus(MediaInterface::STATUS_OK); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritdoc} |
||
| 250 | */ |
||
| 251 | public function updateMetadata(MediaInterface $media, $force = true) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function generatePublicUrl(MediaInterface $media, $format) |
||
| 269 | { |
||
| 270 | if ($format == 'reference') { |
||
| 271 | $path = $this->getReferenceImage($media); |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | public function getHelperProperties(MediaInterface $media, $format, $options = array()) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | public function generatePrivateUrl(MediaInterface $media, $format) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Set the file contents for an image. |
||
| 306 | * |
||
| 307 | * @param MediaInterface $media |
||
| 308 | * @param string $contents path to contents, defaults to MediaInterface BinaryContent |
||
| 309 | */ |
||
| 310 | protected function setFileContents(MediaInterface $media, $contents = null) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param MediaInterface $media |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | protected function generateReferenceName(MediaInterface $media) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param MediaInterface $media |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | protected function generateMediaUniqId(MediaInterface $media) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * {@inheritdoc} |
||
| 352 | */ |
||
| 353 | public function getDownloadResponse(MediaInterface $media, $format, $mode, array $headers = array()) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * {@inheritdoc} |
||
| 391 | */ |
||
| 392 | public function validate(ErrorElement $errorElement, MediaInterface $media) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Set media binary content according to request content. |
||
| 423 | * |
||
| 424 | * @param MediaInterface $media |
||
| 425 | */ |
||
| 426 | protected function generateBinaryFromRequest(MediaInterface $media) |
||
| 464 | } |
||
| 465 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.