Passed
Push — master ( bb74df...01777c )
by Jakub
02:24
created

src/Resolvers/SessionLocaleResolver.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Translation\Resolvers;
5
6
use Nette\Http\Session,
7
    Nette\Http\SessionSection,
8
    Nette\Http\RequestFactory,
9
    Nette\Http\Response;
10
11
/**
12
 * SessionLocaleResolver
13
 *
14
 * @author Jakub Konečný
15
 * @property string|NULL $lang
16
 * @property string $varName
17
 */
18 1
class SessionLocaleResolver implements ILocaleResolver {
19 1
  use \Nette\SmartObject;
20
  
21
  /** @var Session */
22
  protected $session;
23
  /** @var SessionSection */
24
  protected $section;
25
  /** @var string */
26
  protected $varName = "lang";
27
  
28
  public function __construct(Session $session = NULL) {
29 1
    if(is_null($session)) {
30 1
      $request = (new RequestFactory)->createHttpRequest();
31 1
      $response = new Response;
32 1
      $session = new Session($request, $response);
33
    }
34 1
    $this->session = $session;
35 1
    $this->section = $session->getSection(get_class($this));
36 1
  }
37
  
38
  public function getLang(): ?string {
0 ignored issues
show
Expected 1 space after closing parenthesis; found 0
Loading history...
Inline shorthand IF statement must be declared on a single line
Loading history...
39 1
    if(empty($this->section->{$this->varName})) {
40 1
      return NULL;
41
    }
42 1
    return $this->section->{$this->varName};
43
  }
44
  
45
  public function setLang(string $lang) {
46 1
    $this->section->{$this->varName} = $lang;
47 1
  }
48
  
49
  public function getVarName(): string {
0 ignored issues
show
Expected 1 space after closing parenthesis; found 0
Loading history...
50 1
    return $this->varName;
51
  }
52
  
53
  public function setVarName(string $varName) {
54 1
    $this->varName = $varName;
55 1
  }
56
  
57
  /**
58
   * Resolve language
59
   */
60
  public function resolve(): ?string {
0 ignored issues
show
Expected 1 space after closing parenthesis; found 0
Loading history...
Inline shorthand IF statement must be declared on a single line
Loading history...
61 1
    return $this->getLang();
62
  }
63
}
64
?>