| Conditions | 11 |
| Paths | 15 |
| Total Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 63 | public function getHelperProperties(MediaInterface $media, $format, $options = []) |
||
| 64 | { |
||
| 65 | if (MediaProviderInterface::FORMAT_REFERENCE === $format) { |
||
| 66 | $box = $media->getBox(); |
||
| 67 | } else { |
||
| 68 | $resizerFormat = $this->getFormat($format); |
||
| 69 | if (false === $resizerFormat) { |
||
| 70 | throw new \RuntimeException(sprintf('The image format "%s" is not defined. |
||
| 71 | Is the format registered in your ``sonata_media`` configuration?', $format)); |
||
| 72 | } |
||
| 73 | |||
| 74 | $box = $this->resizer->getBox($media, $resizerFormat); |
||
| 75 | } |
||
| 76 | |||
| 77 | $mediaWidth = $box->getWidth(); |
||
| 78 | |||
| 79 | $params = [ |
||
| 80 | 'alt' => $media->getName(), |
||
| 81 | 'title' => $media->getName(), |
||
| 82 | 'src' => $this->generatePublicUrl($media, $format), |
||
| 83 | 'width' => $mediaWidth, |
||
| 84 | 'height' => $box->getHeight(), |
||
| 85 | ]; |
||
| 86 | |||
| 87 | if (MediaProviderInterface::FORMAT_ADMIN !== $format) { |
||
| 88 | $srcSetFormats = $this->getFormats(); |
||
| 89 | |||
| 90 | if (isset($options['srcset']) && is_array($options['srcset'])) { |
||
| 91 | $srcSetFormats = []; |
||
| 92 | foreach ($options['srcset'] as $srcSetFormat) { |
||
| 93 | $formatName = $this->getFormatName($media, $srcSetFormat); |
||
| 94 | $srcSetFormats[$formatName] = $this->getFormat($formatName); |
||
| 95 | } |
||
| 96 | unset($options['srcset']); |
||
| 97 | |||
| 98 | // Make sure the requested format is also in the srcSetFormats |
||
| 99 | if (!isset($srcSetFormats[$format])) { |
||
| 100 | $srcSetFormats[$format] = $this->getFormat($format); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | if (!isset($options['srcset'])) { |
||
| 105 | $srcSet = []; |
||
| 106 | |||
| 107 | foreach ($srcSetFormats as $providerFormat => $settings) { |
||
| 108 | // Check if format belongs to the current media's context |
||
| 109 | if (0 === strpos($providerFormat, $media->getContext())) { |
||
| 110 | $width = $this->resizer->getBox($media, $settings)->getWidth(); |
||
| 111 | |||
| 112 | $srcSet[] = sprintf('%s %dw', $this->generatePublicUrl($media, $providerFormat), $width); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | // The reference format is not in the formats list |
||
| 117 | $srcSet[] = sprintf( |
||
| 118 | '%s %dw', |
||
| 119 | $this->generatePublicUrl($media, MediaProviderInterface::FORMAT_REFERENCE), |
||
| 120 | $media->getBox()->getWidth() |
||
| 121 | ); |
||
| 122 | |||
| 123 | $params['srcset'] = implode(', ', $srcSet); |
||
| 124 | } |
||
| 125 | |||
| 126 | $params['sizes'] = sprintf('(max-width: %1$dpx) 100vw, %1$dpx', $mediaWidth); |
||
| 127 | } |
||
| 128 | |||
| 129 | return array_merge($params, $options); |
||
| 130 | } |
||
| 131 | |||
| 226 |