Completed
Push — master ( 4e3077...d2ec78 )
by Aimeos
15:45
created

Jobs::usage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package Slim
7
 * @subpackage Command
8
 */
9
10
namespace Aimeos\Slim\Command;
11
12
13
/**
14
 * Aimeos cache command class
15
 *
16
 * @package Slim
17
 * @subpackage Command
18
 */
19
class Jobs extends Base implements Iface
20
{
21
	/**
22
	 * Executes the command
23
	 *
24
	 * @param array $argv Associative array from $_SERVER['argv']
0 ignored issues
show
Bug introduced by
There is no parameter named $argv. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
25
	 */
26
	public static function usage()
27
	{
28
		return "Usage: php job.php [--extdir=<path>]* [--config=<path>|<file>]* \"job1 [job2]*\" [\"sitecode1 [sitecode2]*\"]\n";
29
	}
30
31
32
	/**
33
	 * Returns the command usage and options
34
	 *
35
	 * @return string Command usage and options
36
	 */
37
	public static function run( array $argv )
38
	{
39
		array_shift( $argv );
40
		$options = self::getOptions( $argv );
41
42
		if( ( $jobs = array_shift( $argv ) ) === null ) {
43
			throw new \Aimeos\Slim\Command\Exception();
44
		}
45
		$sites = array_shift( $argv );
46
47
		$config = self::getConfig( $options );
48
49
		$app = new \Slim\App( $config );
50
		$aimeos = new \Aimeos\Slim\Bootstrap( $app, $config );
51
		$aimeos->setup( ( isset( $options['extdir'] ) ? $options['extdir'] : './ext' ) );
1 ignored issue
show
Unused Code introduced by
The call to the method Aimeos\Slim\Bootstrap::setup() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
52
53
		$container = $app->getContainer();
54
		$context = self::getContext( $container );
55
56
		$siteItems = self::getSiteItems( $context, $sites );
57
		self::execute( $container->get( 'aimeos' ), $context, $siteItems, $jobs );
58
	}
59
60
61
	/**
62
	 * Returns the configuration based on the given options
63
	 *
64
	 * @param array $options Associative list of given options
65
	 * @return array Multi-dimensional array of configuration settings
66
	 */
67
	protected static function getConfig( array $options )
68
	{
69
		$config = array();
70
71 View Code Duplication
		if( isset( $options['config'] ) )
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
		{
73
			foreach( (array) $options['config'] as $path )
74
			{
75
				if( is_file( $path ) ) {
76
					$config = array_replace_recursive( $config, require $path );
77
				}
78
			}
79
		}
80
81
		return $config;
82
	}
83
84
85
	/**
86
	 * Returns a new context object
87
	 *
88
	 * @param \Interop\Container\ContainerInterface $container Dependency injection container
89
	 * @return \Aimeos\MShop\Context\Item\Standard Context object
90
	 */
91
	protected static function getContext( \Interop\Container\ContainerInterface $container )
92
	{
93
		$aimeos = $container->get( 'aimeos' );
94
		$context = $container->get( 'aimeos_context' )->get( false );
95
96
		$env = \Slim\Http\Environment::mock();
97
		$request = \Slim\Http\Request::createFromEnvironment( $env );
98
		$response = new \Slim\Http\Response();
99
100
		$tmplPaths = $aimeos->getCustomPaths( 'controller/jobs/templates' );
101
		$view = $container->get( 'aimeos_view' )->create( $request, $response, array(), $tmplPaths );
102
103
		$langManager = \Aimeos\MShop\Factory::createManager( $context, 'locale/language' );
104
		$langids = array_keys( $langManager->searchItems( $langManager->createSearch( true ) ) );
105
		$i18n = $container->get( 'aimeos_i18n' )->get( $langids );
106
107
		$context->setEditor( 'aimeos:jobs' );
108
		$context->setView( $view );
109
		$context->setI18n( $i18n );
110
111
		return $context;
112
	}
113
114
115
	/**
116
	 * Removes the cached data for the given sites
117
	 *
118
	 * @param \Aimeos\Bootstrap $aimeos Aimeos bootstrap object
119
	 * @param \Aimeos\MShop\Context\Item\Iface $ctx Context object
120
	 * @param array $siteItems List of site items implementing \Aimeos\MShop\Locale\Site\Iface
121
	 */
122
	protected static function execute( \Aimeos\Bootstrap $aimeos, \Aimeos\MShop\Context\Item\Iface $ctx, array $siteItems, $jobs )
123
	{
124
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $ctx );
125
126
		foreach( $siteItems as $siteItem )
127
		{
128
			$localeItem = $localeManager->bootstrap( $siteItem->getCode(), 'en', '', false );
129
			$ctx->setLocale( $localeItem );
130
131
			printf( "Executing the Aimeos jobs for \"%s\"\n", $siteItem->getCode() );
132
133
			foreach( (array) explode( ' ', $jobs ) as $jobname ) {
134
				\Aimeos\Controller\Jobs\Factory::createController( $ctx, $aimeos, $jobname )->run();
135
			}
136
		}
137
	}
138
}