<?php
namespace Aqarmap\Bundle\ListingBundle\Twig;
use Aqarmap\Bundle\ListingBundle\Entity\Listing;
use Aqarmap\Bundle\MainBundle\Twig\ThumbnailExtension;
use Symfony\Component\Asset\Packages;
use Twig\TwigFunction;
use Vich\UploaderBundle\Twig\Extension\UploaderExtensionRuntime;
class ListingShareExtension extends \Twig\Extension\AbstractExtension
{
public $uploaderExtension;
/**
* @var Packages
*/
public $helper;
/**
* @var ThumbnailExtension
*/
public $thumbnailExtension;
public function __construct(UploaderExtensionRuntime $uploaderExtensionRuntime, Packages $helper, ThumbnailExtension $thumbnailExtension)
{
$this->uploaderExtension = $uploaderExtensionRuntime;
$this->helper = $helper;
$this->thumbnailExtension = $thumbnailExtension;
}
public function getSharingImage(Listing $listing)
{
if ($mainPhoto = $listing->getMainPhoto()) {
return $this
->thumbnailExtension
->getThumbnail(
$this->uploaderExtension->asset($mainPhoto->getFile(), 'file'),
'search-thumb'
)
;
}
if ($userLogo = $listing->getUser()->getLogo()) {
return $this
->thumbnailExtension
->getThumbnail(
$this->uploaderExtension->asset($userLogo, 'file'),
'search-thumb'
)
;
}
return $this->helper->getUrl('images/emblem.svg');
}
/**
* Gets the public path for the file associated with the uploadable object.
*
* @param string||object $obj The object
* @param string $fieldName The field name
* @param string $className The object's class. Mandatory if $obj can't be used to determine it
*
* @see https://github.com/aqarmap/moonshot/issues/3530
*
* @return string|null The public path or null if file not stored
*/
public function customVichUploaderAsset($obj, string $fieldName, ?string $className = null): ?string
{
if (\is_string($obj)) {
return str_replace('gaufrette://s3_storage/', '', $obj);
}
return $this->uploaderExtension->asset($obj, $fieldName, $className);
}
public function getFunctions()
{
return [
new TwigFunction('getSharingImage', [$this, 'getSharingImage']),
new TwigFunction('custom_vich_uploader_asset', [$this, 'customVichUploaderAsset']),
];
}
public function getName()
{
return 'aqarmap_listing_share_extension';
}
}