1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\ViewReferenceBundle\Helper; |
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\CoreBundle\Entity\WebViewInterface; |
10
|
|
|
use Victoire\Bundle\I18nBundle\Entity\ViewTranslation; |
11
|
|
|
use Victoire\Bundle\ViewReferenceBundle\Builder\ViewReferenceBuilder; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ref: victoire_view_reference.helper. |
15
|
|
|
*/ |
16
|
|
|
class ViewReferenceHelper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ViewReferenceBuilder |
20
|
|
|
*/ |
21
|
|
|
private $viewReferenceBuilder; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor. |
25
|
|
|
* |
26
|
|
|
* @param ViewReferenceBuilder $viewReferenceBuilder |
27
|
|
|
*/ |
28
|
|
|
public function __construct(ViewReferenceBuilder $viewReferenceBuilder) |
29
|
|
|
{ |
30
|
|
|
$this->viewReferenceBuilder = $viewReferenceBuilder; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param View $view |
35
|
|
|
* @param mixed $entityId |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public static function generateViewReferenceId(View $view, $entityId = null) |
40
|
|
|
{ |
41
|
|
|
$id = $view->getId(); |
42
|
|
|
if ($view instanceof BusinessPage && $view->getEntity()) { |
43
|
|
|
$id = $view->getTemplate()->getId(); |
44
|
|
|
$accessor = new PropertyAccessor(); |
45
|
|
|
$entity = $view->getEntity(); |
46
|
|
View Code Duplication |
if (method_exists($entity, 'getId')) { |
|
|
|
|
47
|
|
|
$entityId = $entity->getId(); |
48
|
|
|
} else { |
49
|
|
|
$entityId = $accessor->getValue($entity, $view->getBusinessEntity()->getBusinessIdentifiers()->first()->getName()); |
50
|
|
|
} |
51
|
|
|
} elseif (!$view instanceof WebViewInterface) { |
52
|
|
|
return $view->getId(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$refId = sprintf('ref_%s', $id); |
56
|
|
|
if ($entityId) { |
57
|
|
|
$refId .= '_'.$entityId; |
58
|
|
|
} |
59
|
|
|
/** @var string $currentLocale */ |
60
|
|
|
if ('' != $currentLocale = $view->getCurrentLocale()) { |
61
|
|
|
$refId .= '_'.$currentLocale; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $refId; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
|
|
|
|
68
|
|
|
* @param WebViewInterface[] $tree |
69
|
|
|
*/ |
70
|
|
|
public function buildViewReferenceRecursively($tree, EntityManager $entityManager, $isRoot = true) |
71
|
|
|
{ |
72
|
|
|
foreach ($tree as $branch) { |
73
|
|
|
/** @var View $view */ |
74
|
|
|
$view = $branch['view']; |
75
|
|
|
/** @var ViewTranslation $translation */ |
76
|
|
|
foreach ($view->getTranslations() as $translation) { |
77
|
|
|
if (true === $isRoot || $translation->getLocale() == $view->getParent()->getCurrentLocale()) { |
78
|
|
|
$view->setCurrentLocale($translation->getLocale()); |
79
|
|
|
$viewReference = $this->viewReferenceBuilder->buildViewReference($view, $entityManager); |
80
|
|
|
if (!empty($branch['children'])) { |
81
|
|
|
/** @var WebViewInterface $children */ |
82
|
|
|
$children = $branch['children']; |
83
|
|
|
$this->buildViewReferenceRecursively($children, $entityManager, false); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
$view->setReference($viewReference, $translation->getLocale()); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.