1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2017 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2017 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\Theme\Generator; |
18
|
|
|
|
19
|
|
|
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface; |
20
|
|
|
use SWP\Bundle\CoreBundle\Model\MenuItemInterface; |
21
|
|
|
use SWP\Bundle\MenuBundle\Doctrine\MenuItemRepositoryInterface; |
22
|
|
|
use SWP\Bundle\MenuBundle\Factory\MenuFactoryInterface; |
23
|
|
|
use SWP\Bundle\MenuBundle\Form\Type\MenuType; |
24
|
|
|
use SWP\Bundle\MenuBundle\Manager\MenuItemManagerInterface; |
25
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
26
|
|
|
|
27
|
|
|
class ThemeMenusGenerator implements GeneratorInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var RouteProviderInterface |
31
|
|
|
*/ |
32
|
|
|
protected $routeProvider; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var FormFactoryInterface |
36
|
|
|
*/ |
37
|
|
|
protected $formFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var MenuFactoryInterface |
41
|
|
|
*/ |
42
|
|
|
protected $menuFactory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var MenuItemRepositoryInterface |
46
|
|
|
*/ |
47
|
|
|
protected $menuRepository; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var MenuItemManagerInterface |
51
|
|
|
*/ |
52
|
|
|
protected $menuManager; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* ThemeMenusGenerator constructor. |
56
|
|
|
* |
57
|
|
|
* @param RouteProviderInterface $routeProvider |
58
|
|
|
* @param FormFactoryInterface $formFactory |
59
|
|
|
* @param MenuFactoryInterface $menuFactory |
60
|
|
|
* @param MenuItemRepositoryInterface $menuRepository |
61
|
|
|
* @param MenuItemManagerInterface $menuManager |
62
|
|
|
*/ |
63
|
|
|
public function __construct(RouteProviderInterface $routeProvider, FormFactoryInterface $formFactory, MenuFactoryInterface $menuFactory, MenuItemRepositoryInterface $menuRepository, MenuItemManagerInterface $menuManager) |
64
|
|
|
{ |
65
|
|
|
$this->routeProvider = $routeProvider; |
66
|
|
|
$this->formFactory = $formFactory; |
67
|
|
|
$this->menuFactory = $menuFactory; |
68
|
|
|
$this->menuRepository = $menuRepository; |
69
|
|
|
$this->menuManager = $menuManager; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function generate(array $menus, MenuItemInterface $parent = null): void |
76
|
|
|
{ |
77
|
|
|
foreach ($menus as $menuData) { |
78
|
|
|
if (null !== $this->menuRepository->findOneBy(['name' => $menuData['name'], 'parent' => $parent])) { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->handleMenu($menuData, $parent); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $menuData |
88
|
|
|
* @param MenuItemInterface|null $parent |
89
|
|
|
* |
90
|
|
|
* @throws \Exception |
91
|
|
|
*/ |
92
|
|
|
private function handleMenu(array $menuData, MenuItemInterface $parent = null) |
93
|
|
|
{ |
94
|
|
|
$children = null; |
95
|
|
|
if (isset($menuData['children']) && count($menuData['children']) > 0) { |
96
|
|
|
$children = $menuData['children']; |
97
|
|
|
} |
98
|
|
|
unset($menuData['children']); |
99
|
|
|
|
100
|
|
|
if (null !== $parent) { |
101
|
|
|
$menuData['parent'] = $parent->getId(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$menu = $this->createMenu($menuData); |
105
|
|
|
$this->menuRepository->add($menu); |
106
|
|
|
|
107
|
|
|
if (null !== $children) { |
108
|
|
|
$this->generate($children, $menu); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $menuData |
114
|
|
|
* |
115
|
|
|
* @return MenuItemInterface |
116
|
|
|
* |
117
|
|
|
* @throws \Exception |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
private function createMenu(array $menuData): MenuItemInterface |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
/** @var MenuItemInterface $menuItem */ |
122
|
|
|
$menuItem = $this->menuFactory->createItem($menuData['name']); |
123
|
|
|
|
124
|
|
|
if (null !== $menuData['route'] && null !== $route = $this->routeProvider->getRouteByName($menuData['route'])) { |
125
|
|
|
$menuItem->setRoute($route); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
unset($menuData['route']); |
128
|
|
|
|
129
|
|
|
$form = $this->formFactory->create(MenuType::class, $menuItem); |
130
|
|
|
$form->submit($menuData, false); |
131
|
|
|
if (!$form->isValid()) { |
132
|
|
|
throw new \Exception('Invalid menu definition'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->menuManager->update($menuItem); |
136
|
|
|
|
137
|
|
|
return $menuItem; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
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.