These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\TranslatorBundle\Toolbar; |
||
4 | |||
5 | use Kunstmaan\AdminBundle\Helper\Toolbar\AbstractDataCollector; |
||
6 | use Symfony\Component\HttpFoundation\Request; |
||
7 | use Symfony\Component\HttpFoundation\Response; |
||
8 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
9 | |||
10 | class TranslatorDataCollector extends AbstractDataCollector |
||
11 | { |
||
12 | /** |
||
13 | * @var DataCollectorTranslator |
||
14 | */ |
||
15 | private $translator; |
||
16 | |||
17 | /** |
||
18 | * @var UrlGeneratorInterface |
||
19 | */ |
||
20 | private $urlGenerator; |
||
21 | |||
22 | /** |
||
23 | * TranslatorDataCollector constructor. |
||
24 | * |
||
25 | * @param DataCollectorTranslator $translator |
||
26 | * @param UrlGeneratorInterface $urlGenerator |
||
27 | */ |
||
28 | public function __construct(DataCollectorTranslator $translator, UrlGeneratorInterface $urlGenerator) |
||
29 | { |
||
30 | $this->translator = $translator; |
||
31 | $this->urlGenerator = $urlGenerator; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @return array |
||
36 | */ |
||
37 | public function getAccessRoles() |
||
38 | { |
||
39 | return ['ROLE_ADMIN']; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @return array |
||
44 | */ |
||
45 | public function collectData() |
||
46 | { |
||
47 | $route = $this->urlGenerator->generate('KunstmaanTranslatorBundle_settings_translations'); |
||
48 | |||
49 | $options = [ |
||
50 | 'filter_columnname' => [ |
||
51 | 'keyword', |
||
52 | ], |
||
53 | 'filter_uniquefilterid' => [ |
||
54 | 1, |
||
55 | ], |
||
56 | 'filter_comparator_1' => 'equals', |
||
57 | 'filter' => 'filter', |
||
58 | ]; |
||
59 | |||
60 | $translations = []; |
||
61 | |||
62 | foreach ($this->translator->getCollectedMessages() as $message) { |
||
63 | if ($message['state'] !== DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK && !empty($message['id'])) { |
||
64 | $options['filter_value_1'] = $message['id']; |
||
65 | $translations[$message['id']] = [ |
||
66 | 'id' => $message['id'], |
||
67 | 'message' => $message['translation'], |
||
68 | 'route' => $this->urlGenerator->generate('KunstmaanTranslatorBundle_settings_translations', $options), |
||
69 | ]; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | $data = [ |
||
74 | 'route' => $route, |
||
75 | 'translations' => $translations, |
||
76 | ]; |
||
77 | |||
78 | return ['data' => $data]; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param Request $request |
||
83 | * @param Response $response |
||
84 | * @param \Exception|null $exception |
||
85 | */ |
||
86 | View Code Duplication | public function collect(Request $request, Response $response, \Exception $exception = null) |
|
0 ignored issues
–
show
|
|||
87 | { |
||
88 | if (!$this->showDataCollection($request, $response) || !$this->isEnabled()) { |
||
89 | $this->data = false; |
||
90 | } else { |
||
91 | $this->data = $this->collectData(); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Gets the data for template |
||
97 | * |
||
98 | * @return array The request events |
||
99 | */ |
||
100 | public function getTemplateData() |
||
101 | { |
||
102 | return $this->data; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getName() |
||
109 | { |
||
110 | return 'kuma_translation'; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function isEnabled() |
||
117 | { |
||
118 | return true; |
||
119 | } |
||
120 | |||
121 | public function reset() |
||
122 | { |
||
123 | $this->data = []; |
||
124 | } |
||
125 | } |
||
126 |
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.