Passed
Push — master ( 2eb1ae...7132eb )
by Aimeos
02:45
created

Standard::transform()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 22
c 1
b 0
f 0
nc 16
nop 3
dl 0
loc 34
rs 8.9457
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2020-2022
6
 * @package MW
7
 * @subpackage View
8
 */
9
10
11
namespace Aimeos\Base\View\Helper\Image;
12
13
14
/**
15
 * View helper class for creating an HTML image tag
16
 *
17
 * @package MW
18
 * @subpackage View
19
 */
20
class Standard
21
	extends \Aimeos\Base\View\Helper\Base
22
	implements \Aimeos\Base\View\Helper\Image\Iface
23
{
24
	/**
25
	 * Returns the HTML image tag for the given media item
26
	 *
27
	 * @param \Aimeos\MShop\Media\Item\Iface $media Media item
28
	 * @param string $sizes Preferred image srcset sizes
29
	 * @param bool $main TRUE for main image, FALSE for secondary images
30
	 * @return string HTML image tag
31
	 */
32
	public function transform( \Aimeos\MShop\Media\Item\Iface $media, string $sizes = '', $main = true ) : string
33
	{
34
		$view = $this->view();
35
		$enc = $view->encoder();
36
37
		$sources = [];
38
		foreach( $media->getPreviews() as $type => $path ) {
39
			$sources[$type] = $view->content( $path, 'fs-media' );
40
		}
41
42
		$variant = '';
43
		foreach( $media->getRefItems( 'attribute', null, 'variant' ) as $id => $item ) {
44
			$variant .= ' data-variant-' . $item->getType() . '="' . $enc->attr( $id ) . '"';
45
		}
46
47
		if( !strncmp( $media->getMimetype(), 'video/', 6 ) )
48
		{
49
			return '<video autoplay muted class="item" id="image-' . $media->getId() . '" loading="lazy"
50
				itemscope itemtype="http://schema.org/VideoObject"
51
				thumbnail="' . $enc->attr( $view->content( $media->getPreview(), $media->getFileSystem() ) ) . '"
52
				poster="' . $enc->attr( $view->content( $media->getPreview( 600 ), $media->getFileSystem() ) ) . '"
53
				src="' . $enc->attr( $view->content( $media->getUrl(), $media->getFileSystem() ) ) . '"
54
				alt="' . $enc->attr( $media->getProperties( 'title' )->first( $media->getName() ) ) . '"
55
				' . $variant . ' representativeOfPage="' . ( $main ? 'true' : 'false' ) . '"></video>';
56
		}
57
58
		return '<img class="item" id="image-' . $media->getId() . '" loading="lazy"
59
			itemscope itemprop="image" itemtype="http://schema.org/ImageObject"
60
			thumbnail="' . $enc->attr( $view->content( $media->getPreview(), $media->getFileSystem() ) ) . '"
61
			src="' . $enc->attr( $view->content( $media->getPreview(), $media->getFileSystem() ) ) . '"
62
			srcset="' . $enc->attr( $view->imageset( $media->getPreviews( true ), $media->getFileSystem() ) ) . '"
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\MShop\Media\Item\Iface::getPreviews() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
			srcset="' . $enc->attr( $view->imageset( $media->/** @scrutinizer ignore-call */ getPreviews( true ), $media->getFileSystem() ) ) . '"

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
63
			data-zoom="' . $enc->attr( $view->content( $media->getUrl(), $media->getFileSystem() ) ) . '"
64
			alt="' . $enc->attr( $media->getProperties( 'title' )->first( $media->getName() ) ) . '"
65
			sizes="' . $sizes . '" ' . $variant . ' representativeOfPage="' . ( $main ? 'true' : 'false' ) . '" />';
66
	}
67
}
68