Completed
Push — master ( 6f8588...d5f400 )
by Aimeos
19:59
created

JobsCommand::fire()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
nc 3
nop 0
dl 0
loc 31
rs 8.8571
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package laravel
7
 * @subpackage Command
8
 */
9
10
11
namespace Aimeos\Shop\Command;
12
13
use Illuminate\Console\Command;
14
use Symfony\Component\Console\Input\InputArgument;
15
16
17
/**
18
 * Command for executing the Aimeos job controllers
19
 * @package laravel
20
 * @subpackage Command
21
 */
22
class JobsCommand extends AbstractCommand
23
{
24
	/**
25
	 * The console command name.
26
	 *
27
	 * @var string
28
	 */
29
	protected $name = 'aimeos:jobs';
30
31
	/**
32
	 * The console command description.
33
	 *
34
	 * @var string
35
	 */
36
	protected $description = 'Executes the job controllers';
37
38
39
	/**
40
	 * Execute the console command.
41
	 *
42
	 * @return mixed
43
	 */
44
	public function handle()
45
	{
46
		$aimeos = $this->getLaravel()->make( '\Aimeos\Shop\Base\Aimeos' )->get();
47
		$context = $this->getContext();
48
49
		$process = $context->getProcess();
50
		$jobs = explode( ' ', $this->argument( 'jobs' ) );
51
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
52
53
		foreach( $this->getSiteItems( $context, $this->argument( 'site' ) ) as $siteItem )
54
		{
55
			$localeItem = $localeManager->bootstrap( $siteItem->getCode(), '', '', false );
56
			$localeItem->setLanguageId( null );
57
			$localeItem->setCurrencyId( null );
58
59
			$context->setLocale( $localeItem );
60
61
			$this->info( sprintf( 'Executing the Aimeos jobs for "%s"', $siteItem->getCode() ) );
62
63
			foreach( $jobs as $jobname )
64
			{
65
				$fcn = function( $context, $aimeos, $jobname ) {
66
					\Aimeos\Controller\Jobs\Factory::createController( $context, $aimeos, $jobname )->run();
67
				};
68
69
				$process->start( $fcn, [$context, $aimeos, $jobname], true );
70
			}
71
		}
72
73
		$process->wait();
74
	}
75
76
77
	/**
78
	 * Get the console command arguments.
79
	 *
80
	 * @return array
81
	 */
82
	protected function getArguments()
83
	{
84
		return array(
85
			array( 'jobs', InputArgument::REQUIRED, 'One or more job controller names like "admin/job customer/email/watch"' ),
86
			array( 'site', InputArgument::OPTIONAL, 'Site codes to execute the jobs for like "default unittest" (none for all)' ),
87
		);
88
	}
89
90
	/**
91
	 * Get the console command options.
92
	 *
93
	 * @return array
94
	 */
95
	protected function getOptions()
96
	{
97
		return array();
98
	}
99
100
101
	/**
102
	 * Returns a context object
103
	 *
104
	 * @return \Aimeos\MShop\Context\Item\Standard Context object
105
	 */
106
	protected function getContext()
107
	{
108
		$lv = $this->getLaravel();
109
		$aimeos = $lv->make( '\Aimeos\Shop\Base\Aimeos' )->get();
110
		$context = $lv->make( '\Aimeos\Shop\Base\Context' )->get( false, 'command' );
111
112
		$tmplPaths = $aimeos->getCustomPaths( 'controller/jobs/templates' );
113
		$view = $lv->make( '\Aimeos\Shop\Base\View' )->create( $context, $tmplPaths );
114
115
		$langManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context )->getSubManager( 'language' );
116
		$langids = array_keys( $langManager->searchItems( $langManager->createSearch( true ) ) );
117
		$i18n = $lv->make( '\Aimeos\Shop\Base\I18n' )->get( $langids );
118
119
		$context->setEditor( 'aimeos:jobs' );
120
		$context->setView( $view );
121
		$context->setI18n( $i18n );
122
123
		return $context;
124
	}
125
}
126