Completed
Push — wip-platform ( c9fbdf...0e8c27 )
by
unknown
26:56
created

SilLabelTranslatorStrategy   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 67
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setPrefix() 0 6 1
A setFix() 0 11 2
A cleanStr() 0 9 1
A doResetFix() 0 7 2
A getLabel() 0 14 1
1
<?php
2
3
/*
4
 *
5
 * Copyright (C) 2015-2017 Libre Informatique
6
 *
7
 * This file is licenced under the GNU LGPL v3.
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Blast\Bundle\CoreBundle\Translator;
13
14
use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
15
16
/**
17
 * Class SilLabelTranslatorStrategy.
18
 *
19
 * Provides a specific label translation strategy for Librinfo.
20
 * It is based on UnderscoreLabelTranslatorStrategy, but without the context,
21
 * and labels are prefixed by "blast.label."
22
 *
23
 * i.e. isValid => blast.label.is_valid
24
 *
25
 * Type to find missing translation : bin/console blast:translations:extract -f YAML
26
 */
27
class SilLabelTranslatorStrategy implements LabelTranslatorStrategyInterface
28
{
29
    protected $namePrefix; /* may be the bundle name */
30
    protected $nameFix; /* may be the admin name */
31
    protected $nameFixSave; /* may be the admin name */
32
33
    public function __construct($prefix = 'Blast\Bundle\CoreBundle', $fix = 'CoreAdmin')
34
    {
35
        $this->setPrefix($prefix);
36
        $this->setFix($fix);
37
    }
38
39
    public function setPrefix($prefix): SilLabelTranslatorStrategy
40
    {
41
        $this->namePrefix = $this->cleanStr($prefix);
42
43
        return $this;
44
    }
45
46
    public function setFix($fix, $isTmp = false): SilLabelTranslatorStrategy
47
    {
48
        if ($isTmp) {
49
            $this->nameFixSave = $this->nameFix;
50
        }
51
52
        /* Warning last set is current ... */
53
        $this->nameFix = $this->cleanStr($fix);
54
55
        return $this;
56
    }
57
58
    public function cleanStr($str): string
59
    {
60
        $str = strtolower(str_replace('\\', '.', $str));
61
        /* user love \ */
62
        $str = str_replace('..', '.', $str);
63
        $str = str_replace(' ', '_', $str);
64
65
        return $str;
66
    }
67
68
    public function doResetFix()
69
    {
70
        if (isset($this->nameFixSave)) {
71
            $this->nameFix = $this->nameFixSave;
72
            $this->nameFixSave = null;
73
        }
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getLabel($label, $context = '', $type = ''): string
80
    {
81
        $label = str_replace('.', '_', $label);
82
        $label = $this->cleanStr($label); /* if there is still some \ */
83
        $label = sprintf('%s', strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $label)));
84
85
        //$resLabel = $this->namePrefix . '.' . $this->nameFix . '.' . $context . '.' . $label;
86
        //$this->doResetFix(); /* for $isTmp to true see setFix */
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
87
        $resLabel = $this->namePrefix . '.' . $label;
88
89
        $resLabel = $this->cleanStr($resLabel);
90
91
        return $resLabel;
92
    }
93
}
94