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