Completed
Push — master ( da86b5...1bc0f1 )
by Sander
19:35 queued 01:28
created

TranslatorDataCollector::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
36
     */
37
    public function getAccessRoles()
38
    {
39
        return ['ROLE_ADMIN'];
40
    }
41
42
    /**
43
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<string,string|array>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
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
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
87
    {
88
        if (!$this->showDataCollection($request, $response) || !$this->isEnabled()) {
89
            $this->data = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
90
        }
91
        else {
92
            $this->data = $this->collectData();
93
        }
94
    }
95
96
    /**
97
     * Gets the data for template
98
     *
99
     * @return array The request events
100
     */
101
    public function getTemplateData()
102
    {
103
        return $this->data;
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    public function getName()
110
    {
111
        return 'kuma_translation';
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    public function isEnabled()
118
    {
119
        return true;
120
    }
121
122
    public function reset()
123
    {
124
        $this->data = [];
125
    }
126
}
127