| Conditions | 10 |
| Paths | 11 |
| Total Lines | 63 |
| Code Lines | 37 |
| 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 |
||
| 61 | public function getHelperProperties(MediaInterface $media, $format, $options = []) |
||
| 62 | { |
||
| 63 | if (MediaProviderInterface::FORMAT_REFERENCE === $format) { |
||
| 64 | $box = $media->getBox(); |
||
| 65 | } else { |
||
| 66 | $resizerFormat = $this->getFormat($format); |
||
| 67 | if (false === $resizerFormat) { |
||
| 68 | throw new \RuntimeException(sprintf('The image format "%s" is not defined. |
||
| 69 | Is the format registered in your ``sonata_media`` configuration?', $format)); |
||
| 70 | } |
||
| 71 | |||
| 72 | $box = $this->resizer->getBox($media, $resizerFormat); |
||
| 73 | } |
||
| 74 | |||
| 75 | $mediaWidth = $box->getWidth(); |
||
| 76 | |||
| 77 | $params = [ |
||
| 78 | 'alt' => $media->getName(), |
||
| 79 | 'title' => $media->getName(), |
||
| 80 | 'src' => $this->generatePublicUrl($media, $format), |
||
| 81 | 'width' => $mediaWidth, |
||
| 82 | 'height' => $box->getHeight(), |
||
| 83 | ]; |
||
| 84 | |||
| 85 | if (MediaProviderInterface::FORMAT_ADMIN !== $format) { |
||
| 86 | $srcSetFormats = $this->getFormats(); |
||
| 87 | |||
| 88 | if (isset($options['srcset']) && is_array($options['srcset'])) { |
||
| 89 | $srcSetFormats = []; |
||
| 90 | foreach ($options['srcset'] as $srcSetFormat) { |
||
| 91 | $formatName = $this->getFormatName($media, $srcSetFormat); |
||
| 92 | $srcSetFormats[$formatName] = $this->getFormat($formatName); |
||
| 93 | } |
||
| 94 | unset($options['srcset']); |
||
| 95 | } |
||
| 96 | |||
| 97 | if (!isset($options['srcset'])) { |
||
| 98 | $srcSet = []; |
||
| 99 | |||
| 100 | foreach ($srcSetFormats as $providerFormat => $settings) { |
||
| 101 | // Check if format belongs to the current media's context |
||
| 102 | if (0 === strpos($providerFormat, $media->getContext())) { |
||
| 103 | $width = $this->resizer->getBox($media, $settings)->getWidth(); |
||
| 104 | |||
| 105 | $srcSet[] = sprintf('%s %dw', $this->generatePublicUrl($media, $providerFormat), $width); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | // The reference format is not in the formats list |
||
| 110 | $srcSet[] = sprintf( |
||
| 111 | '%s %dw', |
||
| 112 | $this->generatePublicUrl($media, MediaProviderInterface::FORMAT_REFERENCE), |
||
| 113 | $media->getBox()->getWidth() |
||
| 114 | ); |
||
| 115 | |||
| 116 | $params['srcset'] = implode(', ', $srcSet); |
||
| 117 | } |
||
| 118 | |||
| 119 | $params['sizes'] = sprintf('(max-width: %1$dpx) 100vw, %1$dpx', $mediaWidth); |
||
| 120 | } |
||
| 121 | |||
| 122 | return array_merge($params, $options); |
||
| 123 | } |
||
| 124 | |||
| 225 |