1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\Application\Environment\Storage; |
6
|
|
|
|
7
|
|
|
use Nette\Http\Session; |
8
|
|
|
use Nette\SmartObject; |
9
|
|
|
use SixtyEightPublishers\Application\Environment\ActiveProfile; |
10
|
|
|
use SixtyEightPublishers\Application\Environment\IProfile; |
11
|
|
|
use SixtyEightPublishers\Application\Environment\IProfileStorage; |
12
|
|
|
use SixtyEightPublishers\Application\Environment\ProfileConfigurationException; |
13
|
|
|
|
14
|
|
|
class SessionProfileStorage implements IProfileStorage |
15
|
|
|
{ |
16
|
|
|
use SmartObject; |
17
|
|
|
|
18
|
|
|
const SESSION_SECTION = 'SixtyEightPublishers.Application'; |
19
|
|
|
|
20
|
|
|
/** @var null|ActiveProfile */ |
21
|
|
|
private $profile; |
22
|
|
|
|
23
|
|
|
/** @var \Nette\Http\SessionSection */ |
24
|
|
|
private $session; |
25
|
|
|
|
26
|
|
|
/** @var null|callable */ |
27
|
|
|
public $onPersist; |
28
|
|
|
|
29
|
|
|
/** @var null|callable */ |
30
|
|
|
public $onProfileSet; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \Nette\Http\Session $session |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Session $session) |
36
|
|
|
{ |
37
|
|
|
$this->session = $session->getSection(self::SESSION_SECTION); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/************* interface \SixtyEightPublishers\Application\Environment\IProfileStorage *************/ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function setProfile(IProfile $profile) |
47
|
|
|
{ |
48
|
|
|
$this->profile = new ActiveProfile($profile, $this); |
49
|
|
|
|
50
|
|
|
if ($this->session['profileName'] === $this->profile->getName()) { |
51
|
|
|
try { |
52
|
|
|
$this->profile->changeCountry($this->session['profileCountry'], FALSE); |
53
|
|
|
} catch (ProfileConfigurationException $e) { |
|
|
|
|
54
|
|
|
} |
55
|
|
|
try { |
56
|
|
|
$this->profile->changeLanguage($this->session['profileLanguage'], FALSE); |
57
|
|
|
} catch (ProfileConfigurationException $e) { |
|
|
|
|
58
|
|
|
} |
59
|
|
|
try { |
60
|
|
|
$this->profile->changeCurrency($this->session['profileCurrency'], FALSE); |
61
|
|
|
} catch (ProfileConfigurationException $e) { |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} else { |
64
|
|
|
$this->session['profileName'] = $this->profile->getName(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->onProfileSet($this->profile); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return \SixtyEightPublishers\Application\Environment\ActiveProfile |
72
|
|
|
*/ |
73
|
|
|
public function getProfile() : ActiveProfile |
74
|
|
|
{ |
75
|
|
|
return $this->profile; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function persist() |
82
|
|
|
{ |
83
|
|
|
$this->onPersist($this->profile); |
84
|
|
|
$this->session['profileName'] = $this->profile->getName(); |
85
|
|
|
$this->session['profileCountry'] = $this->profile->getCountry(); |
86
|
|
|
$this->session['profileLanguage'] = $this->profile->getLanguage(); |
87
|
|
|
$this->session['profileCurrency'] = $this->profile->getCurrency(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|