<?php
namespace App\Classes\DataMapper\Document;
use Factory\SupportBundle\Classes\DataMapper\Ecommerce\PriceDataMapper;
use Factory\SupportBundle\Contract\DataMapper\AbstractDataMapper;
use Pimcore\Model\WebsiteSetting;
use Symfony\Component\HttpFoundation\Request;
class ProductListingDataMapper extends AbstractDataMapper
{
public function toArray($request)
{
$defaultImage = WebsiteSetting::getByName('defaultGroupedProductImage') ?
WebsiteSetting::getByName('defaultGroupedProductImage')->getData() :
null;
return [
"id" => $this->getId(),
"key" => $this->getKey(),
"name" => $this->getName(),
"image" => $this->getMainImage() ?? $defaultImage,
"href" => $this->getHref($request),
'minPrice' => (new PriceDataMapper($this->getOSMinPrice()))->toArray($request),
"shortDescription" => $this->getShortDescription(),
];
}
protected function getHref(Request $request)
{
$params = [];
$document = $request->attributes->get('contentDocument');
if ($document) {
$params['navigate_from_document'] = $document->getId();
}
$category = $this->getDocumentCategory($document);
if ($category) {
$params['navigated_from_object'] = $category->getId();
}
return $this->resource->getHref($params);
}
private function getDocumentCategory($document)
{
$filter = $document->getProperty('products_filter');
if (!$filter) {
return null;
}
return $filter->getParent();
}
}