Completed
Push — master ( ad6ad2...4f1a9d )
by Aimeos
02:36
created

Locale   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 25.37 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 17
loc 67
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B get() 0 16 5
A getBackend() 17 17 2

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
 * @subpackage Base
8
 */
9
10
namespace Aimeos\Slim\Base;
11
12
use Interop\Container\ContainerInterface;
13
14
15
/**
16
 * Service providing the locale objects
17
 *
18
 * @package Slim
19
 * @subpackage Base
20
 */
21
class Locale
22
{
23
	private $container;
24
	private $locale;
25
26
27
	/**
28
	 * Initializes the object
29
	 *
30
	 * @param ContainerInterface $container Dependency container
31
	 */
32
	public function __construct( ContainerInterface $container )
33
	{
34
		$this->container = $container;
35
	}
36
37
38
	/**
39
	 * Returns the locale item for the current request
40
	 *
41
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
42
	 * @param array $attributes Associative list of URL parameter
43
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item object
44
	 */
45
	public function get( \Aimeos\MShop\Context\Item\Iface $context, array $attributes )
46
	{
47
		if( $this->locale === null )
48
		{
49
			$disableSites = $this->container->get( 'aimeos_config' )->get( 'disableSites', true );
50
51
			$site = ( isset( $attributes['site'] ) ? $attributes['site'] : 'default' );
52
			$lang = ( isset( $attributes['locale'] ) ? $attributes['locale'] : '' );
53
			$currency = ( isset( $attributes['currency'] ) ? $attributes['currency'] : '' );
54
55
			$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
56
			$this->locale = $localeManager->bootstrap( $site, $lang, $currency, $disableSites );
57
		}
58
59
		return $this->locale;
60
	}
61
62
63
	/**
64
	 * Returns the locale item for the current request
65
	 *
66
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
67
	 * @param string $site Unique site code
68
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item object
69
	 */
70 View Code Duplication
	public function getBackend( \Aimeos\MShop\Context\Item\Iface $context, $site )
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
71
	{
72
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
73
74
		try
75
		{
76
			$localeItem = $localeManager->bootstrap( $site, '', '', false );
77
			$localeItem->setLanguageId( null );
78
			$localeItem->setCurrencyId( null );
79
		}
80
		catch( \Aimeos\MShop\Locale\Exception $e )
81
		{
82
			$localeItem = $localeManager->createItem();
83
		}
84
85
		return $localeItem;
86
	}
87
}
88