1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\EventListener\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\EventSubscriber; |
6
|
|
|
use Doctrine\DBAL\Platforms\SqlitePlatform; |
7
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
8
|
|
|
use Doctrine\ORM\UnitOfWork; |
9
|
|
|
// use Enqueue\Client\TraceableProducer; |
10
|
|
|
use Liip\ImagineBundle\Async\Commands; |
11
|
|
|
use Liip\ImagineBundle\Async\ResolveCache; |
12
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
13
|
|
|
use Liip\ImagineBundle\Service\FilterService; |
14
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\FileInterface; |
15
|
|
|
use Silverback\ApiComponentBundle\Entity\SortableInterface; |
16
|
|
|
use Silverback\ApiComponentBundle\Serializer\ApiNormalizer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class FileEntitySubscriber |
20
|
|
|
* @package Silverback\ApiComponentBundle\EventListener |
21
|
|
|
* @author Daniel West <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class EntitySubscriber implements EventSubscriber |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var CacheManager |
27
|
|
|
*/ |
28
|
|
|
private $imagineCacheManager; |
29
|
|
|
/** |
30
|
|
|
* @var FilterService |
31
|
|
|
*/ |
32
|
|
|
private $filterService; |
33
|
|
|
/** |
34
|
|
|
* @var ApiNormalizer |
35
|
|
|
*/ |
36
|
|
|
private $fileNormalizer; |
37
|
|
|
|
38
|
|
|
// private $producer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* FileListener constructor. |
42
|
|
|
* @param CacheManager $imagineCacheManager |
43
|
|
|
* @param FilterService $filterService |
44
|
|
|
* @param ApiNormalizer $fileNormalizer |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
CacheManager $imagineCacheManager, |
48
|
|
|
FilterService $filterService, |
49
|
|
|
ApiNormalizer $fileNormalizer //, |
50
|
|
|
// TraceableProducer $producer |
51
|
|
|
) { |
52
|
|
|
$this->imagineCacheManager = $imagineCacheManager; |
53
|
|
|
$this->fileNormalizer = $fileNormalizer; |
54
|
|
|
$this->filterService = $filterService; |
55
|
|
|
// $this->producer = $producer; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function getSubscribedEvents(): array |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
'onFlush' |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param OnFlushEventArgs $eventArgs |
70
|
|
|
* @throws \Enqueue\Rpc\TimeoutException |
71
|
|
|
* @throws \Doctrine\DBAL\DBALException |
72
|
|
|
*/ |
73
|
1 |
|
public function onFlush(OnFlushEventArgs $eventArgs): void |
74
|
|
|
{ |
75
|
1 |
|
$entityManager = $eventArgs->getEntityManager(); |
76
|
1 |
|
if ($entityManager->getConnection()->getDatabasePlatform() instanceof SqlitePlatform) { |
77
|
1 |
|
$entityManager->getConnection()->exec('PRAGMA foreign_keys = ON;'); |
78
|
|
|
} |
79
|
1 |
|
$unitOfWork = $entityManager->getUnitOfWork(); |
80
|
1 |
|
$this->processNewEntities($unitOfWork); |
81
|
1 |
|
$this->processUpdatedEntities($unitOfWork); |
82
|
1 |
|
$this->processDeletedEntities($unitOfWork); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param UnitOfWork $unitOfWork |
87
|
|
|
* @throws \Enqueue\Rpc\TimeoutException |
88
|
|
|
*/ |
89
|
1 |
|
private function processNewEntities(UnitOfWork $unitOfWork): void |
90
|
|
|
{ |
91
|
1 |
|
$newEntities = $unitOfWork->getScheduledEntityInsertions(); |
92
|
1 |
|
foreach ($newEntities as $entity) { |
93
|
1 |
|
if ($entity instanceof SortableInterface) { |
94
|
|
|
try { |
95
|
|
|
$entity->getSort(); |
96
|
|
|
} catch (\TypeError $e) { |
97
|
|
|
$entity->setSort($entity->calculateSort(true)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
if ( |
101
|
1 |
|
$entity instanceof FileInterface && |
102
|
1 |
|
$this->fileNormalizer->isImagineSupportedFile($entity->getFilePath()) |
103
|
|
|
) { |
104
|
1 |
|
$this->sendCommand($entity); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
1 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param UnitOfWork $unitOfWork |
111
|
|
|
* @throws \Enqueue\Rpc\TimeoutException |
112
|
|
|
*/ |
113
|
1 |
|
private function processUpdatedEntities(UnitOfWork $unitOfWork): void |
114
|
|
|
{ |
115
|
1 |
|
$updatedEntities = $unitOfWork->getScheduledEntityUpdates(); |
116
|
1 |
|
foreach ($updatedEntities as $entity) { |
117
|
|
|
if ($entity instanceof FileInterface) { |
118
|
|
|
$changes = $unitOfWork->getEntityChangeSet($entity); |
119
|
|
|
if (!\is_array($changes)) { |
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
if (array_key_exists('filePath', $changes)) { |
123
|
|
|
$fpChanges = $changes['filePath']; |
124
|
|
|
$previousValueForField = $fpChanges[0] ?? null; |
125
|
|
|
$newValueForField = $fpChanges[1] ?? null; |
126
|
|
|
if ($previousValueForField !== $newValueForField) { |
127
|
|
|
if ($this->fileNormalizer->isImagineSupportedFile($previousValueForField)) { |
128
|
|
|
$this->imagineCacheManager->remove($previousValueForField); |
129
|
|
|
} |
130
|
|
|
if ($this->fileNormalizer->isImagineSupportedFile($newValueForField)) { |
131
|
|
|
$this->createFilteredImages($entity); |
132
|
|
|
// $this->sendCommand($entity); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param UnitOfWork $unitOfWork |
142
|
|
|
*/ |
143
|
1 |
|
private function processDeletedEntities(UnitOfWork $unitOfWork): void |
144
|
|
|
{ |
145
|
1 |
|
$deletedEntities = $unitOfWork->getScheduledEntityDeletions(); |
146
|
1 |
|
foreach ($deletedEntities as $entity) { |
147
|
|
|
if ( |
148
|
|
|
$entity instanceof FileInterface && |
149
|
|
|
$this->fileNormalizer->isImagineSupportedFile($entity->getFilePath()) |
150
|
|
|
) { |
151
|
|
|
$this->imagineCacheManager->remove($entity->getFilePath()); |
152
|
|
|
} |
153
|
|
|
} |
154
|
1 |
|
} |
155
|
|
|
|
156
|
|
|
private function createFilteredImages(FileInterface $file): void |
157
|
|
|
{ |
158
|
|
|
$filters = $file::getImagineFilters(); |
159
|
|
|
foreach ($filters as $filter) { |
160
|
|
|
$this->filterService->getUrlOfFilteredImage($file->getFilePath(), $filter); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/* |
165
|
|
|
private function sendCommand(FileInterface $file): void |
166
|
|
|
{ |
167
|
|
|
$this->producer |
168
|
|
|
->sendCommand( |
169
|
|
|
Commands::RESOLVE_CACHE, |
170
|
|
|
new ResolveCache($file->getFilePath(), $file::getImagineFilters()), |
171
|
|
|
true |
172
|
|
|
) |
173
|
|
|
->receive(20000) |
174
|
|
|
; |
175
|
|
|
} |
176
|
|
|
*/ |
177
|
|
|
} |
178
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.