Issues (281)

Branch: master

MediaLibrary/EventListener/MediaItemSubscriber.php (1 issue)

1
<?php
2
3
namespace Backend\Modules\MediaLibrary\EventListener;
4
5
use Backend\Modules\MediaLibrary\Manager\FileManager;
6
use Backend\Modules\MediaLibrary\Domain\MediaItem\MediaItem;
7
use Doctrine\Common\EventSubscriber;
8
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
0 ignored issues
show
The type Doctrine\Common\Persiste...vent\LifecycleEventArgs was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Doctrine\ORM\Event\PostPersistEventArgs;
10
use Doctrine\ORM\Event\PostRemoveEventArgs;
11
use Doctrine\ORM\Events;
12
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
13
use SimpleBus\Message\Bus\MessageBus;
14
15
/**
16
 * MediaItem Subscriber
17
 */
18
final class MediaItemSubscriber implements EventSubscriber
19
{
20
    /** @var CacheManager */
21
    protected $cacheManager;
22
23
    /** @var MessageBus */
24
    protected $commandBus;
25
26
    /** @var FileManager */
27 165
    protected $fileManager;
28
29
    public function __construct(
30
        FileManager $fileManager,
31
        CacheManager $cacheManager,
32 165
        MessageBus $commandBus
33 165
    ) {
34 165
        $this->fileManager = $fileManager;
35 165
        $this->cacheManager = $cacheManager;
36
        $this->commandBus = $commandBus;
37 165
    }
38
39
    public function getSubscribedEvents(): array
40 165
    {
41
        return [
42
            Events::postPersist,
43
            Events::postRemove,
44
        ];
45
    }
46
47
    private function deleteSource(MediaItem $mediaItem): void
48
    {
49
        $this->fileManager->deleteFile($mediaItem->getAbsolutePath());
50
    }
51
52
    private function deleteThumbnails(MediaItem $mediaItem): void
53
    {
54
        // We have an image, so we have thumbnails to delete
55
        if ($mediaItem->getType()->isImage()) {
56
            // Delete all thumbnails
57
            $this->cacheManager->remove($mediaItem->getWebPath());
58
        }
59
    }
60
61
    private function generateThumbnails(MediaItem $mediaItem): void
62
    {
63
        // We have an image, so we have a backend thumbnail to generate
64
        if ($mediaItem->getType()->isImage()) {
65
            // Generate Backend thumbnail
66
            $this->cacheManager->getBrowserPath($mediaItem->getWebPath(), 'media_library_backend_thumbnail');
67
        }
68 1
    }
69
70 1
    public function postPersist(PostPersistEventArgs $eventArgs): void
71 1
    {
72 1
        $entity = $eventArgs->getObject();
73
        if (!$entity instanceof MediaItem) {
74
            return;
75
        }
76
77
        $this->generateThumbnails($entity);
78
    }
79
80
    public function postRemove(PostRemoveEventArgs $eventArgs): void
81
    {
82
        $entity = $eventArgs->getObject();
83
        if (!$entity instanceof MediaItem) {
84
            return;
85
        }
86
87
        $this->deleteSource($entity);
88
        $this->deleteThumbnails($entity);
89
    }
90
}
91