/
ProductDetailFriendlyUrlDataProvider.php
78 lines (66 loc) · 2.63 KB
/
ProductDetailFriendlyUrlDataProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Shopsys\FrameworkBundle\Model\Product;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr\Join;
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrl;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataFactoryInterface;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface;
class ProductDetailFriendlyUrlDataProvider implements FriendlyUrlDataProviderInterface
{
protected const ROUTE_NAME = 'front_product_detail';
/**
* @var \Doctrine\ORM\EntityManagerInterface
*/
protected $em;
/**
* @var \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataFactoryInterface
*/
protected $friendlyUrlDataFactory;
/**
* @param \Doctrine\ORM\EntityManagerInterface $em
* @param \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataFactoryInterface $friendlyUrlDataFactory
*/
public function __construct(
EntityManagerInterface $em,
FriendlyUrlDataFactoryInterface $friendlyUrlDataFactory
) {
$this->em = $em;
$this->friendlyUrlDataFactory = $friendlyUrlDataFactory;
}
/**
* @param \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig $domainConfig
* @return \Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlData[]
*/
public function getFriendlyUrlData(DomainConfig $domainConfig): array
{
$queryBuilder = $this->em->createQueryBuilder()
->select('p.id, pt.name')
->distinct()
->from(Product::class, 'p')
->join('p.translations', 'pt', Join::WITH, 'pt.locale = :locale')
->setParameter('locale', $domainConfig->getLocale())
->leftJoin(
FriendlyUrl::class,
'f',
Join::WITH,
'p.id = f.entityId AND f.routeName = :routeName AND f.domainId = :domainId'
)
->setParameter('routeName', static::ROUTE_NAME)
->setParameter('domainId', $domainConfig->getId())
->where('f.entityId IS NULL AND pt.name IS NOT NULL');
$scalarData = $queryBuilder->getQuery()->getScalarResult();
$friendlyUrlsData = [];
foreach ($scalarData as $data) {
$friendlyUrlsData[] = $this->friendlyUrlDataFactory->createFromIdAndName($data['id'], $data['name']);
}
return $friendlyUrlsData;
}
/**
* @return string
*/
public function getRouteName(): string
{
return static::ROUTE_NAME;
}
}