Passed
Push — master ( 37bd96...8c19b2 )
by Aimeos
02:27
created

Standard::run()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 21
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 34
rs 9.584
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2018
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()
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()
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->createSearch();
58
		$expr = array(
59
			$search->compare( '==', 'media.domain', 'product' ),
60
			$search->compare( '=~', 'media.mimetype', 'image/' ),
61
		);
62
		$search->setConditions( $search->combine( '&&', $expr ) );
63
		$search->setSortations( array( $search->sort( '+', 'media.id' ) ) );
64
65
		$start = 0;
66
67
		$fcn = function( array $items ) {
68
			$this->rescale( $items );
69
		};
70
71
		do
72
		{
73
			$search->setSlice( $start );
74
			$items = $manager->searchItems( $search );
75
76
			$context->__sleep();
77
			$process->start( $fcn, [$items] );
78
79
			$count = count( $items );
80
			$start += $count;
81
		}
82
		while( $count === $search->getSliceSize() );
83
84
		$process->wait();
85
	}
86
87
88
	/**
89
	 * Recreates the preview images for the given media items
90
	 *
91
	 * @param \Aimeos\MShop\Media\Item\Iface[] $items List of media items
92
	 */
93
	protected function rescale( array $items )
94
	{
95
		$context = $this->getContext();
96
		$manager = \Aimeos\MShop::create( $context, 'media' );
97
		$cntl = \Aimeos\Controller\Common\Media\Factory::create( $context );
98
99
		$force = $context->getConfig()->get( 'controller/jobs/media/scale/standard/force', true );
100
		$fs = $context->getFileSystemManager()->get( 'fs-media' );
101
		$is = ( $fs instanceof MetaIface ? true : false );
0 ignored issues
show
Bug introduced by
The type Aimeos\Controller\Jobs\Media\Scale\MetaIface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
102
103
		foreach( $items as $item )
104
		{
105
			try
106
			{
107
				if( $is && date( 'Y-m-d H:i:s', $fs->time( $item->getUrl() ) ) > $item->getTimeModified() || $force ) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($is && date('Y-m-d H:i:...meModified()) || $force, Probably Intended Meaning: $is && (date('Y-m-d H:i:...meModified() || $force)
Loading history...
108
					$manager->saveItem( $cntl->scale( $item ) );
109
				}
110
			}
111
			catch( \Exception $e )
112
			{
113
				$msg = sprintf( 'Scaling media item "%1$s" failed: %2$s', $item->getId(), $e->getMessage() );
114
				$context->getLogger()->log( $msg );
115
			}
116
		}
117
	}
118
}
119