ProfileStorageResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A resolve() 0 13 4
1
<?php
2
3
namespace SixtyEightPublishers\Application\Environment\Translation;
4
5
use Kdyby\Translation\IUserLocaleResolver;
6
use Kdyby\Translation\Translator;
7
use SixtyEightPublishers\Application\Environment\IProfileStorage;
8
9
class ProfileStorageResolver implements IUserLocaleResolver
10
{
11
	/** @var \SixtyEightPublishers\Application\Environment\IProfileStorage  */
12
	private $storage;
13
14
	/** @var bool  */
15
	private $useDefault = FALSE;
16
17
	/** @var bool */
18
	private $lock = FALSE;
19
20
	/**
21
	 * @param \SixtyEightPublishers\Application\Environment\IProfileStorage $storage
22
	 * @param bool                                                          $useDefault
23
	 */
24
	public function __construct(IProfileStorage $storage, bool $useDefault = FALSE)
25
	{
26
		$this->storage = $storage;
27
		$this->useDefault = $useDefault;
28
	}
29
30
	/**
31
	 * @param \Kdyby\Translation\Translator $translator
32
	 *
33
	 * @return string
34
	 */
35
	public function resolve(Translator $translator)
36
	{
37
		$locale = $this->storage->getProfile()->getLanguage($this->useDefault);
38
		if (is_null($locale) && !$this->lock) {
39
			$this->lock = TRUE;
40
			if (NULL !== ($newLocale = $translator->getLocale())) {
41
				$this->storage->getProfile()->changeLanguage($newLocale, FALSE);
42
			}
43
			$this->lock = FALSE;
44
		}
45
46
		return $locale;
47
	}
48
}
49