Test Failed
Push — main ( 6ea9a3...5960f6 )
by Rafael
04:57
created

TranslatorCollector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 25
dl 0
loc 89
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addTranslations() 0 6 2
A __construct() 0 3 1
A getAssets() 0 5 1
A getName() 0 3 1
A getWidgets() 0 13 1
A collect() 0 7 1
1
<?php
2
3
namespace Alxarafe\Lib\DebugBarCollector;
4
5
use Alxarafe\Lib\Trans;
6
use DebugBar\DataCollector\AssetProvider;
7
use DebugBar\DataCollector\DataCollector;
8
use DebugBar\DataCollector\Renderable;
9
use Symfony\Component\Translation\Translator;
10
11
class TranslatorCollector extends DataCollector implements Renderable, AssetProvider
12
{
13
    /**
14
     * Array containing the translations
15
     *
16
     * @var array
17
     */
18
    protected array $translations;
19
20
    /**
21
     * TranslationCollector constructor.
22
     *
23
     * @param Translator $translator
24
     */
25
    public function __construct()
26
    {
27
        $this->translations = [];
28
    }
29
30
    /**
31
     * Returns the unique name of the collector
32
     *
33
     * @return string
34
     */
35
    public function getName(): string
36
    {
37
        return 'translations';
38
    }
39
40
    /**
41
     * Returns a hash where keys are control names and their values
42
     * an array of options as defined in {@see DebugBar\JavascriptRenderer::addControl()}
43
     *
44
     * @return array
45
     */
46
    public function getWidgets(): array
47
    {
48
        return [
49
            'translations' => [
50
                'icon' => 'language',
51
                'tooltip' => 'Translations',
52
                'widget' => 'PhpDebugBar.Widgets.TranslationsWidget',
53
                'map' => 'translations',
54
                'default' => '[]',
55
            ],
56
            'translations:badge' => [
57
                'map' => 'translations.nb_statements',
58
                'default' => 0,
59
            ],
60
        ];
61
    }
62
63
    /**
64
     * Returns the needed assets
65
     *
66
     * @return array
67
     */
68
    public function getAssets(): array
69
    {
70
        return [
71
            'css' => constant('BASE_URL') . '/alxarafe/assets/debugbar/translations.css',
72
            'js' => constant('BASE_URL') . '/alxarafe/assets/debugbar/translations.js',
73
        ];
74
    }
75
76
    /**
77
     * Called by the DebugBar when data needs to be collected
78
     *
79
     * @return array Collected data
80
     */
81
    public function collect(): array
82
    {
83
        $this->addTranslations();
84
85
        return [
86
            'nb_statements' => count($this->translations),
87
            'translations' => $this->translations,
88
        ];
89
    }
90
91
    /**
92
     * Add a translation key to the collector
93
     */
94
    private function addTranslations()
95
    {
96
        foreach (Trans::getMissingStrings() as $key => $value) {
97
            $this->translations[] = [
98
                'key' => $key,
99
                'value' => $value,
100
            ];
101
        }
102
    }
103
}
104