Completed
Push — master ( 96c485...89068a )
by Aimeos
02:47
created

Bootstrap   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 128
Duplicated Lines 2.34 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 13
c 2
b 0
f 1
lcom 1
cbo 8
dl 3
loc 128
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A routes() 0 23 3
A setup() 3 47 3
B getVersion() 0 17 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package Slim
7
 */
8
9
10
namespace Aimeos\Slim;
11
12
13
/**
14
 * Bootstrap class for Aimeos Slim integration
15
 *
16
 * @package Slim
17
 */
18
class Bootstrap
19
{
20
	private $app;
21
	private $settings;
22
23
24
	/**
25
	 * Initializes the object
26
	 *
27
	 * @param \Slim\App $app Slim application
28
	 * @param array $settings Multi-dimensional array of configuration settings
29
	 */
30
	public function __construct( \Slim\App $app, array $settings )
31
	{
32
		$this->app = $app;
33
		$this->settings = $settings;
34
	}
35
36
37
	/**
38
	 * Registers the Aimeos routes
39
	 *
40
	 * @param string $path Absolute or relative path to the Aimeos route file
41
	 * @return \Aimeos\Slim\Bootstrap Self instance
42
	 */
43
	public function routes( $path )
44
	{
45
		$app = $this->app;
46
		$settings = $this->settings;
47
48
		$config = function( $key, $default ) use ( $settings )
49
		{
50
			foreach( explode( '/', trim( $key, '/' ) ) as $part )
51
			{
52
				if( isset( $settings[$part] ) ) {
53
					$settings = $settings[$part];
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $settings, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
54
				} else {
55
					return $default;
56
				}
57
			}
58
59
			return $settings;
60
		};
61
62
		require $path;
63
64
		return $this;
65
	}
66
67
68
	/**
69
	 * Sets up the Aimeos environemnt
70
	 *
71
	 * @param string $extdir Absolute or relative path to the Aimeos extension directory
72
	 * @return \Aimeos\Slim\Bootstrap Self instance
73
	 */
74
	public function setup( $extdir = '../ext' )
75
	{
76
		$settings = $this->settings;
77
		$container = $this->app->getContainer();
78
79
		$container['aimeos'] = function( $c ) use ( $extdir ) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
			return new \Aimeos\Bootstrap( (array) $extdir, false );
81
		};
82
83
		$container['aimeos_context'] = function( $c ) {
84
			return new \Aimeos\Slim\Base\Context( $c );
85
		};
86
87
		$container['aimeos_i18n'] = function( $c ) {
88
			return new \Aimeos\Slim\Base\I18n( $c );
89
		};
90
91
		$container['aimeos_page'] = function( $c ) {
92
			return new \Aimeos\Slim\Base\Page( $c );
93
		};
94
95
		$container['aimeos_view'] = function( $c ) {
96
			return new \Aimeos\Slim\Base\View( $c );
97
		};
98
99
100
		$container['aimeos_config'] = function( $c ) use ( $settings ) {
101
102
			$default = include __DIR__ . DIRECTORY_SEPARATOR . 'aimeos-default.php';
103
			$settings = array_replace_recursive( $default, $settings );
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $settings, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
104
105
			$config = new \Aimeos\MW\Config\PHPArray( $settings, $c['aimeos']->getConfigPaths() );
106
107 View Code Duplication
			if( function_exists( 'apc_store' ) === true && $config->get( 'apc_enabled', false ) == true ) {
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...
108
				$config = new \Aimeos\MW\Config\Decorator\APC( $config, $config->get( 'apc_prefix', 'slim:' ) );
109
			}
110
111
			return $config;
112
		};
113
114
115
		$container['mailer'] = function( $c ) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
			return \Swift_Mailer::newInstance( \Swift_SendmailTransport::newInstance() );
0 ignored issues
show
Bug introduced by
The method newInstance() does not seem to exist on object<Swift_SendmailTransport>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method newInstance() does not seem to exist on object<Swift_Mailer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
		};
118
119
		return $this;
120
	}
121
122
123
	/**
124
	 * Returns the version of the Aimeos package
125
	 *
126
	 * @return string Version string
127
	 */
128
	public static function getVersion()
129
	{
130
		$basedir = dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) . DIRECTORY_SEPARATOR;
131
132
		if( ( $content = @file_get_contents( $basedir . 'composer.lock' ) ) !== false
133
			&& ( $content = json_decode( $content, true ) ) !== null && isset( $content['packages'] )
134
		) {
135
			foreach( (array) $content['packages'] as $item )
136
			{
137
				if( $item['name'] === 'aimeos/aimeos-slim' ) {
138
					return $item['version'];
139
				}
140
			}
141
		}
142
143
		return '';
144
	}
145
}