attachmentReverseSearch = $attachmentReverseSearch; $this->attachmentHelper = $attachmentHelper; } /** * Removes the file associated with the attachment, if the file associated with the attachment changes. * @param Attachment $attachment * @param PreUpdateEventArgs $event * * @PreUpdate */ public function preUpdateHandler(Attachment $attachment, PreUpdateEventArgs $event) { if ($event->hasChangedField('path')) { $file = new \SplFileInfo($this->attachmentHelper->placeholderToRealPath($event->getOldValue('path'))); $this->attachmentReverseSearch->deleteIfNotUsed($file); } } /** * Removes the file associated with the attachment, after the attachment was deleted. * * @param Attachment $attachment * @param LifecycleEventArgs $event * * @PostRemove */ public function postRemoveHandler(Attachment $attachment, LifecycleEventArgs $event) { $file = $this->attachmentHelper->attachmentToFile($attachment); //Only delete if the attachment has a valid file. if ($file !== null) { $this->attachmentReverseSearch->deleteIfNotUsed($file); } } }