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\Factory\RouteFactoryInterface; |
20
|
|
|
use SWP\Bundle\ContentBundle\Form\Type\RouteType; |
21
|
|
|
use SWP\Bundle\ContentBundle\Model\RouteInterface; |
22
|
|
|
use SWP\Bundle\ContentBundle\Model\RouteRepositoryInterface; |
23
|
|
|
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface; |
24
|
|
|
use SWP\Bundle\ContentBundle\Service\RouteServiceInterface; |
25
|
|
|
use SWP\Bundle\CoreBundle\Model\ArticleInterface; |
26
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
27
|
|
|
|
28
|
|
|
class ThemeRoutesGenerator implements GeneratorInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var RouteServiceInterface |
32
|
|
|
*/ |
33
|
|
|
protected $routeService; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var RouteRepositoryInterface |
37
|
|
|
*/ |
38
|
|
|
protected $routeRepository; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var RouteProviderInterface |
42
|
|
|
*/ |
43
|
|
|
protected $routeProvider; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var RouteFactoryInterface |
47
|
|
|
*/ |
48
|
|
|
protected $routeFactory; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var FormFactoryInterface |
52
|
|
|
*/ |
53
|
|
|
protected $formFactory; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var FakeArticlesGeneratorInterface |
57
|
|
|
*/ |
58
|
|
|
protected $fakeArticlesGenerator; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* ThemeRoutesGenerator constructor. |
62
|
|
|
* |
63
|
|
|
* @param RouteServiceInterface $routeService |
64
|
|
|
* @param RouteRepositoryInterface $routeRepository |
65
|
|
|
* @param RouteProviderInterface $routeProvider |
66
|
|
|
* @param RouteFactoryInterface $routeFactory |
67
|
|
|
* @param FormFactoryInterface $formFactory |
68
|
|
|
* @param FakeArticlesGeneratorInterface $fakeArticlesGenerator |
69
|
|
|
*/ |
70
|
|
|
public function __construct(RouteServiceInterface $routeService, RouteRepositoryInterface $routeRepository, RouteProviderInterface $routeProvider, RouteFactoryInterface $routeFactory, FormFactoryInterface $formFactory, FakeArticlesGeneratorInterface $fakeArticlesGenerator) |
71
|
|
|
{ |
72
|
|
|
$this->routeService = $routeService; |
73
|
|
|
$this->routeRepository = $routeRepository; |
74
|
|
|
$this->routeProvider = $routeProvider; |
75
|
|
|
$this->routeFactory = $routeFactory; |
76
|
|
|
$this->formFactory = $formFactory; |
77
|
|
|
$this->fakeArticlesGenerator = $fakeArticlesGenerator; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function generate(array $routes): void |
84
|
|
|
{ |
85
|
|
|
foreach ($routes as $routeData) { |
86
|
|
|
$cleanRouteData = $routeData; |
87
|
|
|
unset($cleanRouteData['numberOfArticles']); |
88
|
|
|
$route = $this->createRoute($cleanRouteData); |
89
|
|
|
if (null !== $this->routeProvider->getOneByStaticPrefix($route->getStaticPrefix())) { |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->processFakeArticles($route, $routeData); |
94
|
|
|
|
95
|
|
|
$this->routeRepository->add($route); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param array $routeData |
101
|
|
|
* |
102
|
|
|
* @return RouteInterface |
103
|
|
|
* |
104
|
|
|
* @throws \Exception |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
protected function createRoute(array $routeData): RouteInterface |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
/** @var RouteInterface $route */ |
109
|
|
|
$route = $this->routeFactory->create(); |
110
|
|
|
|
111
|
|
|
if (null !== $routeData['parent']) { |
112
|
|
|
if (null !== $parent = $this->routeProvider->getRouteByName($routeData['parent'])) { |
113
|
|
|
$route->setParent($parent); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
unset($routeData['parent']); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$form = $this->formFactory->create(RouteType::class, $route); |
120
|
|
|
$form->submit($routeData, false); |
121
|
|
|
|
122
|
|
|
if ($form->isValid()) { |
123
|
|
|
$this->routeService->createRoute($route); |
124
|
|
|
} else { |
125
|
|
|
throw new \Exception('Invalid route definition'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $route; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param RouteInterface $route |
133
|
|
|
* @param array $routeData |
134
|
|
|
*/ |
135
|
|
|
protected function processFakeArticles(RouteInterface $route, array $routeData) |
136
|
|
|
{ |
137
|
|
|
if (null !== $routeData['numberOfArticles']) { |
138
|
|
|
$articles = $this->fakeArticlesGenerator->generate($routeData['numberOfArticles']); |
139
|
|
|
/** @var ArticleInterface $article */ |
140
|
|
|
foreach ($articles as $article) { |
141
|
|
|
$route->addArticle($article); |
142
|
|
|
$article->setRoute($route); |
143
|
|
|
$this->routeRepository->persist($article); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
unset($routeData['numberOfArticles']); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
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.