Passed
Push — master ( c13612...d9e22e )
by Aimeos
12:46 queued 08:08
created

JobsCommand::context()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
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
15
16
/**
17
 * Command for executing the Aimeos job controllers
18
 * @package laravel
19
 * @subpackage Command
20
 */
21
class JobsCommand extends AbstractCommand
22
{
23
	/**
24
	 * The name and signature of the console command.
25
	 *
26
	 * @var string
27
	 */
28
	protected $signature = 'aimeos:jobs
29
		{jobs : One or more job controller names like "admin/job customer/email/watch"}
30
		{site? : Site codes to execute the jobs for like "default unittest" (none for all)}
31
	';
32
33
	/**
34
	 * The console command description.
35
	 *
36
	 * @var string
37
	 */
38
	protected $description = 'Executes the job controllers';
39
40
41
	/**
42
	 * Execute the console command.
43
	 *
44
	 * @return mixed
45
	 */
46
	public function handle()
47
	{
48
		$jobs = $this->argument( 'jobs' );
49
		$jobs = !is_array( $jobs ) ? explode( ' ', (string) $jobs ) : $jobs;
50
51
		$fcn = function( \Aimeos\MShop\Context\Item\Iface $lcontext, \Aimeos\Bootstrap $aimeos ) use ( $jobs )
52
		{
53
			$jobfcn = function( $context, $aimeos, $jobname ) {
54
				\Aimeos\Controller\Jobs::create( $context, $aimeos, $jobname )->run();
55
			};
56
57
			$process = $lcontext->getProcess();
58
			$site = $lcontext->getLocale()->getSiteItem()->getCode();
59
60
			foreach( $jobs as $jobname )
61
			{
62
				$this->info( sprintf( 'Executing Aimeos jobs "%s" for "%s"', $jobname, $site ), 'v' );
63
				$process->start( $jobfcn, [$lcontext, $aimeos, $jobname], false );
64
			}
65
66
			$process->wait();
67
		};
68
69
		$this->exec( $this->context(), $fcn, $this->argument( 'site' ) );
70
	}
71
72
73
	/**
74
	 * Returns a context object
75
	 *
76
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
77
	 */
78
	protected function context() : \Aimeos\MShop\Context\Item\Iface
79
	{
80
		$lv = $this->getLaravel();
81
		$context = $lv->make( 'aimeos.context' )->get( false, 'command' );
82
83
		$langManager = \Aimeos\MShop::create( $context, 'locale/language' );
84
		$langids = $langManager->search( $langManager->filter( true ) )->keys()->toArray();
85
		$i18n = $lv->make( 'aimeos.i18n' )->get( $langids );
86
87
		$context->setEditor( 'aimeos:jobs' );
88
		$context->setI18n( $i18n );
89
90
		return $context;
91
	}
92
}
93