Passed
Push — master ( 3c2872...0ffb5b )
by Aimeos
03:13
created

Standard::rescale()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 3
b 0
f 0
nc 3
nop 2
dl 0
loc 27
rs 9.9666
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2020
6
 * @package Controller
7
 * @subpackage Jobs
8
 */
9
10
11
namespace Aimeos\Controller\Jobs\Media\Scale;
12
13
14
/**
15
 * Image rescale job controller.
16
 *
17
 * @package Controller
18
 * @subpackage Jobs
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Jobs\Base
22
	implements \Aimeos\Controller\Jobs\Iface
23
{
24
	/**
25
	 * Returns the localized name of the job.
26
	 *
27
	 * @return string Name of the job
28
	 */
29
	public function getName() : string
30
	{
31
		return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Rescale product images' );
32
	}
33
34
35
	/**
36
	 * Returns the localized description of the job.
37
	 *
38
	 * @return string Description of the job
39
	 */
40
	public function getDescription() : string
41
	{
42
		return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Rescales product images to the new sizes' );
43
	}
44
45
46
	/**
47
	 * Executes the job.
48
	 *
49
	 * @throws \Aimeos\Controller\Jobs\Exception If an error occurs
50
	 */
51
	public function run()
52
	{
53
		$context = $this->getContext();
54
		$process = $context->getProcess();
55
		$manager = \Aimeos\MShop::create( $context, 'media' );
56
57
		$search = $manager->filter();
58
		$expr = array(
59
			$search->compare( '==', 'media.domain', ['attribute', 'catalog', 'product', 'service', 'supplier'] ),
0 ignored issues
show
Bug introduced by
The method compare() does not exist on Aimeos\MW\Criteria\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MW\Criteria\Base. Are you sure you never get one of those? ( Ignorable by Annotation )

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

59
			$search->/** @scrutinizer ignore-call */ 
60
            compare( '==', 'media.domain', ['attribute', 'catalog', 'product', 'service', 'supplier'] ),
Loading history...
60
			$search->compare( '=~', 'media.mimetype', 'image/' ),
61
		);
62
		$search->setConditions( $search->combine( '&&', $expr ) );
0 ignored issues
show
Bug introduced by
The method combine() does not exist on Aimeos\MW\Criteria\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MW\Criteria\Base. Are you sure you never get one of those? ( Ignorable by Annotation )

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

62
		$search->setConditions( $search->/** @scrutinizer ignore-call */ combine( '&&', $expr ) );
Loading history...
Bug introduced by
The method setConditions() does not exist on Aimeos\MW\Criteria\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MW\Criteria\Base. Are you sure you never get one of those? ( Ignorable by Annotation )

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

62
		$search->/** @scrutinizer ignore-call */ 
63
           setConditions( $search->combine( '&&', $expr ) );
Loading history...
63
		$search->setSortations( array( $search->sort( '+', 'media.id' ) ) );
0 ignored issues
show
Bug introduced by
The method sort() does not exist on Aimeos\MW\Criteria\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MW\Criteria\Base. Are you sure you never get one of those? ( Ignorable by Annotation )

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

63
		$search->setSortations( array( $search->/** @scrutinizer ignore-call */ sort( '+', 'media.id' ) ) );
Loading history...
Bug introduced by
The method setSortations() does not exist on Aimeos\MW\Criteria\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MW\Criteria\Base. Are you sure you never get one of those? ( Ignorable by Annotation )

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

63
		$search->/** @scrutinizer ignore-call */ 
64
           setSortations( array( $search->sort( '+', 'media.id' ) ) );
Loading history...
64
65
		$start = 0;
66
67
		$fcn = function( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\Map $items ) {
68
			$this->rescale( $context, $items );
69
		};
70
71
		do
72
		{
73
			$search->slice( $start );
0 ignored issues
show
Bug introduced by
The call to Aimeos\MW\Criteria\Iface::slice() has too few arguments starting with limit. ( Ignorable by Annotation )

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

73
			$search->/** @scrutinizer ignore-call */ 
74
            slice( $start );

This check compares calls to functions or methods with their respective definitions. If the call has less 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...
74
			$items = $manager->search( $search );
75
76
			$process->start( $fcn, [$context, $items] );
77
78
			$count = count( $items );
79
			$start += $count;
80
		}
81
		while( $count === $search->getLimit() );
82
83
		$process->wait();
84
	}
85
86
87
	/**
88
	 * Recreates the preview images for the given media items
89
	 *
90
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
91
	 * @param \Aimeos\Map $items List of media items implementing \Aimeos\MShop\Media\Item\Iface
92
	 */
93
	protected function rescale( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\Map $items )
94
	{
95
		$logger = $context->getLogger();
96
		$manager = \Aimeos\MShop::create( $context, 'media' );
97
		$cntl = \Aimeos\Controller\Common\Media\Factory::create( $context );
98
99
		/** controller/jobs/media/scale/force
100
		 * Enforce rescaling all images
101
		 *
102
		 * By default, all images are rescaled when executing the job controller.
103
		 * You can limit scaling to new images only (if mtime of the file is newer
104
		 * than the mtime of the media record) by setting this configuration option
105
		 * to false or 0
106
		 *
107
		 * @param bool True to rescale all images, false for new ones only
108
		 * @category Developer
109
		 * @category User
110
		 * @since 2019.10
111
		 */
112
		$force = $context->getConfig()->get( 'controller/jobs/media/scale/force', true );
113
114
		foreach( $items as $item )
115
		{
116
			try {
117
				$manager->save( $cntl->scale( $item, 'fs-media', $force ) );
0 ignored issues
show
Unused Code introduced by
The call to Aimeos\Controller\Common\Media\Iface::scale() has too many arguments starting with $force. ( Ignorable by Annotation )

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

117
				$manager->save( $cntl->/** @scrutinizer ignore-call */ scale( $item, 'fs-media', $force ) );

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...
118
			} catch( \Exception $e ) {
119
				$logger->log( sprintf( 'Scaling media item "%1$s" failed: %2$s', $item->getId(), $e->getMessage() ) );
120
			}
121
		}
122
	}
123
}
124