88use Doctrine \ORM \NonUniqueResultException ;
99use Doctrine \Persistence \ManagerRegistry ;
1010use Liip \ImagineBundle \Service \FilterService ;
11+ use Psr \Log \LoggerInterface ;
1112use function Safe \sprintf ;
1213use Setono \SyliusImageOptimizerPlugin \Factory \ImageFileFactoryInterface ;
1314use Setono \SyliusImageOptimizerPlugin \ImageFile \ImageFile ;
@@ -46,20 +47,25 @@ final class OptimizeImageHandler implements MessageHandlerInterface
4647 /** @var MessageBusInterface */
4748 private $ eventBus ;
4849
50+ /** @var LoggerInterface */
51+ private $ logger ;
52+
4953 public function __construct (
5054 OptimizerInterface $ optimizer ,
5155 ManagerRegistry $ managerRegistry ,
5256 FilterService $ filterService ,
5357 OptimizedImageWriterInterface $ optimizedImageWriter ,
5458 ImageFileFactoryInterface $ imageFileFactory ,
55- MessageBusInterface $ eventBus
59+ MessageBusInterface $ eventBus ,
60+ LoggerInterface $ logger
5661 ) {
5762 $ this ->optimizer = $ optimizer ;
5863 $ this ->managerRegistry = $ managerRegistry ;
5964 $ this ->filterService = $ filterService ;
6065 $ this ->optimizedImageWriter = $ optimizedImageWriter ;
6166 $ this ->imageFileFactory = $ imageFileFactory ;
6267 $ this ->eventBus = $ eventBus ;
68+ $ this ->logger = $ logger ;
6369 }
6470
6571 public function __invoke (OptimizeImage $ message ): void
@@ -73,23 +79,46 @@ public function __invoke(OptimizeImage $message): void
7379 ));
7480 }
7581
76- foreach ($ message ->getFilterSets () as $ filterSet ) {
77- $ url = $ this ->filterService ->getUrlOfFilteredImage ($ message ->getPath (), $ filterSet );
78- $ imageFile = $ this ->imageFileFactory ->createFromUrl ($ url );
79-
80- $ result = $ this ->optimizer ->optimize ($ imageFile );
81- $ this ->handleOptimizationResult ($ message ->getImageResource (), $ result , $ imageFile , $ message ->getPath (), $ filterSet );
82+ $ exceptions = [];
8283
83- if ($ this ->optimizer instanceof WebPOptimizerInterface) {
84- $ result = $ this ->optimizer ->optimizeAndConvertToWebP ($ imageFile );
85- $ this ->handleOptimizationResult ($ message ->getImageResource (), $ result , $ imageFile , $ message ->getPath (), $ filterSet );
84+ foreach ($ message ->getFilterSets () as $ filterSet ) {
85+ try {
86+ $ url = $ this ->filterService ->getUrlOfFilteredImage ($ message ->getPath (), $ filterSet );
87+ $ imageFile = $ this ->imageFileFactory ->createFromUrl ($ url );
88+
89+ $ result = $ this ->optimizer ->optimize ($ imageFile );
90+ $ this ->handleOptimizationResult (
91+ $ message ->getImageResource (), $ result , $ imageFile , $ message ->getPath (), $ filterSet
92+ );
93+
94+ if ($ this ->optimizer instanceof WebPOptimizerInterface) {
95+ $ result = $ this ->optimizer ->optimizeAndConvertToWebP ($ imageFile );
96+ $ this ->handleOptimizationResult (
97+ $ message ->getImageResource (), $ result , $ imageFile , $ message ->getPath (), $ filterSet
98+ );
99+ }
100+ } catch (\Throwable $ e ) {
101+ $ exceptions [] = $ e ;
86102 }
87103 }
88104
105+ // todo notice we are catching exceptions above meaning that an image can fail in optimization, but still be marked as optimized
106+ // todo this is done as an easy fix right now to solve the problem where a command keeps trying to optimize an image that fails
89107 $ image ->setOptimized ();
90108
91109 $ manager = $ this ->getManager ($ message ->getClass ());
92110 $ manager ->flush ();
111+
112+ if (count ($ exceptions ) > 0 ) {
113+ $ errors = array_map (static function (\Throwable $ e ): string {
114+ return $ e ->getMessage ();
115+ }, $ exceptions );
116+
117+ $ this ->logger ->error (sprintf (
118+ "An error occurred during optimization of image %s with id %s. Errors: \n%s " ,
119+ $ message ->getClass (), $ message ->getId (), '- ' . implode ("\n- " , $ errors )
120+ ));
121+ }
93122 }
94123
95124 private function handleOptimizationResult (
0 commit comments