<?php

namespace OfficeBrain\Bundle\SubShopBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * SubShopDesignRepository.
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class SubShopThemeDesignRepository extends EntityRepository
{
    public function add($subShopId, $mode)
    {
        $subShopThemeDesign = new SubShopThemeDesign();
        $subShopThemeDesign->setSubshopId($subShopId);
        $subShopThemeDesign->setMode($mode);
        $this->set($subShopThemeDesign);
    }

    public function set($subShopThemeDesign)
    {
        return $this->commit($subShopThemeDesign);
    }

    public function findByMode($subShopId, $mode)
    {
        return $this->createQueryBuilder('std')
                    ->select('std')
                    ->where('std.subshopId = :subshop_id')
                    ->andWhere('std.mode = :mode')
                    ->setParameter(':subshop_id', $subShopId)
                    ->setParameter(':mode', $mode)
                    ->getQuery()
                    ->getOneOrNullResult();
    }

    public function resetBySubShopId($subShopId)
    {
        return $this->createQueryBuilder('std')
             ->update()
             ->set('std.customDesign', 'NULL')
             ->where('std.subshopId = :subshopId')
             ->setParameter('subshopId', $subShopId)
             ->getQuery()
             ->execute();
    }
    /**
     * Commit entity.
     */
    private function commit($entity)
    {
        $this->_em->persist($entity);
        $this->_em->flush();

        return $entity->getId();
    }
}
