| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Superdesk Web Publisher Core Bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Copyright 2018 Sourcefabric z.ú. and contributors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please see the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * AUTHORS and LICENSE files distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @copyright 2018 Sourcefabric z.ú. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @license http://www.superdesk.org/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | namespace SWP\Bundle\CoreBundle\Serializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Doctrine\Common\Collections\ArrayCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use JMS\Serializer\EventDispatcher\EventSubscriberInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use JMS\Serializer\EventDispatcher\PreSerializeEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use SWP\Bundle\ContentBundle\Model\ImageRenditionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use SWP\Bundle\CoreBundle\Model\ArticleMedia; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use SWP\Bundle\CoreBundle\Model\ArticleMediaInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | final class ArticleMediaSerializationSubscriber implements EventSubscriberInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private const VIEW_IMAGE = 'viewImage'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private const THUMBNAIL = 'thumbnail'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $thumbnailRenditionName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $viewImageRenditionName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function __construct(string $thumbnailRenditionName, string $viewImageRenditionName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->thumbnailRenditionName = $thumbnailRenditionName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->viewImageRenditionName = $viewImageRenditionName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public static function getSubscribedEvents(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 'event' => 'serializer.pre_serialize', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 'class' => ArticleMedia::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 'method' => 'onPreSerialize', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     public function onPreSerialize(PreSerializeEvent $event): void | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         /** @var ArticleMediaInterface $data */ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $data = $event->getObject(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         /** @var ImageRenditionInterface[] $renditions */ | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         if (null !== ($renditions = $data->getRenditions()) && count($renditions) > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $thumbnailRendition = $this->copyRendition($data, $this->thumbnailRenditionName, self::THUMBNAIL); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             if ($thumbnailRendition) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                 $data->addRendition($thumbnailRendition); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             $viewImageRendition = $this->copyRendition($data, $this->viewImageRenditionName, self::VIEW_IMAGE); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             if ($viewImageRendition) { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                 $data->addRendition($viewImageRendition); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     private function copyRendition(ArticleMediaInterface $media, string $from, string $to): ?ImageRenditionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         // check if rendition we want to create don't exists already in package | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $existingRendition = $media->getRenditions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ->filter(static function ($rendition) use ($to) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 /* @var ImageRenditionInterface $rendition */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 return $to === $rendition->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         if (0 !== count($existingRendition)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         /** @var ArrayCollection<ImageRenditionInterface> $searchedRenditions */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $searchedRenditions = $media->getRenditions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             ->filter(static function ($rendition) use ($from) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 /* @var ImageRenditionInterface $rendition */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 return $rendition->getName() === $from; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if (0 === count($searchedRenditions)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $copiedRendition = clone $searchedRenditions->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $copiedRendition->setName($to); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return $copiedRendition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |  | 
            
                        
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.