src/Aqarmap/Bundle/ListingBundle/Twig/ListingShareExtension.php line 39

  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\Twig;
  3. use Aqarmap\Bundle\ListingBundle\Entity\Listing;
  4. use Aqarmap\Bundle\MainBundle\Twig\ThumbnailExtension;
  5. use Symfony\Component\Asset\Packages;
  6. use Twig\TwigFunction;
  7. use Vich\UploaderBundle\Twig\Extension\UploaderExtensionRuntime;
  8. class ListingShareExtension extends \Twig\Extension\AbstractExtension
  9. {
  10. public $uploaderExtension;
  11. /**
  12. * @var Packages
  13. */
  14. public $helper;
  15. /**
  16. * @var ThumbnailExtension
  17. */
  18. public $thumbnailExtension;
  19. public function __construct(UploaderExtensionRuntime $uploaderExtensionRuntime, Packages $helper, ThumbnailExtension $thumbnailExtension)
  20. {
  21. $this->uploaderExtension = $uploaderExtensionRuntime;
  22. $this->helper = $helper;
  23. $this->thumbnailExtension = $thumbnailExtension;
  24. }
  25. public function getSharingImage(Listing $listing)
  26. {
  27. if ($mainPhoto = $listing->getMainPhoto()) {
  28. return $this
  29. ->thumbnailExtension
  30. ->getThumbnail(
  31. $this->uploaderExtension->asset($mainPhoto->getFile(), 'file'),
  32. 'search-thumb'
  33. )
  34. ;
  35. }
  36. if ($userLogo = $listing->getUser()->getLogo()) {
  37. return $this
  38. ->thumbnailExtension
  39. ->getThumbnail(
  40. $this->uploaderExtension->asset($userLogo, 'file'),
  41. 'search-thumb'
  42. )
  43. ;
  44. }
  45. return $this->helper->getUrl('images/emblem.svg');
  46. }
  47. /**
  48. * Gets the public path for the file associated with the uploadable object.
  49. *
  50. * @param string||object $obj The object
  51. * @param string $fieldName The field name
  52. * @param string $className The object's class. Mandatory if $obj can't be used to determine it
  53. *
  54. * @see https://github.com/aqarmap/moonshot/issues/3530
  55. *
  56. * @return string|null The public path or null if file not stored
  57. */
  58. public function customVichUploaderAsset($obj, string $fieldName, ?string $className = null): ?string
  59. {
  60. if (\is_string($obj)) {
  61. return str_replace('gaufrette://s3_storage/', '', $obj);
  62. }
  63. return $this->uploaderExtension->asset($obj, $fieldName, $className);
  64. }
  65. public function getFunctions()
  66. {
  67. return [
  68. new TwigFunction('getSharingImage', $this->getSharingImage(...)),
  69. new TwigFunction('custom_vich_uploader_asset', $this->customVichUploaderAsset(...)),
  70. ];
  71. }
  72. public function getName()
  73. {
  74. return 'aqarmap_listing_share_extension';
  75. }
  76. }