AimeosCommandController::setOptions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://www.gnu.org/copyleft/lgpl.html
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package flow
7
 * @subpackage Command
8
 */
9
10
11
namespace Aimeos\Shop\Command;
12
13
use Neos\Flow\Annotations as Flow;
14
15
16
/**
17
 * Aimeos CLI controller for cronjobs
18
 *
19
 * @package flow
20
 * @subpackage Command
21
 * @Flow\Scope("singleton")
22
 */
23
class AimeosCommandController extends \Neos\Flow\Cli\CommandController
24
{
25
	/**
26
	 * @var string
27
	 * @Flow\InjectConfiguration(path="http.baseUri", package="Neos.Flow")
28
	 */
29
	protected $baseUri;
30
31
	/**
32
	 * @var \Neos\Cache\Frontend\StringFrontend
33
	 */
34
	protected $cache;
35
36
	/**
37
	 * @var \Neos\Flow\ObjectManagement\ObjectManagerInterface
38
	 */
39
	protected $objectManager;
40
41
42
	/**
43
	 * Clears the content cache
44
	 *
45
	 * @param string $sites List of sites separated by a space character the jobs should be executed for, e.g. "default unittest"
46
	 * @return void
47
	 */
48
	public function cacheCommand( $sites = '' )
49
	{
50
		$context = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\Context' )->get( null, 'command' );
51
		$context->setEditor( 'aimeos:cache' );
52
53
		$config = $context->getConfig();
54
		$name = $config->get( 'flow/cache/name', 'Flow' );
55
56
		$localeManager = \Aimeos\MShop::create( $context, 'locale' );
57
58
		foreach( $this->getSiteItems( $context, $sites ) as $siteItem )
59
		{
60
			$localeItem = $localeManager->bootstrap( $siteItem->getCode(), '', '', false );
0 ignored issues
show
Bug introduced by
The method bootstrap() does not exist on Aimeos\MShop\Common\Manager\Iface. It seems like you code against a sub-type of Aimeos\MShop\Common\Manager\Iface such as Aimeos\MShop\Locale\Manager\Iface or Aimeos\MShop\Common\Manager\Decorator\Base or Aimeos\MShop\Service\Manager\Lists\Type\Standard or Aimeos\MShop\Price\Manager\Standard or Aimeos\MShop\Attribute\Manager\Type\Standard or Aimeos\MShop\Price\Manager\Lists\Type\Standard or Aimeos\MShop\Media\Manager\Type\Standard or Aimeos\MShop\Coupon\Manager\Code\Standard or Aimeos\MShop\Order\Manager\Base\Coupon\Standard or Aimeos\MShop\Index\Manager\DBBase or Aimeos\MShop\Product\Manager\Standard or Aimeos\MShop\Index\Manager\Standard or Aimeos\MShop\Index\Manager\Attribute\Standard or Aimeos\MShop\Index\Manager\Text\Standard or Aimeos\MShop\Index\Manager\Supplier\Standard or Aimeos\MShop\Index\Manager\Catalog\Standard or Aimeos\MShop\Index\Manager\Price\Standard or Aimeos\MShop\Supplier\Manager\Standard or Aimeos\MShop\Price\Manager\Property\Type\Standard or Aimeos\MShop\Customer\Manager\Property\Standard or Aimeos\MShop\Order\Manager\Base\Service\Standard or Aimeos\MShop\Order\Manager\Base\Standard or Aimeos\MShop\Price\Manager\Lists\Standard or Aimeos\MShop\Supplier\Manager\Lists\Type\Standard or Aimeos\MShop\Order\Manag...vice\Attribute\Standard or Aimeos\MShop\Service\Manager\Lists\Standard or Aimeos\MShop\Tag\Manager\Type\Standard or Aimeos\MShop\Text\Manager\Lists\Standard or Aimeos\MShop\Price\Manager\Type\Standard or Aimeos\MShop\Locale\Manager\Currency\Standard or Aimeos\MShop\Order\Manag...duct\Attribute\Standard or Aimeos\MShop\Media\Manager\Lists\Type\Standard or Aimeos\MShop\Catalog\Manager\Lists\Standard or Aimeos\MShop\Tag\Manager\Standard or Aimeos\MShop\Coupon\Manager\Standard or Aimeos\MShop\Attribute\Manager\Standard or Aimeos\MShop\Attribute\M...\Property\Type\Standard or Aimeos\MShop\Service\Manager\Type\Standard or Aimeos\MShop\Product\Manager\Lists\Standard or Aimeos\MShop\Customer\Ma...\Property\Type\Standard or Aimeos\MShop\Order\Manager\Standard or Aimeos\MShop\Customer\Manager\Standard or Aimeos\MShop\Media\Manager\Standard or Aimeos\MShop\Customer\Manager\Lists\Type\Standard or Aimeos\MShop\Attribute\Manager\Lists\Standard or Aimeos\MShop\Product\Man...\Property\Type\Standard or Aimeos\MShop\Media\Manager\Lists\Standard or Aimeos\MShop\Plugin\Manager\Standard or Aimeos\MShop\Order\Manager\Base\Address\Standard or Aimeos\MShop\Catalog\Manager\Standard or Aimeos\MShop\Locale\Manager\Site\Standard or Aimeos\MShop\Product\Manager\Type\Standard or Aimeos\MShop\Supplier\Manager\Lists\Standard or Aimeos\MShop\Stock\Manager\Type\Standard or Aimeos\MShop\Text\Manager\Standard or Aimeos\MAdmin\Job\Manager\Standard or Aimeos\MShop\Customer\Manager\Group\Standard or Aimeos\MShop\Product\Manager\Lists\Type\Standard or Aimeos\MShop\Text\Manager\Lists\Type\Standard or Aimeos\MShop\Text\Manager\Type\Standard or Aimeos\MShop\Order\Manager\Status\Standard or Aimeos\MShop\Supplier\Manager\Address\Standard or Aimeos\MShop\Customer\Manager\Address\Standard or Aimeos\MShop\Plugin\Manager\Type\Standard or Aimeos\MShop\Stock\Manager\Standard or Aimeos\MShop\Stock\Manager\Nolimit or Aimeos\MShop\Attribute\Manager\Property\Standard or Aimeos\MShop\Subscription\Manager\Standard or Aimeos\MShop\Media\Manager\Property\Type\Standard or Aimeos\MShop\Product\Manager\Property\Standard or Aimeos\MShop\Locale\Manager\Language\Standard or Aimeos\MShop\Media\Manager\Property\Standard or Aimeos\MShop\Service\Manager\Standard or Aimeos\MShop\Attribute\Manager\Lists\Type\Standard or Aimeos\MAdmin\Log\Manager\Standard or Aimeos\MAdmin\Cache\Manager\Standard or Aimeos\MAdmin\Cache\Manager\None or Aimeos\MShop\Order\Manager\Base\Product\Standard or Aimeos\MShop\Price\Manager\Property\Standard or Aimeos\MShop\Customer\Manager\Lists\Standard or Aimeos\MShop\Catalog\Manager\Lists\Type\Standard. ( Ignorable by Annotation )

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

60
			/** @scrutinizer ignore-call */ 
61
   $localeItem = $localeManager->bootstrap( $siteItem->getCode(), '', '', false );
Loading history...
61
62
			$lcontext = clone $context;
63
			$lcontext->setLocale( $localeItem );
64
65
			switch( $name )
66
			{
67
				case 'None':
68
					$config->set( 'client/html/basket/cache/enable', false );
69
					$cache = \Aimeos\MW\Cache\Factory::create( 'None', array(), null );
70
					break;
71
				case 'Flow':
72
					$cache = new \Aimeos\MAdmin\Cache\Proxy\Flow( $lcontext, $this->cache );
73
					break;
74
				default:
75
					$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $lcontext );
76
					break;
77
			}
78
79
			$this->outputFormatted( 'Clearing the Aimeos cache for site <b>%s</b>', array( $siteItem->getCode() ) );
80
81
			$cache->clear();
82
		}
83
	}
84
85
86
	/**
87
	 * Executes the Aimeos maintenance jobs
88
	 *
89
	 * The Aimeos shop system needs some maintenance tasks that must be
90
	 * regularly executed. These include
91
	 *
92
	 * - admin/cache (remove expired cache entries once a day)
93
	 * - admin/log (archivate and delete old log entries once a day)
94
	 * - catalog/import/csv (import categories from CSV files)
95
	 * - customer/email/account (create new customer accounts and send e-mails)
96
	 * - customer/email/watch (send customers e-mails if their watched products have changed)
97
	 * - index/rebuild (rebuild the catalog index once a day after midnight)
98
	 * - index/optimize (optimize the catalog index once a day one hour after the rebuild)
99
	 * - media/scale (rescales the product images to the new sizes)
100
	 * - order/cleanup/unfinished (remove unfinised orders once a day)
101
	 * - order/cleanup/unfinised (remove unpaid orders once a day)
102
	 * - order/email/delivery (send delivery status update e-mails to the customers every few hours)
103
	 * - order/email/payment (send payment status update e-mails to the customers every few hours)
104
	 * - order/export/csv (export orders in admin interface)
105
	 * - order/service/async (import batch delivery or payment status updates if necessary)
106
	 * - order/service/delivery (sends paid orders to the ERP system or logistic partner)
107
	 * - order/service/payment (captures authorized payments after the configured amount of time automatically)
108
	 * - product/bought (updates the suggested products based on what other customers bought once a day)
109
	 * - product/export (export products)
110
	 * - product/export/sitemap (generate product sitemaps for search engines)
111
	 * - product/import/csv (import products from CSV files)
112
	 * - subscription/export/csv (export subscriptions in admin interface)
113
	 * - subscription/process/begin (start subscription period and add permissions if applicable)
114
	 * - subscription/process/renew (renew subscriptions on next date)
115
	 * - subscription/process/end (finish subscription period and revoke permissions if applicable)
116
	 *
117
	 * Each of these maintenance tasks must be executed for all shop instances
118
	 * if you have more than one site in your installation. The sites parameter
119
	 * should contain a list of site codes in this case. If you only have one
120
	 * site named "default" then you don't need to specify the site.
121
	 *
122
	 * @param string $jobs List of job names separated by a space character like "admin/job catalog/index/rebuild"
123
	 * @param string $sites List of sites separated by a space character the jobs should be executed for, e.g. "default unittest"
124
	 * @return void
125
	 */
126
	public function jobsCommand( $jobs, $sites = 'default' )
127
	{
128
		$aimeos = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\Aimeos' )->get();
129
		$context = $this->getContext();
130
		$process = $context->getProcess();
131
132
		$jobs = explode( ' ', $jobs );
133
		$localeManager = \Aimeos\MShop::create( $context, 'locale' );
134
135
		foreach( $this->getSiteItems( $context, $sites ) as $siteItem )
136
		{
137
			$localeItem = $localeManager->bootstrap( $siteItem->getCode(), '', '', false );
138
			$localeItem->setLanguageId( null );
139
			$localeItem->setCurrencyId( null );
140
141
			$context->setLocale( $localeItem );
142
143
			$this->outputFormatted( 'Executing jobs for site <b>%s</b>', array( $siteItem->getCode() ) );
144
145
			foreach( $jobs as $jobname )
146
			{
147
				$fcn = function( $context, $aimeos, $jobname ) {
148
					\Aimeos\Controller\Jobs::create( $context, $aimeos, $jobname )->run();
149
				};
150
151
				$process->start( $fcn, [$context, $aimeos, $jobname], true );
152
			}
153
		}
154
155
		$process->wait();
156
	}
157
158
159
	/**
160
	 * Initialize or update the Aimeos database tables
161
	 *
162
	 * After installing and updating the Aimeos package, the database structure
163
	 * must be created or upgraded to the current version. Depending on the size
164
	 * of the database, this may take a while.
165
	 *
166
	 * @param string $site Site for updating database entries
167
	 * @param string $tplsite Template site for creating or updating database entries
168
	 * @param array $option Optional setup configuration, name and value are separated by ":" like "setup/default/demo:1".
169
	 * @param string|null $task Setup task name to execute
170
	 * @param string $action Setup task action, e.g. "migrate", "rollback" or "clean"
171
	 * @return void
172
	 */
173
	public function setupCommand( $site = 'default', $tplsite = 'default', array $option = array(), $task = null, $action = 'migrate' )
174
	{
175
		$context = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\Context' )->get( null, 'command' );
176
		$context->setEditor( 'aimeos:setup' );
177
178
		$config = $context->getConfig();
179
		$config->set( 'setup/site', $site );
180
		$dbconfig = $this->getDbConfig( $config );
181
		$this->setOptions( $config, $option );
182
183
		\Aimeos\MShop::cache( false );
184
		\Aimeos\MAdmin::cache( false );
185
186
		$taskPaths = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\Aimeos' )->get()->getSetupPaths( $tplsite );
187
		$manager = new \Aimeos\MW\Setup\Manager\Multiple( $context->getDatabaseManager(), $dbconfig, $taskPaths, $context );
188
189
		$this->outputFormatted( 'Initializing or updating the Aimeos database tables for site <b>%s</b>', array( $site ) );
190
191
		switch( $action )
192
		{
193
			case 'migrate':
194
				$manager->migrate( $task );
195
				break;
196
			case 'rollback':
197
				$manager->rollback( $task );
198
				break;
199
			case 'clean':
200
				$manager->clean( $task );
201
				break;
202
			default:
203
				throw new \Exception( sprintf( 'Invalid setup action "%1$s"', $action ) );
204
		}
205
	}
206
207
208
	/**
209
	 * Returns a context object for the jobs command
210
	 *
211
	 * @return \Aimeos\MShop\Context\Item\Standard Context object
212
	 */
213
	protected function getContext()
214
	{
215
		$aimeos = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\Aimeos' )->get();
216
		$context = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\Context' )->get( null, 'command' );
217
		$uriBuilder = $this->objectManager->get( '\\Neos\\Flow\\Mvc\\Routing\\UriBuilder' );
218
219
		$request = \Neos\Flow\Http\Request::createFromEnvironment();
220
		$request->setBaseUri( new \Neos\Flow\Http\Uri( $this->baseUri ) );
221
		$uriBuilder->setRequest( new \Neos\Flow\Mvc\ActionRequest( $request ) );
222
223
		$tmplPaths = $aimeos->getCustomPaths( 'controller/jobs/templates' );
224
225
		$langManager = \Aimeos\MShop::create( $context, 'locale/language' );
226
		$langids = array_keys( $langManager->searchItems( $langManager->createSearch( true ) ) );
227
228
		$i18n = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\I18n' )->get( $langids );
229
		$view = $this->objectManager->get( '\\Aimeos\\Shop\\Base\\View' )->create( $context, $uriBuilder, $tmplPaths );
230
231
		$context->setEditor( 'aimeos:jobs' );
232
		$context->setView( $view );
233
		$context->setI18n( $i18n );
234
235
		return $context;
236
	}
237
238
239
	/**
240
	 * Returns the database configuration from the config object.
241
	 *
242
	 * @param \Aimeos\MW\Config\Iface $conf Config object
243
	 * @return array Multi-dimensional associative list of database configuration parameters
244
	 */
245
	protected function getDbConfig( \Aimeos\MW\Config\Iface $conf )
246
	{
247
		$dbconfig = $conf->get( 'resource', array() );
248
249
		foreach( $dbconfig as $rname => $dbconf )
250
		{
251
			if( strncmp( $rname, 'db', 2 ) !== 0 ) {
252
				unset( $dbconfig[$rname] );
253
			}
254
		}
255
256
		return $dbconfig;
257
	}
258
259
260
	/**
261
	 * Returns the enabled site items which may be limited by the input arguments.
262
	 *
263
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
264
	 * @param string $sites Unique site codes
265
	 * @return \Aimeos\MShop\Locale\Item\Site\Iface[] List of site items
266
	 */
267
	protected function getSiteItems( \Aimeos\MShop\Context\Item\Iface $context, $sites )
268
	{
269
		$manager = \Aimeos\MShop::create( $context, 'locale/site' );
270
		$search = $manager->createSearch();
271
272
		if( $sites !== '' ) {
273
			$search->setConditions( $search->compare( '==', 'locale.site.code', explode( ' ', $sites ) ) );
274
		}
275
276
		return $manager->searchItems( $search );
277
	}
278
279
280
	/**
281
	 * @param \Neos\Cache\Frontend\StringFrontend $cache
282
	 * @return void
283
	 */
284
	public function injectCache( \Neos\Cache\Frontend\StringFrontend $cache )
285
	{
286
		$this->cache = $cache;
287
	}
288
289
290
	/**
291
	 * @param \Neos\Flow\ObjectManagement\ObjectManagerInterface $objectManager
292
	 * @return void
293
	 */
294
	public function injectObjectManager( \Neos\Flow\ObjectManagement\ObjectManagerInterface $objectManager )
295
	{
296
		$this->objectManager = $objectManager;
297
	}
298
299
300
	/**
301
	 * Extracts the configuration options from the input object and updates the configuration values in the config object.
302
	 *
303
	 * @param \Aimeos\MW\Config\Iface $conf Configuration object
304
	 * @param array $options List of option key/value pairs
305
	 * @param array Associative list of database configurations
0 ignored issues
show
Bug introduced by
The type Aimeos\Shop\Command\Associative 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...
306
	 */
307
	protected function setOptions( \Aimeos\MW\Config\Iface $conf, array $options )
308
	{
309
		foreach( $options as $option )
310
		{
311
			list( $name, $value ) = explode( ':', $option );
312
			$conf->set( str_replace( '\\', '/', $name ), $value );
313
		}
314
	}
315
}
316