SessionLocaleResolver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 8
eloc 18
c 3
b 0
f 1
dl 0
loc 41
rs 10
ccs 18
cts 18
cp 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getLang() 0 5 2
A getVarName() 0 2 1
A __construct() 0 8 2
A resolve() 0 2 1
A setLang() 0 2 1
A setVarName() 0 2 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Translation\Resolvers;
5
6
use Nette\Http\Session;
7
use Nette\Http\SessionSection;
8
use Nette\Http\RequestFactory;
9
use Nette\Http\Response;
10
use Nexendrie\Translation\ISettableLocaleResolver;
11
12
/**
13
 * SessionLocaleResolver
14
 *
15
 * @author Jakub Konečný
16
 * @property string|null $lang
17
 * @property string $varName
18
 */
19 1
final class SessionLocaleResolver implements ISettableLocaleResolver {
20 1
  use \Nette\SmartObject;
21
  
22
  /** @var Session */
23
  protected $session;
24
  /** @var SessionSection */
25
  protected $section;
26
  /** @var string */
27
  protected $varName = "lang";
28
  
29
  public function __construct(Session $session = null) {
30 1
    if(is_null($session)) {
31 1
      $request = (new RequestFactory())->createHttpRequest();
32 1
      $response = new Response();
33 1
      $session = new Session($request, $response);
34
    }
35 1
    $this->session = $session;
36 1
    $this->section = $session->getSection(get_class($this));
37 1
  }
38
  
39
  public function getLang(): ?string {
40 1
    if(empty($this->section->{$this->varName})) {
41 1
      return null;
42
    }
43 1
    return $this->section->{$this->varName};
44
  }
45
  
46
  public function setLang(string $lang): void {
47 1
    $this->section->{$this->varName} = $lang;
48 1
  }
49
  
50
  public function getVarName(): string {
51 1
    return $this->varName;
52
  }
53
  
54
  public function setVarName(string $varName): void {
55 1
    $this->varName = $varName;
56 1
  }
57
  
58
  public function resolve(): ?string {
59 1
    return $this->getLang();
60
  }
61
}
62
?>