1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\BusinessPageBundle\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
6
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
7
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntity; |
8
|
|
|
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The Business Entity Page repository. |
12
|
|
|
*/ |
13
|
|
|
class BusinessPageRepository extends EntityRepository |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Find the pagePatterns of the business entity. |
17
|
|
|
* |
18
|
|
|
* @param BusinessTemplate $pattern |
19
|
|
|
* @param object $entity | int $entityId |
20
|
|
|
* @param BusinessEntity $businessEntity |
21
|
|
|
* |
22
|
|
|
* @return mixed |
23
|
|
|
*/ |
24
|
|
|
public function findPageByBusinessEntityAndPattern(BusinessTemplate $pattern, $entity, BusinessEntity $businessEntity) |
25
|
|
|
{ |
26
|
|
View Code Duplication |
if (is_object($entity)) { |
|
|
|
|
27
|
|
|
$accessor = new PropertyAccessor(); |
28
|
|
|
if (method_exists($entity, 'getId')) { |
29
|
|
|
$entityId = $entity->getId(); |
30
|
|
|
} else { |
31
|
|
|
$entityId = $accessor->getValue($entity, $businessEntity->getBusinessIdentifiers()->first()->getName()); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
$qb = $this->createQueryBuilder('BusinessPage'); |
35
|
|
|
$qb->join('BusinessPage.entityProxy', 'proxy'); |
36
|
|
|
$qb->join('BusinessPage.template', 'template'); |
37
|
|
|
$qb->join('proxy.businessEntity', 'businessEntity'); |
38
|
|
|
|
39
|
|
|
$qb->where('template.id = :templateId'); |
40
|
|
|
|
41
|
|
|
$qb->andWhere('businessEntity.name = :entityName'); |
42
|
|
|
$qb->andWhere('proxy.ressourceId = :entityId'); |
43
|
|
|
|
44
|
|
|
$qb->setParameter(':templateId', $pattern); |
45
|
|
|
$qb->setParameter(':entityId', $entityId); |
|
|
|
|
46
|
|
|
$qb->setParameter(':entityName', $businessEntity->getName()); |
47
|
|
|
|
48
|
|
|
return $qb->getQuery()->getOneOrNullResult(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.