JobsCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A context() 0 13 1
A handle() 0 24 3
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2023
6
 */
7
8
9
namespace Aimeos\Shop\Command;
10
11
use Illuminate\Console\Command;
12
13
14
/**
15
 * Command for executing the Aimeos job controllers
16
 */
17
class JobsCommand extends AbstractCommand
18
{
19
	/**
20
	 * The name and signature of the console command.
21
	 *
22
	 * @var string
23
	 */
24
	protected $signature = 'aimeos:jobs
25
		{jobs : One or more job controller names like "admin/job customer/email/watch"}
26
		{site? : Site codes to execute the jobs for like "default unittest" (none for all)}
27
	';
28
29
	/**
30
	 * The console command description.
31
	 *
32
	 * @var string
33
	 */
34
	protected $description = 'Executes the job controllers';
35
36
37
	/**
38
	 * Execute the console command.
39
	 *
40
	 * @return mixed
41
	 */
42
	public function handle()
43
	{
44
		$jobs = $this->argument( 'jobs' );
45
		$jobs = !is_array( $jobs ) ? explode( ' ', (string) $jobs ) : $jobs;
46
47
		$fcn = function( \Aimeos\MShop\ContextIface $lcontext, \Aimeos\Bootstrap $aimeos ) use ( $jobs )
48
		{
49
			$jobfcn = function( $context, $aimeos, $jobname ) {
50
				\Aimeos\Controller\Jobs::create( $context, $aimeos, $jobname )->run();
51
			};
52
53
			$process = $lcontext->process();
54
			$site = $lcontext->locale()->getSiteItem()->getCode();
55
56
			foreach( $jobs as $jobname )
57
			{
58
				$this->info( sprintf( 'Executing Aimeos jobs "%s" for "%s"', $jobname, $site ), 'v' );
59
				$process->start( $jobfcn, [$lcontext, $aimeos, $jobname], false );
60
			}
61
62
			$process->wait();
63
		};
64
65
		$this->exec( $this->context(), $fcn, $this->argument( 'site' ) );
66
	}
67
68
69
	/**
70
	 * Returns a context object
71
	 *
72
	 * @return \Aimeos\MShop\ContextIface Context object
73
	 */
74
	protected function context() : \Aimeos\MShop\ContextIface
75
	{
76
		$lv = $this->getLaravel();
77
		$context = $lv->make( 'aimeos.context' )->get( false, 'command' );
78
79
		$langManager = \Aimeos\MShop::create( $context, 'locale/language' );
80
		$langids = $langManager->search( $langManager->filter( true ) )->keys()->toArray();
81
		$i18n = $lv->make( 'aimeos.i18n' )->get( $langids );
82
83
		$context->setEditor( 'aimeos:jobs' );
84
		$context->setI18n( $i18n );
85
86
		return $context;
87
	}
88
}
89