<?php

namespace Webkul\DamBundle\Datasource;

use Webkul\DamBundle\Repository\AssetRepository;
use Webkul\DamBundle\Listener\ClassDefinationForCompatibility;

$versionCompatiblility = new ClassDefinationForCompatibility();
$versionCompatiblility->createClassAliases();
/**
 * Export Mapping datasource
 *
 * @author    Webkul
 * @copyright 2017 Webkul Software Pvt Ltd (http://www.webkul.com)
 */
class DamAssetDatasource extends \Datasource
{
    /** @var AssetRepository */
    protected $repository;

    /** @var CustomObjectIdHydrator */
    protected $hydrator;

    /** @var array */
    protected $parameters = [];

    /**
     * @param AssetRepository $om
     * @param HydratorInterface          $hydrator
     */
    public function __construct(AssetRepository $repository, \HydratorInterface $hydrator)
    {
        $this->repository = $repository;

        $this->hydrator = $hydrator;
    }

    
    /**
     * @param string $method the query builder creation method
     * @param array  $config the query builder creation config
     *
     * @return Datasource
     */
    protected function initializeQueryBuilder($method, array $config = [])
    {
        $this->qb = $this->repository->$method('wem');
       
        $this->qb->leftJoin('wem.medias', 'md');
        $this->qb->leftJoin('wem.tags', 'tg');
        $this->qb->leftJoin('wem.category', 'category');
        $this->qb->resetDQLPart('select');
        $this->qb->addSelect('MIN(md.filePath) as thumbnail');
        $this->qb->addSelect('wem.id, wem.code, wem.description, wem.starred, wem.createdAt, wem.updatedAt, wem.title, wem.alt');
        $this->qb->groupBy('wem.id');
        

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function getResults()
    {
        return $this->hydrator->hydrate($this->qb);
    }
}
