SessionLocaleResolver::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 8
rs 10
ccs 6
cts 6
cp 1
cc 2
nc 2
nop 1
crap 2
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
 */
18 1
final class SessionLocaleResolver implements ISettableLocaleResolver {
19
  use \Nette\SmartObject;
20
21
  private Session $session;
22
  private SessionSection $section;
23
  public string $varName = "lang";
24
  
25
  public function __construct(Session $session = null) {
26 1
    if($session === null) {
27 1
      $request = (new RequestFactory())->fromGlobals();
28 1
      $response = new Response();
29 1
      $session = new Session($request, $response);
30
    }
31 1
    $this->session = $session;
32 1
    $this->section = $session->getSection(get_class($this));
33 1
  }
34
35
  /**
36
   * @deprecated Access the property directly
37
   */
38
  public function getLang(): ?string {
39 1
    $lang = (string) $this->section->{$this->varName};
40 1
    if($lang === '') {
41 1
      return null;
42
    }
43 1
    return $lang;
44
  }
45
46
  /**
47
   * @deprecated Access the property directly
48
   */
49
  public function setLang(?string $lang): void {
50 1
    $this->section->{$this->varName} = $lang;
51 1
  }
52
53
  /**
54
   * @deprecated Access the property directly
55
   */
56
  public function getVarName(): string {
57
    return $this->varName;
58
  }
59
60
  /**
61
   * @deprecated Access the property directly
62
   */
63
  public function setVarName(string $varName): void {
64
    $this->varName = $varName;
65
  }
66
  
67
  public function resolve(): ?string {
68 1
    return $this->getLang();
0 ignored issues
show
Deprecated Code introduced by
The function Nexendrie\Translation\Re...caleResolver::getLang() has been deprecated: Access the property directly ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

68
    return /** @scrutinizer ignore-deprecated */ $this->getLang();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
69
  }
70
}
71
?>