| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Superdesk Web Publisher Content Bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright 2016 Sourcefabric z.ú. and contributors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please see the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * AUTHORS and LICENSE files distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright 2016 Sourcefabric z.ú | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license http://www.superdesk.org/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace SWP\Bundle\ContentBundle\Routing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Psr\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use SWP\Bundle\ContentBundle\Model\FileInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use SWP\Bundle\ContentBundle\Model\ImageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use SWP\Bundle\ContentBundle\Model\ImageRendition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use SWP\Bundle\ContentBundle\Model\ImageRenditionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use SWP\Bundle\ContentBundle\Model\PreviewUrlAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use SWP\Component\TemplatesSystem\Gimme\Meta\Meta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Symfony\Bundle\FrameworkBundle\Routing\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Symfony\Cmf\Component\Routing\VersatileGeneratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Symfony\Component\Routing\RequestContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | class MediaRouter extends Router implements VersatileGeneratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $mediaManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         ContainerInterface $container, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $resource, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         array $options = [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         RequestContext $context = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         ContainerInterface $parameters = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         LoggerInterface $logger = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         string $defaultLocale = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->mediaManager = $container->get('swp_content_bundle.manager.media'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 84 |  |         parent::__construct($container, $resource, $options, $context, $parameters, $logger, $defaultLocale); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 84 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function getRouteDebugMessage($meta, array $parameters = array()): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return 'Route for media '.$meta->getValues()->getId().' not found'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function supports($meta): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $meta instanceof Meta && ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $meta->getValues() instanceof ArticleMediaInterface || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $meta->getValues() instanceof ImageRenditionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |     /** @param Meta $meta */ | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     public function generate($meta, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         $item = $this->getItem($meta); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         if ($meta->getValues() instanceof ImageRenditionInterface && null !== ($previewUrl = $meta->getValues()->getPreviewUrl())) { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             return $previewUrl; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         if ($item instanceof PreviewUrlAwareInterface && null !== ($previewUrl = $item->getPreviewUrl())) { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             return $previewUrl; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         return  $this->mediaManager->getMediaPublicUrl($item); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     private function getItem($meta): ?FileInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if (!$meta instanceof Meta) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if (($rendition = $meta->getValues()) instanceof ImageRendition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             return $rendition->getImage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         if (($image = $meta->getValues()->getImage()) instanceof ImageInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             return $image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if (($file = $meta->getValues()->getFile()) instanceof FileInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             return $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  |