Completed
Push — master ( 22fee6...a6e7a8 )
by Aimeos
02:32
created

Locale::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2014-2016
6
 * @package symfony
7
 * @subpackage Service
8
 */
9
10
namespace Aimeos\ShopBundle\Service;
11
12
use Symfony\Component\HttpFoundation\RequestStack;
13
use Symfony\Component\DependencyInjection\Container;
14
15
16
/**
17
 * Service providing the locale objects
18
 *
19
 * @package symfony
20
 * @subpackage Service
21
 */
22
class Locale
23
{
24
	private $requestStack;
25
	private $container;
26
	private $locale;
27
28
29
	/**
30
	 * Initializes the locale factory object
31
	 *
32
	 * @param RequestStack $requestStack Current request stack
33
	 * @param Container $container Container object to access parameters
34
	 */
35
	public function __construct( RequestStack $requestStack, Container $container )
36
	{
37
		$this->requestStack = $requestStack;
38
		$this->container = $container;
39
	}
40
41
42
	/**
43
	 * Returns the locale item for the current request
44
	 *
45
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
46
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item object
47
	 */
48
	public function get( \Aimeos\MShop\Context\Item\Iface $context )
49
	{
50
		if( $this->locale === null )
51
		{
52
			$status = $this->container->getParameter( 'aimeos_shop.disable_sites' );
53
			$request = $this->requestStack->getMasterRequest();
54
55
			$site = $request->attributes->get( 'site', $request->query->get( 'site', 'default' ) );
56
			$currency = $request->attributes->get( 'currency', $request->query->get( 'currency', '' ) );
57
			$lang = $request->attributes->get( 'locale', $request->query->get( 'locale', '' ) );
58
59
			$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $context );
60
			$this->locale = $localeManager->bootstrap( $site, $lang, $currency, $status );
61
		}
62
63
		return $this->locale;
64
	}
65
66
67
	/**
68
	 * Returns the locale item for the current request
69
	 *
70
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
71
	 * @param string $site Unique site code
72
	 * @return \Aimeos\MShop\Locale\Item\Iface Locale item object
73
	 */
74
	public function getBackend( \Aimeos\MShop\Context\Item\Iface $context, $site )
75
	{
76
		$localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
77
78
		try
79
		{
80
			$localeItem = $localeManager->bootstrap( $site, '', '', false );
81
			$localeItem->setLanguageId( null );
82
			$localeItem->setCurrencyId( null );
83
		}
84
		catch( \Aimeos\MShop\Locale\Exception $e )
85
		{
86
			$localeItem = $localeManager->createItem();
87
		}
88
89
		return $localeItem;
90
	}
91
}
92