@@ -31,82 +31,82 @@ discard block |
||
31 | 31 | use Symfony\Component\Routing\RequestContext; |
32 | 32 | |
33 | 33 | class MediaRouter extends Router implements VersatileGeneratorInterface { |
34 | - private $mediaManager; |
|
35 | - |
|
36 | - const OBJECT_BASED_ROUTE_NAME = "__media_router_route_name__"; |
|
37 | - |
|
38 | - public function __construct( |
|
39 | - ContainerInterface $container, |
|
40 | - $resource, |
|
41 | - array $options = [], |
|
42 | - RequestContext $context = null, |
|
43 | - ContainerInterface $parameters = null, |
|
44 | - LoggerInterface $logger = null, |
|
45 | - string $defaultLocale = null |
|
46 | - ) { |
|
34 | + private $mediaManager; |
|
35 | + |
|
36 | + const OBJECT_BASED_ROUTE_NAME = "__media_router_route_name__"; |
|
37 | + |
|
38 | + public function __construct( |
|
39 | + ContainerInterface $container, |
|
40 | + $resource, |
|
41 | + array $options = [], |
|
42 | + RequestContext $context = null, |
|
43 | + ContainerInterface $parameters = null, |
|
44 | + LoggerInterface $logger = null, |
|
45 | + string $defaultLocale = null |
|
46 | + ) { |
|
47 | 47 | $this->mediaManager = $container->get('swp_content_bundle.manager.media'); |
48 | 48 | |
49 | 49 | parent::__construct($container, $resource, $options, $context, $parameters, $logger, $defaultLocale); |
50 | - } |
|
50 | + } |
|
51 | 51 | |
52 | - public function getRouteDebugMessage($meta, array $parameters = array()): string { |
|
52 | + public function getRouteDebugMessage($meta, array $parameters = array()): string { |
|
53 | 53 | if (self::OBJECT_BASED_ROUTE_NAME === $meta && array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)) { |
54 | - $meta = $parameters[RouteObjectInterface::ROUTE_OBJECT]; |
|
55 | - unset($parameters[RouteObjectInterface::ROUTE_OBJECT]); |
|
54 | + $meta = $parameters[RouteObjectInterface::ROUTE_OBJECT]; |
|
55 | + unset($parameters[RouteObjectInterface::ROUTE_OBJECT]); |
|
56 | 56 | } |
57 | 57 | return 'Route for media ' . $meta->getValues()->getId() . ' not found'; |
58 | - } |
|
58 | + } |
|
59 | 59 | |
60 | - public function supports($name): bool { |
|
60 | + public function supports($name): bool { |
|
61 | 61 | return (is_string($name) && $name == self::OBJECT_BASED_ROUTE_NAME) || |
62 | 62 | ($name instanceof Meta && ( |
63 | 63 | $name->getValues() instanceof ArticleMediaInterface || |
64 | 64 | $name->getValues() instanceof ImageRenditionInterface)); |
65 | - } |
|
65 | + } |
|
66 | 66 | |
67 | - public function generate($meta, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string { |
|
67 | + public function generate($meta, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string { |
|
68 | 68 | if (self::OBJECT_BASED_ROUTE_NAME === $meta && array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)) { |
69 | - $meta = $parameters[RouteObjectInterface::ROUTE_OBJECT]; |
|
70 | - unset($parameters[RouteObjectInterface::ROUTE_OBJECT]); |
|
69 | + $meta = $parameters[RouteObjectInterface::ROUTE_OBJECT]; |
|
70 | + unset($parameters[RouteObjectInterface::ROUTE_OBJECT]); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | if (!$meta instanceof Meta) { |
74 | - return ''; |
|
74 | + return ''; |
|
75 | 75 | } |
76 | 76 | |
77 | 77 | $item = $this->getItem($meta); |
78 | 78 | if (null === $item) { |
79 | - return ''; |
|
79 | + return ''; |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | if ($meta->getValues() instanceof ImageRenditionInterface && null !== ($previewUrl = $meta->getValues()->getPreviewUrl())) { |
83 | - return $previewUrl; |
|
83 | + return $previewUrl; |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | if ($item instanceof PreviewUrlAwareInterface && null !== ($previewUrl = $item->getPreviewUrl())) { |
87 | - return $previewUrl; |
|
87 | + return $previewUrl; |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | return $this->getUrlWithCorrectExtension($item, $parameters); |
91 | - } |
|
91 | + } |
|
92 | 92 | |
93 | - private function getItem(Meta $meta): ?FileInterface { |
|
93 | + private function getItem(Meta $meta): ?FileInterface { |
|
94 | 94 | if (($rendition = $meta->getValues()) instanceof ImageRendition) { |
95 | - return $rendition->getImage(); |
|
95 | + return $rendition->getImage(); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | if (($image = $meta->getValues()->getImage()) instanceof ImageInterface) { |
99 | - return $image; |
|
99 | + return $image; |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | if (($file = $meta->getValues()->getFile()) instanceof FileInterface) { |
103 | - return $file; |
|
103 | + return $file; |
|
104 | 104 | } |
105 | 105 | |
106 | 106 | return null; |
107 | - } |
|
107 | + } |
|
108 | 108 | |
109 | - private function getUrlWithCorrectExtension(FileInterface $item, array $parameters): string { |
|
109 | + private function getUrlWithCorrectExtension(FileInterface $item, array $parameters): string { |
|
110 | 110 | $url = $this->mediaManager->getMediaPublicUrl($item); |
111 | 111 | |
112 | 112 | if ( |
@@ -115,9 +115,9 @@ discard block |
||
115 | 115 | true === $parameters['webp'] && |
116 | 116 | $item->hasVariant(ImageInterface::VARIANT_WEBP) |
117 | 117 | ) { |
118 | - return str_replace('.' . $item->getFileExtension(), '.webp', $url); |
|
118 | + return str_replace('.' . $item->getFileExtension(), '.webp', $url); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | return $url; |
122 | - } |
|
122 | + } |
|
123 | 123 | } |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | $meta = $parameters[RouteObjectInterface::ROUTE_OBJECT]; |
55 | 55 | unset($parameters[RouteObjectInterface::ROUTE_OBJECT]); |
56 | 56 | } |
57 | - return 'Route for media ' . $meta->getValues()->getId() . ' not found'; |
|
57 | + return 'Route for media '.$meta->getValues()->getId().' not found'; |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | public function supports($name): bool { |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | true === $parameters['webp'] && |
116 | 116 | $item->hasVariant(ImageInterface::VARIANT_WEBP) |
117 | 117 | ) { |
118 | - return str_replace('.' . $item->getFileExtension(), '.webp', $url); |
|
118 | + return str_replace('.'.$item->getFileExtension(), '.webp', $url); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | return $url; |
@@ -34,87 +34,87 @@ discard block |
||
34 | 34 | use SWP\Component\Common\Exception\UnexpectedTypeException; |
35 | 35 | |
36 | 36 | final class DownloadArticleAuthorAvatarListener { |
37 | - /** |
|
38 | - * @var MediaManagerInterface |
|
39 | - */ |
|
40 | - private $authorMediaManager; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var EntityManagerInterface |
|
44 | - */ |
|
45 | - private $entityManager; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var string |
|
49 | - */ |
|
50 | - private $cacheDirectory; |
|
51 | - |
|
52 | - public function __construct(EntityManagerInterface $entityManager, $cacheDirectory) { |
|
37 | + /** |
|
38 | + * @var MediaManagerInterface |
|
39 | + */ |
|
40 | + private $authorMediaManager; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var EntityManagerInterface |
|
44 | + */ |
|
45 | + private $entityManager; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var string |
|
49 | + */ |
|
50 | + private $cacheDirectory; |
|
51 | + |
|
52 | + public function __construct(EntityManagerInterface $entityManager, $cacheDirectory) { |
|
53 | 53 | $this->entityManager = $entityManager; |
54 | 54 | $this->cacheDirectory = $cacheDirectory; |
55 | - } |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @param MediaManagerInterface $authorMediaManager |
|
59 | - */ |
|
60 | - public function setAuthorMediaManager(MediaManagerInterface $authorMediaManager) { |
|
57 | + /** |
|
58 | + * @param MediaManagerInterface $authorMediaManager |
|
59 | + */ |
|
60 | + public function setAuthorMediaManager(MediaManagerInterface $authorMediaManager) { |
|
61 | 61 | $this->authorMediaManager = $authorMediaManager; |
62 | - } |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * @param GenericEvent $event |
|
66 | - */ |
|
67 | - public function processAuthors(GenericEvent $event): void { |
|
64 | + /** |
|
65 | + * @param GenericEvent $event |
|
66 | + */ |
|
67 | + public function processAuthors(GenericEvent $event): void { |
|
68 | 68 | $package = $event->getSubject(); |
69 | 69 | |
70 | 70 | if (!$package instanceof PackageInterface) { |
71 | - throw new UnexpectedTypeException($package, PackageInterface::class); |
|
71 | + throw new UnexpectedTypeException($package, PackageInterface::class); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | $authors = []; |
75 | 75 | /** @var ArticleAuthorInterface $packageAuthor */ |
76 | 76 | foreach ($package->getAuthors() as $packageAuthor) { |
77 | - $authors[] = $this->handle($packageAuthor); |
|
77 | + $authors[] = $this->handle($packageAuthor); |
|
78 | 78 | } |
79 | 79 | $package->setAuthors(new ArrayCollection($authors)); |
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @param ArticleAuthorInterface $object |
|
84 | - * |
|
85 | - * @return ArticleAuthorInterface |
|
86 | - */ |
|
87 | - private function handle(ArticleAuthorInterface $object): ArticleAuthorInterface { |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @param ArticleAuthorInterface $object |
|
84 | + * |
|
85 | + * @return ArticleAuthorInterface |
|
86 | + */ |
|
87 | + private function handle(ArticleAuthorInterface $object): ArticleAuthorInterface { |
|
88 | 88 | if (null !== $object->getAvatarUrl()) { |
89 | - $filesystem = new Filesystem(); |
|
90 | - $pathParts = \pathinfo($object->getAvatarUrl()); |
|
91 | - $assetId = \sha1($pathParts['filename']); |
|
92 | - if (null !== $object->getSlug()) { |
|
89 | + $filesystem = new Filesystem(); |
|
90 | + $pathParts = \pathinfo($object->getAvatarUrl()); |
|
91 | + $assetId = \sha1($pathParts['filename']); |
|
92 | + if (null !== $object->getSlug()) { |
|
93 | 93 | $assetId = $object->getSlug() . '_' . $assetId; |
94 | - } |
|
95 | - $existingAvatar = $this->entityManager->getRepository(Image::class)->findBy(['assetId' => $assetId]); |
|
96 | - if (\count($existingAvatar) > 0) { |
|
94 | + } |
|
95 | + $existingAvatar = $this->entityManager->getRepository(Image::class)->findBy(['assetId' => $assetId]); |
|
96 | + if (\count($existingAvatar) > 0) { |
|
97 | 97 | $object->setAvatarUrl((string)\reset($existingAvatar)); |
98 | 98 | |
99 | 99 | return $object; |
100 | - } |
|
100 | + } |
|
101 | 101 | |
102 | - try { |
|
102 | + try { |
|
103 | 103 | $tempDirectory = $this->cacheDirectory . \DIRECTORY_SEPARATOR . 'downloaded_avatars'; |
104 | 104 | $tempLocation = $tempDirectory . \DIRECTORY_SEPARATOR . \sha1($assetId . date('his')); |
105 | 105 | if (!$filesystem->exists($tempDirectory)) { |
106 | - $filesystem->mkdir($tempDirectory); |
|
106 | + $filesystem->mkdir($tempDirectory); |
|
107 | 107 | } |
108 | 108 | |
109 | 109 | $avatarUrl = $object->getAvatarUrl(); |
110 | 110 | if (strpos($avatarUrl, "http://") === 0 || strpos($avatarUrl, "https://") === 0) { |
111 | - self::downloadFile($avatarUrl, $tempLocation); |
|
111 | + self::downloadFile($avatarUrl, $tempLocation); |
|
112 | 112 | } else { |
113 | - $file = \file_get_contents($avatarUrl); |
|
114 | - if (false === $file) { |
|
113 | + $file = \file_get_contents($avatarUrl); |
|
114 | + if (false === $file) { |
|
115 | 115 | throw new \Exception('File can\'t be downloaded'); |
116 | - } |
|
117 | - $filesystem->dumpFile($tempLocation, $file); |
|
116 | + } |
|
117 | + $filesystem->dumpFile($tempLocation, $file); |
|
118 | 118 | } |
119 | 119 | |
120 | 120 | $uploadedFile = new UploadedFile($tempLocation, |
@@ -123,29 +123,29 @@ discard block |
||
123 | 123 | \filesize($tempLocation), |
124 | 124 | true |
125 | 125 | ); |
126 | - } catch (\Exception $e) { |
|
126 | + } catch (\Exception $e) { |
|
127 | 127 | return $object; |
128 | - } |
|
129 | - /** @var Image $image */ |
|
130 | - $image = $this->authorMediaManager->handleUploadedFile($uploadedFile, $assetId); |
|
131 | - $avatar = $this->createAuthorMedia($object, $image); |
|
132 | - $this->entityManager->persist($avatar); |
|
133 | - $this->entityManager->persist($image); |
|
134 | - |
|
135 | - $object->setAvatar($avatar); |
|
136 | - $object->setAvatarUrl((string)$image); |
|
128 | + } |
|
129 | + /** @var Image $image */ |
|
130 | + $image = $this->authorMediaManager->handleUploadedFile($uploadedFile, $assetId); |
|
131 | + $avatar = $this->createAuthorMedia($object, $image); |
|
132 | + $this->entityManager->persist($avatar); |
|
133 | + $this->entityManager->persist($image); |
|
134 | + |
|
135 | + $object->setAvatar($avatar); |
|
136 | + $object->setAvatarUrl((string)$image); |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | return $object; |
140 | - } |
|
140 | + } |
|
141 | 141 | |
142 | - private function createAuthorMedia(ArticleAuthorInterface $articleAuthor, Image $image): AuthorMediaInterface { |
|
142 | + private function createAuthorMedia(ArticleAuthorInterface $articleAuthor, Image $image): AuthorMediaInterface { |
|
143 | 143 | return new AuthorMedia('avatar', $articleAuthor, $image); |
144 | - } |
|
144 | + } |
|
145 | 145 | |
146 | - private static function downloadFile($url, $location) { |
|
146 | + private static function downloadFile($url, $location) { |
|
147 | 147 | $handlerStack = HandlerStack::create(new CurlHandler()); |
148 | 148 | $client = new Client(['handler' => $handlerStack]); |
149 | 149 | $client->request('GET', $url, ['sink' => $location]); |
150 | - } |
|
150 | + } |
|
151 | 151 | } |
@@ -90,18 +90,18 @@ discard block |
||
90 | 90 | $pathParts = \pathinfo($object->getAvatarUrl()); |
91 | 91 | $assetId = \sha1($pathParts['filename']); |
92 | 92 | if (null !== $object->getSlug()) { |
93 | - $assetId = $object->getSlug() . '_' . $assetId; |
|
93 | + $assetId = $object->getSlug().'_'.$assetId; |
|
94 | 94 | } |
95 | 95 | $existingAvatar = $this->entityManager->getRepository(Image::class)->findBy(['assetId' => $assetId]); |
96 | 96 | if (\count($existingAvatar) > 0) { |
97 | - $object->setAvatarUrl((string)\reset($existingAvatar)); |
|
97 | + $object->setAvatarUrl((string) \reset($existingAvatar)); |
|
98 | 98 | |
99 | 99 | return $object; |
100 | 100 | } |
101 | 101 | |
102 | 102 | try { |
103 | - $tempDirectory = $this->cacheDirectory . \DIRECTORY_SEPARATOR . 'downloaded_avatars'; |
|
104 | - $tempLocation = $tempDirectory . \DIRECTORY_SEPARATOR . \sha1($assetId . date('his')); |
|
103 | + $tempDirectory = $this->cacheDirectory.\DIRECTORY_SEPARATOR.'downloaded_avatars'; |
|
104 | + $tempLocation = $tempDirectory.\DIRECTORY_SEPARATOR.\sha1($assetId.date('his')); |
|
105 | 105 | if (!$filesystem->exists($tempDirectory)) { |
106 | 106 | $filesystem->mkdir($tempDirectory); |
107 | 107 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | $this->entityManager->persist($image); |
134 | 134 | |
135 | 135 | $object->setAvatar($avatar); |
136 | - $object->setAvatarUrl((string)$image); |
|
136 | + $object->setAvatarUrl((string) $image); |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | return $object; |