Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active December 11, 2020 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/b2b6b5685700f9b9383f1427d449cbe9 to your computer and use it in GitHub Desktop.
Save kurozumi/b2b6b5685700f9b9383f1427d449cbe9 to your computer and use it in GitHub Desktop.
ProductConverter
<?php
/**
* This file is part of Customize
*
* Copyright(c) Akira Kurozumi <info@a-zumi.net>
*
* https://a-zumi.net
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Customize\Request\ParamConverter;
use Doctrine\Common\Persistence\ManagerRegistry;
use Eccube\Repository\ProductRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Class ProductConverter
* @package Customize\Request\ParamConverter
*/
class ProductConverter implements ParamConverterInterface
{
/**
* @var ManagerRegistry
*/
private $registry;
/**
* ProductConverter constructor.
* @param ManagerRegistry $registry
*/
public function __construct(ManagerRegistry $registry)
{
$this->registry = $registry;
}
/**
* 商品IDでも商品スラッグでも商品情報が取得できるようにする
*
* @param Request $request
* @param ParamConverter $configuration
* @return bool
* @throws \ReflectionException
*/
public function apply(Request $request, ParamConverter $configuration)
{
$id = $this->getIdentifier($request, $configuration->getOptions());
if (false === $id || null === $id) {
return false;
}
$class = $configuration->getClass();
$om = $this->registry->getManagerForClass($class);
$repository = $om->getRepository($class);
if ($repository instanceof ProductRepository) {
$qb = $repository->createQueryBuilder('p');
$qb->addSelect(['pc', 'cc1', 'cc2', 'pi', 'pt'])
->innerJoin('p.ProductClasses', 'pc')
->leftJoin('pc.ClassCategory1', 'cc1')
->leftJoin('pc.ClassCategory2', 'cc2')
->leftJoin('p.ProductImage', 'pi')
->leftJoin('p.ProductTag', 'pt')
->where('pc.visible = :visible')
->setParameter('id', $id)
->setParameter('visible', true)
->orderBy('cc1.sort_no', 'DESC')
->addOrderBy('cc2.sort_no', 'DESC');
if (is_numeric($id)) {
// 商品IDの場合
$qb->andWhere('p.id = :id');
} else {
// 商品スラッグの場合
$qb->andWhere('p.slug = :id');
}
try {
$product = $qb
->getQuery()
->getSingleResult();
} catch (\Exception $e) {
throw new NotFoundHttpException();
}
// リクエストにProductというキー名で商品オブジェクトをセット
$request->attributes->set((new \ReflectionClass($class))->getShortName(), $product);
return true;
}
return false;
}
/**
* nameがproduct_detailのParamConverterに適用
*
* @param ParamConverter $configuration
* @return bool
*/
public function supports(ParamConverter $configuration)
{
return $configuration->getName() === "product_detail";
}
/**
* {id}の値を取得
*
* @param Request $request
* @param $options
* @return false|mixed
*/
protected function getIdentifier(Request $request, $options)
{
$name = "";
if (null !== $options['id']) {
if (!\is_array($options['id'])) {
$name = $options['id'];
}
}
if ($request->attributes->has($name)) {
return $request->attributes->get($name);
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment