Completed
Pull Request — master (#27)
by
unknown
05:50
created

ProfileStorageResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 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 $lock = FALSE;
16
17
	/**
18
	 * @param \SixtyEightPublishers\Application\Environment\IProfileStorage $storage
19
	 */
20
	public function __construct(IProfileStorage $storage)
21
	{
22
		$this->storage = $storage;
23
	}
24
25
	/**
26
	 * @param \Kdyby\Translation\Translator $translator
27
	 *
28
	 * @return string
29
	 */
30
	public function resolve(Translator $translator)
31
	{
32
		$locale = $this->storage->getProfile()->getLanguage(FALSE);
33
		if (is_null($locale) && !$this->lock) {
34
			$this->lock = TRUE;
35
			if (NULL !== ($newLocale = $translator->getLocale())) {
36
				$this->storage->getProfile()->changeLanguage($newLocale, FALSE);
37
			}
38
			$this->lock = FALSE;
39
		}
40
41
		return $locale;
42
	}
43
}
44