1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the SexyField package. |
5
|
|
|
* |
6
|
|
|
* (c) Dion Snoeijen <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare (strict_types = 1); |
13
|
|
|
|
14
|
|
|
namespace Tardigrades\SectionField\Service; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
17
|
|
|
use Tardigrades\SectionField\Event\SectionEntryBeforeCreate; |
18
|
|
|
use Tardigrades\SectionField\Event\SectionEntryCreated; |
19
|
|
|
use Tardigrades\SectionField\Generator\CommonSectionInterface; |
20
|
|
|
use Tardigrades\SectionField\ValueObject\FullyQualifiedClassName; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
class CreateSection implements CreateSectionInterface |
26
|
|
|
{ |
27
|
|
|
/** @var CreateSectionInterface[] */ |
28
|
|
|
private $creators; |
29
|
|
|
|
30
|
|
|
/** @var EventDispatcherInterface */ |
31
|
|
|
private $dispatcher; |
32
|
|
|
|
33
|
|
|
/** @var CacheInterface */ |
34
|
|
|
private $cache; |
35
|
|
|
|
36
|
|
|
/** @var string[] */ |
37
|
|
|
private $invalidatedCaches = []; |
38
|
|
|
|
39
|
|
|
public function __construct( |
40
|
|
|
array $creators, |
41
|
|
|
EventDispatcherInterface $dispatcher, |
42
|
|
|
CacheInterface $cache |
43
|
|
|
) { |
44
|
|
|
$this->creators = $creators; |
45
|
|
|
$this->dispatcher = $dispatcher; |
46
|
|
|
$this->cache = $cache; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function save(CommonSectionInterface $sectionEntryEntity) |
53
|
|
|
{ |
54
|
|
|
$this->dispatcher->dispatch( |
55
|
|
|
SectionEntryBeforeCreate::NAME, |
56
|
|
|
new SectionEntryBeforeCreate($sectionEntryEntity) |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$update = !empty($sectionEntryEntity->getId()); |
60
|
|
|
|
61
|
|
|
/** @var CreateSectionInterface $writer */ |
62
|
|
|
foreach ($this->creators as $writer) { |
63
|
|
|
$writer->save($sectionEntryEntity); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
try { |
67
|
|
|
$this->cache->invalidateForSection( |
68
|
|
|
FullyQualifiedClassName::fromString(get_class($sectionEntryEntity)) |
69
|
|
|
); |
70
|
|
|
} catch (\Psr\Cache\InvalidArgumentException $exception) { |
71
|
|
|
// |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->dispatcher->dispatch( |
75
|
|
|
SectionEntryCreated::NAME, |
76
|
|
|
new SectionEntryCreated($sectionEntryEntity, $update) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function persist(CommonSectionInterface $sectionEntryEntity) |
84
|
|
|
{ |
85
|
|
|
/** @var CreateSectionInterface $writer */ |
86
|
|
|
foreach ($this->creators as $writer) { |
87
|
|
|
$writer->persist($sectionEntryEntity); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$class = get_class($sectionEntryEntity); |
91
|
|
|
if (!in_array($class, $this->invalidatedCaches)) { |
92
|
|
|
$this->invalidatedCaches[] = $class; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function flush() |
100
|
|
|
{ |
101
|
|
|
/** @var CreateSectionInterface $writer */ |
102
|
|
|
foreach ($this->creators as $writer) { |
103
|
|
|
$writer->flush(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
foreach ($this->invalidatedCaches as $class) { |
107
|
|
|
try { |
108
|
|
|
$this->cache->invalidateForSection( |
109
|
|
|
FullyQualifiedClassName::fromString($class) |
110
|
|
|
); |
111
|
|
|
} catch (\Psr\Cache\InvalidArgumentException $exception) { |
112
|
|
|
// |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
$this->invalidatedCaches = []; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|