Completed
Push — master ( 1ae041...757543 )
by Beñat
03:37
created

Translator::create()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 3
eloc 17
nc 4
nop 0
1
<?php
2
3
/*
4
 * This file is part of the WPSymfonyForm plugin.
5
 *
6
 * Copyright (c) 2015-2016 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\WPSymfonyForm\Translation;
13
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\Translation\Loader\XliffFileLoader;
16
use Symfony\Component\Translation\Translator as BaseTranslator;
17
18
/**
19
 * Translator singleton class.
20
 *
21
 * @author Gorka Laucirica <[email protected]>
22
 * @author Beñat Espiña <[email protected]>
23
 */
24
class Translator
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
25
{
26
    /**
27
     * The instance.
28
     *
29
     * @var self
30
     */
31
    private static $instance;
32
33
    /**
34
     * The factory method that returns the
35
     * instance of class in a singleton way.
36
     *
37
     * @return static
38
     */
39
    public static function instance()
40
    {
41
        if (!self::$instance) {
42
            self::$instance = static::create();
0 ignored issues
show
Documentation Bug introduced by
It seems like static::create() of type object<Symfony\Component\Translation\Translator> is incompatible with the declared type object<LIN3S\WPSymfonyFo...Translation\Translator> of property $instance.

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...
43
        }
44
45
        return self::$instance;
46
    }
47
48
    /**
49
     * Protected facade.
50
     *
51
     * @return Translator
52
     */
53
    protected static function create()
54
    {
55
        $locale = 'es_ES';
56
        if (defined('ICL_LANGUAGE_CODE')) {
57
            $locale = ICL_LANGUAGE_CODE;
58
        }
59
60
        $translator = new BaseTranslator($locale);
61
        $translator->addLoader('xlf', new XliffFileLoader());
62
63
        $finder = new Finder();
64
        $finder->files()->in(ABSPATH . '/../vendor/symfony/validator/Resources/translations/');
65
        foreach ($finder as $validator) {
66
            $locale = str_replace('validators.', '', $validator->getRelativePathName());
67
            $locale = str_replace('.xlf', '', $locale);
68
69
            $translator->addResource(
70
                'xlf',
71
                $validator->getRealpath(),
72
                $locale,
73
                'validators'
74
            );
75
        }
76
77
        return $translator;
78
    }
79
80
    /**
81
     * This class cannot be instantiated.
82
     */
83
    private function __construct()
84
    {
85
    }
86
}
87