Skip to content

Commit e3e3b29

Browse files
committed
Added method to obtain the browser session controller from the trait's parent class
1 parent 5080fcb commit e3e3b29

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/BrowserSession/BrowserSessionTrait.php

+25-19
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,55 @@
33

44
trait BrowserSessionTrait
55
{
6-
private $browserSessionController;
7-
86
/**
97
* @return BrowserSessionController
8+
* @throws \Exception
109
*/
11-
protected function getBrowserSessionController()
10+
private function tryGetBrowserSessionController()
1211
{
13-
return $this->browserSessionController;
14-
}
12+
$browserSessionController = null;
13+
if (method_exists($this, 'getContainer')) {
14+
$container = call_user_func([$this, 'getContainer']);
15+
if (isset($container['browserSessionController'])) {
16+
$browserSessionController = $container['browserSessionController'];
17+
}
18+
} elseif (method_exists($this, 'getBrowserSessionController')) {
19+
$browserSessionController = call_user_func([$this, 'getBrowserSessionController']);
20+
}
1521

16-
/**
17-
* @param BrowserSessionController $browserSessionController
18-
* @return $this
19-
*/
20-
protected function setBrowserSessionController(BrowserSessionController $browserSessionController)
21-
{
22-
$this->browserSessionController = $browserSessionController;
23-
return $this;
22+
if ($browserSessionController instanceof BrowserSessionController) {
23+
return $browserSessionController;
24+
}
25+
26+
throw new \Exception('You need to provide the trait a method to get the BrowserSessionController');
2427
}
2528

2629
/**
2730
* @return null|BrowserSessionEntity
2831
*/
2932
protected function getBrowserSession()
3033
{
31-
if (!$session = $this->getBrowserSessionController()->getSession()) {
32-
$session = $this->getBrowserSessionController()->createSession();
34+
$browserSessionController = $this->tryGetBrowserSessionController();
35+
if (!$session = $browserSessionController->getSession()) {
36+
$session = $browserSessionController->createSession();
3337
} else {
34-
$this->getBrowserSessionController()->flagSessionForUpdate($session);
38+
$browserSessionController->flagSessionForUpdate($session);
3539
}
36-
$this->getBrowserSessionController()->saveCookie($session);
40+
$browserSessionController->saveCookie($session);
3741
return $session;
3842
}
3943

4044
protected function updateBrowserSession()
4145
{
46+
$browserSessionController = $this->tryGetBrowserSessionController();
4247
$session = $this->getBrowserSession();
43-
$this->getBrowserSessionController()->updateSession($session);
48+
$browserSessionController->updateSession($session);
4449
}
4550

4651
protected function flagBrowserSessionForUpdate()
4752
{
53+
$browserSessionController = $this->tryGetBrowserSessionController();
4854
$session = $this->getBrowserSession();
49-
$this->getBrowserSessionController()->flagSessionForUpdate($session);
55+
$browserSessionController->flagSessionForUpdate($session);
5056
}
5157
}

0 commit comments

Comments
 (0)