|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Everlution\AjaxcomBundle\Mutation; |
|
6
|
|
|
|
|
7
|
|
|
use Everlution\Ajaxcom\Handler; |
|
8
|
|
|
use Everlution\AjaxcomBundle\AjaxcomException; |
|
9
|
|
|
use Everlution\AjaxcomBundle\DataObject\Block; |
|
10
|
|
|
use Everlution\AjaxcomBundle\Service\RenderBlock; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class FlashMessages. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Ivan Barlog <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class FlashMessages implements MutatorInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var RenderBlock */ |
|
22
|
|
|
private $renderBlock; |
|
23
|
|
|
/** @var RequestStack */ |
|
24
|
|
|
private $requestStack; |
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
private $flashesTemplate; |
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
private $flashesBlockId; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
RenderBlock $renderBlock, |
|
32
|
|
|
RequestStack $requestStack, |
|
33
|
|
|
string $flashesTemplate, |
|
34
|
|
|
string $flashesBlockId |
|
35
|
|
|
) { |
|
36
|
|
|
$this->renderBlock = $renderBlock; |
|
37
|
|
|
$this->requestStack = $requestStack; |
|
38
|
|
|
$this->flashesTemplate = $flashesTemplate; |
|
39
|
|
|
$this->flashesBlockId = $flashesBlockId; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function mutate(Handler $ajax): Handler |
|
43
|
|
|
{ |
|
44
|
|
|
$session = $this->requestStack->getSession(); |
|
45
|
|
|
if (!$session instanceof Session) { |
|
46
|
|
|
throw new AjaxcomException('Bad session instance'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
try { |
|
50
|
|
|
$messages = $this->renderBlock->render( |
|
51
|
|
|
(new Block($this->flashesBlockId))->refresh(), |
|
52
|
|
|
$this->flashesTemplate, |
|
53
|
|
|
['flashes' => $session->/** @scrutinizer ignore-call */getFlashBag()->all()] |
|
54
|
|
|
); |
|
55
|
|
|
$ajax->container("#$this->flashesBlockId")->html($messages); |
|
56
|
|
|
} catch (AjaxcomException $exception) { |
|
57
|
|
|
// do nothing |
|
58
|
|
|
} finally { |
|
59
|
|
|
return $ajax; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|