2828use App \Entity \HasId ;
2929use Doctrine \Common \Util \ClassUtils ;
3030use Doctrine \ORM \EntityManagerInterface ;
31- use Doctrine \ORM \Event \OnFlushEventArgs ;
3231use Doctrine \ORM \Event \PreUpdateEventArgs ;
3332use Doctrine \ORM \Mapping \AssociationMapping ;
3433use Doctrine \ORM \Mapping \ClassMetadata ;
3938/**
4039 * Purges responses containing modified entities from the proxy cache.
4140 */
42- final class PurgeHttpCacheListener {
41+ final readonly class PurgeHttpCacheListener {
4342 use ClassInfoTrait;
4443
4544 public const IRI_RELATION_DELIMITER = '# ' ;
4645
47- public function __construct (private readonly IriConverterInterface $ iriConverter , private readonly ResourceClassResolverInterface $ resourceClassResolver , private readonly PropertyAccessorInterface $ propertyAccessor , private readonly ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory , private readonly CacheManager $ cacheManager ) {}
46+ public function __construct (
47+ private IriConverterInterface $ iriConverter ,
48+ private ResourceClassResolverInterface $ resourceClassResolver ,
49+ private PropertyAccessorInterface $ propertyAccessor ,
50+ private ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory ,
51+ private CacheManager $ cacheManager ,
52+ private EntityManagerInterface $ em ,
53+ ) {}
4854
4955 /**
5056 * Collects tags from the previous and the current version of the updated entities to purge related documents.
@@ -72,32 +78,25 @@ public function preUpdate(PreUpdateEventArgs $eventArgs): void {
7278 /**
7379 * Collects tags from inserted, updated and deleted entities, including relations.
7480 */
75- public function onFlush (OnFlushEventArgs $ eventArgs ): void {
76- /** @var EntityManagerInterface */
77- $ em = $ eventArgs ->getObjectManager ();
78-
79- if (!$ em instanceof EntityManagerInterface) {
80- return ;
81- }
82-
83- $ uow = $ em ->getUnitOfWork ();
81+ public function onFlush (): void {
82+ $ uow = $ this ->em ->getUnitOfWork ();
8483
8584 foreach ($ uow ->getScheduledEntityInsertions () as $ entity ) {
86- $ this ->gatherResourceTags ($ em , $ entity );
87- $ this ->gatherRelationTags ($ em , $ entity );
85+ $ this ->gatherResourceTags ($ entity );
86+ $ this ->gatherRelationTags ($ entity );
8887 }
8988
9089 foreach ($ uow ->getScheduledEntityUpdates () as $ entity ) {
91- $ originalEntity = $ this ->getOriginalEntity ($ entity, $ em );
90+ $ originalEntity = $ this ->getOriginalEntity ($ entity );
9291 $ this ->addTagForItem ($ entity );
93- $ this ->gatherResourceTags ($ em , $ entity , $ originalEntity );
92+ $ this ->gatherResourceTags ($ entity , $ originalEntity );
9493 }
9594
9695 foreach ($ uow ->getScheduledEntityDeletions () as $ entity ) {
97- $ originalEntity = $ this ->getOriginalEntity ($ entity, $ em );
96+ $ originalEntity = $ this ->getOriginalEntity ($ entity );
9897 $ this ->addTagForItem ($ originalEntity );
99- $ this ->gatherResourceTags ($ em , $ originalEntity );
100- $ this ->gatherRelationTags ($ em , $ originalEntity );
98+ $ this ->gatherResourceTags ($ originalEntity );
99+ $ this ->gatherRelationTags ($ originalEntity );
101100 }
102101
103102 // trigger cache purges for changes on many-to-many relations
@@ -138,13 +137,13 @@ private function addTagsForManyToManyRelations($collection, $entities) {
138137 /**
139138 * Computes the original state of the entity based on the current entity and on the changeset.
140139 */
141- private function getOriginalEntity ($ entity, $ em ) {
142- $ uow = $ em ->getUnitOfWork ();
140+ private function getOriginalEntity ($ entity ) {
141+ $ uow = $ this -> em ->getUnitOfWork ();
143142 $ changeSet = $ uow ->getEntityChangeSet ($ entity );
144- $ classMetadata = $ em ->getClassMetadata (ClassUtils::getClass ($ entity ));
143+ $ classMetadata = $ this -> em ->getClassMetadata (ClassUtils::getClass ($ entity ));
145144
146145 $ originalEntity = clone $ entity ;
147- $ em ->detach ($ originalEntity );
146+ $ this -> em ->detach ($ originalEntity );
148147 foreach ($ changeSet as $ key => $ value ) {
149148 $ classMetadata ->setFieldValue ($ originalEntity , $ key , $ value [0 ]);
150149 }
@@ -158,7 +157,7 @@ private function getOriginalEntity($entity, $em) {
158157 * If oldEntity is provided, purge is only done if the IRI of the collection has changed
159158 * (e.g. for updating period on a ScheduleEntry and the IRI changes from /periods/1/schedule_entries to /periods/2/schedule_entries)
160159 */
161- private function gatherResourceTags (EntityManagerInterface $ em , object $ entity , ?object $ oldEntity = null ): void {
160+ private function gatherResourceTags (object $ entity , ?object $ oldEntity = null ): void {
162161 $ entityClass = $ this ->getObjectClass ($ entity );
163162 if (!$ this ->resourceClassResolver ->isResourceClass ($ entityClass )) {
164163 return ;
@@ -168,7 +167,7 @@ private function gatherResourceTags(EntityManagerInterface $em, object $entity,
168167 $ this ->gatherResourceTagsForClass ($ resourceClass , $ entity , $ oldEntity );
169168
170169 // also purge parent classes (e.g. /content_nodes)
171- $ classMetadata = $ em ->getClassMetadata (ClassUtils::getClass ($ entity ));
170+ $ classMetadata = $ this -> em ->getClassMetadata (ClassUtils::getClass ($ entity ));
172171 foreach ($ classMetadata ->parentClasses as $ parentClass ) {
173172 $ this ->gatherResourceTagsForClass ($ parentClass , $ entity , $ oldEntity );
174173 }
@@ -221,8 +220,8 @@ private function invalidateCollection(GetCollection $operation, object $entity,
221220 *
222221 * @psalm-suppress UndefinedClass
223222 */
224- private function gatherRelationTags (EntityManagerInterface $ em , object $ entity ): void {
225- $ associationMappings = $ em ->getClassMetadata (ClassUtils::getClass ($ entity ))->getAssociationMappings ();
223+ private function gatherRelationTags (object $ entity ): void {
224+ $ associationMappings = $ this -> em ->getClassMetadata (ClassUtils::getClass ($ entity ))->getAssociationMappings ();
226225
227226 foreach ($ associationMappings as $ property => $ associationMapping ) {
228227 if ($ associationMapping instanceof AssociationMapping && ($ associationMapping ->targetEntity ?? null ) && !$ this ->resourceClassResolver ->isResourceClass ($ associationMapping ->targetEntity )) {
0 commit comments