Completed
Push — master ( 4d6583...31ed54 )
by Vojtěch
02:09
created

ProfileStorageResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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