Passed
Push — master ( 0c9a7a...84baf7 )
by Aimeos
02:36
created

Standard::transform()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 5
eloc 25
c 4
b 0
f 0
nc 12
nop 3
dl 0
loc 37
rs 9.2088
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 '
50
				<video autoplay muted class="item" id="image-' . $media->getId() . '" loading="lazy"
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 . ' />
56
				</video>
57
			';
58
		}
59
60
		return '
61
			<div itemscope itemtype="http://schema.org/ImageObject" representativeOfPage="' . ( $main ? 'true' : 'false' ) . '">
62
				<img class="item" id="image-' . $media->getId() . '" loading="lazy" itemprop="contentUrl"
63
					thumbnail="' . $enc->attr( $view->content( $media->getPreview(), $media->getFileSystem() ) ) . '"
64
					src="' . $enc->attr( $view->content( $media->getPreview(), $media->getFileSystem() ) ) . '"
65
					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

65
					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...
66
					data-zoom="' . $enc->attr( $view->content( $media->getUrl(), $media->getFileSystem() ) ) . '"
67
					alt="' . $enc->attr( $media->getProperties( 'title' )->first( $media->getName() ) ) . '"
68
					sizes="' . $sizes . '" ' . $variant . ' />
69
			</div>
70
		';
71
	}
72
}
73