Passed
Push — master ( 5274cf...3733ff )
by Radovan
02:25 queued 11s
created

CookieResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 5
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
19
/**
20
 * @api
21
 */
22
readonly class CookieResolver implements Resolver
23
{
24
	public function __construct(
25
		protected string $parameterName,
26
		protected Request $httpRequest,
27
		protected Response $httpResponse,
28
	) {}
29
30
	/**
31
	 * @param string[] $allowed
32
	 */
33
	#[\Override]
34
	public function resolve(array $allowed): ?string
35
	{
36
		return $this->httpRequest->getCookie($this->parameterName);
37
	}
38
39
	public function set(string $lang): bool
40
	{
41
		try {
42
			$this->httpResponse->setCookie(
43
				$this->parameterName,
44
				$lang,
45
				null,
46
			);
47
			return true;
48
		} catch (\Throwable) {
49
			return false;
50
		}
51
	}
52
}
53