1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\BusinessPageBundle\Builder; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
7
|
|
|
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage; |
8
|
|
|
use Victoire\Bundle\CoreBundle\Entity\View; |
9
|
|
|
use Victoire\Bundle\ViewReferenceBundle\Builder\BaseReferenceBuilder; |
10
|
|
|
use Victoire\Bundle\ViewReferenceBundle\Helper\ViewReferenceHelper; |
11
|
|
|
use Victoire\Bundle\ViewReferenceBundle\ViewReference\BusinessPageReference; |
12
|
|
|
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* BusinessPageReferenceBuilder. |
16
|
|
|
*/ |
17
|
|
|
class BusinessPageReferenceBuilder extends BaseReferenceBuilder |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param BusinessPage $businessPage |
21
|
|
|
* @param EntityManager $em |
22
|
|
|
* |
23
|
|
|
* @return BusinessPageReference|ViewReference |
24
|
|
|
*/ |
25
|
|
|
public function buildReference(View $businessPage, EntityManager $em) |
26
|
|
|
{ |
27
|
|
|
$businessEntity = $businessPage->getEntityProxy()->getBusinessEntity(); |
|
|
|
|
28
|
|
|
$entity = $businessPage->getEntityProxy()->getEntity(); |
|
|
|
|
29
|
|
|
$accessor = new PropertyAccessor(); |
30
|
|
|
$entityId = null; |
31
|
|
View Code Duplication |
if ($entity) { |
|
|
|
|
32
|
|
|
if (method_exists($entity, 'getId')) { |
33
|
|
|
$entityId = $entity->getId(); |
34
|
|
|
} else { |
35
|
|
|
$entityId = $accessor->getValue($entity, $businessEntity->getBusinessIdentifiers()->first()->getName()); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
$referenceId = ViewReferenceHelper::generateViewReferenceId($businessPage); |
39
|
|
|
$businessPageReference = new BusinessPageReference(); |
40
|
|
|
$businessPageReference->setId($referenceId); |
41
|
|
|
$businessPageReference->setLocale($businessPage->getCurrentLocale()); |
42
|
|
|
$businessPageReference->setName($businessPage->getName()); |
43
|
|
|
$businessPageReference->setViewId($businessPage->getId()); |
44
|
|
|
$businessPageReference->setTemplateId($businessPage->getTemplate()->getId()); |
|
|
|
|
45
|
|
|
$businessPageReference->setSlug($businessPage->getSlug()); |
46
|
|
|
$businessPageReference->setEntityId($entityId); |
47
|
|
|
$businessPageReference->setBusinessEntity($businessEntity->getId()); |
48
|
|
|
$businessPageReference->setViewNamespace($em->getClassMetadata(get_class($businessPage))->name); |
49
|
|
|
if ($parent = $businessPage->getParent()) { |
50
|
|
|
$parent->setCurrentLocale($businessPage->getCurrentLocale()); |
51
|
|
|
$businessPageReference->setParent(ViewReferenceHelper::generateViewReferenceId($parent)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $businessPageReference; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: