Passed
Push — main ( 1f01d2...1fb2d3 )
by Radovan
02:16
created

CookieLocaleResolver::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * Nette extension for bckp/translator
6
 * (c) Radovan Kepák
7
 *
8
 * For the full copyright and license information, please view the file license.md that was distributed with this source code.
9
 *
10
 * @author Radovan Kepak <[email protected]>
11
 *  --------------------------------------------------------------------------
12
 */
13
14
namespace Bckp\Translator\Nette\Resolvers;
15
16
use Nette\Http\Request;
17
use Nette\Http\Response;
18
use Throwable;
19
20
/**
21
 * @api
22
 */
23
readonly class CookieLocaleResolver implements LocaleResolver
24
{
25
	public function __construct(
26
		protected string $parameterName,
27
		protected Request $httpRequest,
28
		protected Response $httpResponse,
29
	) {}
30
31
	/**
32
	 * @param string[] $allowed
33
	 */
34
	#[\Override]
35
	public function resolve(array $allowed): ?string
36
	{
37
		return $this->httpRequest->getCookie($this->parameterName);
38
	}
39
40
	public function set(string $lang): bool
41
	{
42
		try {
43
			$this->httpResponse->setCookie(
44
				$this->parameterName,
45
				$lang,
46
				null,
47
			);
48
			return true;
49
		} catch (Throwable) {
50
			return false;
51
		}
52
	}
53
}
54