1 | <?php |
||
38 | class ImageConversionConsumer implements ConsumerInterface |
||
39 | { |
||
40 | protected $serializer; |
||
41 | |||
42 | protected $logger; |
||
43 | |||
44 | protected $mediaManager; |
||
45 | |||
46 | protected $tenantContext; |
||
47 | |||
48 | protected $entityManager; |
||
49 | |||
50 | public function __construct( |
||
51 | SerializerInterface $serializer, |
||
52 | LoggerInterface $logger, |
||
53 | MediaManagerInterface $mediaManager, |
||
54 | TenantContextInterface $tenantContext, |
||
55 | EntityManagerInterface $entityManager |
||
56 | ) { |
||
57 | $this->serializer = $serializer; |
||
58 | $this->logger = $logger; |
||
59 | $this->mediaManager = $mediaManager; |
||
60 | $this->tenantContext = $tenantContext; |
||
61 | $this->entityManager = $entityManager; |
||
62 | } |
||
63 | |||
64 | public function execute(AMQPMessage $message): int |
||
65 | { |
||
66 | try { |
||
67 | ['renditionId' => $imageRenditionId, 'tenantId' => $tenantId] = unserialize($message->body, [false]); |
||
68 | if (($tenant = $this->entityManager->find(Tenant::class, $tenantId)) instanceof TenantInterface) { |
||
69 | $this->tenantContext->setTenant($tenant); |
||
70 | } |
||
71 | } catch (RuntimeException $e) { |
||
72 | $this->logger->error('Message REJECTED: '.$e->getMessage(), ['exception' => $e->getTraceAsString()]); |
||
73 | |||
74 | return ConsumerInterface::MSG_REJECT; |
||
75 | } |
||
76 | |||
77 | $imageRendition = $this->entityManager->find(ImageRendition::class, $imageRenditionId); |
||
78 | if (null !== $imageRendition) { |
||
79 | $mediaId = $imageRendition->getImage()->getAssetId(); |
||
80 | $tempLocation = rtrim(sys_get_temp_dir(), '/').DIRECTORY_SEPARATOR.sha1($mediaId); |
||
81 | |||
82 | try { |
||
83 | if (!function_exists('imagewebp')) { |
||
84 | throw new BadFunctionCallException('"imagewebp" function is missing. Looks like GD was compiled without webp support'); |
||
85 | } |
||
86 | imagewebp($this->getImageAsResource($imageRendition->getImage()), $tempLocation); |
||
87 | $uploadedFile = new UploadedFile($tempLocation, $mediaId, 'image/webp', strlen($tempLocation), null, true); |
||
88 | $this->mediaManager->saveFile($uploadedFile, $mediaId); |
||
89 | |||
90 | $this->logger->info(sprintf('File "%s" converted successfully to WEBP', $mediaId)); |
||
91 | |||
92 | $imageRendition->getImage()->addVariant(ImageInterface::VARIANT_WEBP); |
||
93 | $this->entityManager->flush(); |
||
94 | } catch (Exception $e) { |
||
95 | $this->logger->error('File NOT converted '.$e->getMessage(), ['exception' => $e->getTraceAsString()]); |
||
96 | |||
97 | return ConsumerInterface::MSG_REJECT; |
||
98 | } finally { |
||
99 | $filesystem = new Filesystem(); |
||
100 | if ($filesystem->exists($tempLocation)) { |
||
101 | $filesystem->remove($tempLocation); |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | |||
106 | return ConsumerInterface::MSG_ACK; |
||
107 | } |
||
108 | |||
109 | private function getImageAsResource(FileInterface $asset) |
||
132 | } |
||
133 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.