GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Translator   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 16
lcom 1
cbo 4
dl 0
loc 128
rs 10
c 2
b 1
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B setLocale() 0 25 3
C prepare() 0 76 12
1
<?php
2
3
namespace Application;
4
5
use Symfony\Bridge\Twig\Translation\TwigExtractor;
6
use Symfony\Component\Translation\MessageCatalogue;
7
use Symfony\Component\Finder\Finder;
8
use Symfony\Component\Yaml\Dumper;
9
use Silex\Application;
10
11
/**
12
 * @author Borut Balažek <[email protected]>
13
 */
14
class Translator
15
{
16
    protected $app;
17
18
    /**
19
     * @param Application $app
20
     */
21
    public function __construct(Application $app)
22
    {
23
        $this->app = $app;
24
    }
25
26
    /**
27
     * Sets the locale (if it can).
28
     *
29
     * @param string $locale
30
     */
31
    public function setLocale($locale)
32
    {
33
        $app = $this->app;
34
35
        $app['translator']->setLocale($locale);
36
37
        $localeMessagesFile = APP_DIR.'/locales/'.$app['locale'].'/messages.yml';
38
        if (file_exists($localeMessagesFile)) {
39
            $app['translator']->addResource(
40
                'yaml',
41
                $localeMessagesFile,
42
                $app['locale']
43
            );
44
        }
45
46
        $localeValidatorsFile = APP_DIR.'/locales/'.$app['locale'].'/validators.yml';
47
        if (file_exists($localeValidatorsFile)) {
48
            $app['translator']->addResource(
49
                'yaml',
50
                $localeValidatorsFile,
51
                $app['locale'],
52
                'validators'
53
            );
54
        }
55
    }
56
57
    /**
58
     * Prepares and finds all the translated and untranslated string in tempates and controllers.
59
     *
60
     * @param Application $app
61
     * @param string      $locale
62
     *
63
     * @return array
64
     */
65
    public function prepare(Application $app, $locale)
66
    {
67
        $templatesPath = APP_DIR.'/templates';
68
        $untranslatedMessagesFile = APP_DIR.'/locales/'.$locale.'/messages_untranslated.yml';
69
70
        $extractor = new TwigExtractor($app['twig']);
71
72
        /***** All translations *****/
73
        $catalogueAll = new MessageCatalogue($locale);
74
        $extractor->extract($templatesPath, $catalogueAll);
75
        $allMessages = $catalogueAll->all('messages');
76
77
        // String from controller, controller provider, etc.
78
        $finder = new Finder();
79
        $finder->files()->in(ROOT_DIR.'/src');
80
81
        foreach ($finder as $file) {
82
            $fileMessageStrings = array();
83
84
            $filePath = $file->getRealpath();
85
            $fileContent = file_get_contents($filePath);
86
87
            $pregMatch = "#->trans.*\(\s*'(.+?)(?=')#m";
88
            preg_match_all($pregMatch, $fileContent, $matches);
89
90
            $matches = $matches[1];
91
92
            if ($matches) {
93
                foreach ($matches as $match) {
94
                    $fileMessageStrings[] = $match;
95
                }
96
            }
97
98
            if (!empty($fileMessageStrings)) {
99
                foreach ($fileMessageStrings as $fileMessageString) {
100
                    if (!isset($allMessages[$fileMessageString])) {
101
                        $allMessages[$fileMessageString] = $fileMessageString;
102
                    }
103
                }
104
            }
105
        }
106
107
        /***** Already translated *****/
108
        $app['application.translator']->setLocale($locale);
109
        $translatedMessages = $app['translator']->getCatalogue($locale);
110
        $translatedMessages = $translatedMessages->all('messages');
111
112
        /***** Untranslated *****/
113
        $untranslatedMessages = array();
114
115
        if (!empty($allMessages)) {
116
            foreach ($allMessages as $singleMessageKey => $singleMessage) {
117
                if (!isset($translatedMessages[$singleMessageKey])) {
118
                    $untranslatedMessages[$singleMessageKey] = $singleMessage;
119
                }
120
            }
121
        }
122
123
        if (!empty($untranslatedMessages)) {
124
            $dumper = new Dumper();
125
126
            $yaml = $dumper->dump($untranslatedMessages, 1);
127
128
            if (file_exists($untranslatedMessagesFile)) {
129
                unlink($untranslatedMessagesFile);
130
            }
131
132
            file_put_contents($untranslatedMessagesFile, $yaml);
133
        }
134
135
        return array(
136
            'allMessages' => $allMessages,
137
            'translatedMessages' => $translatedMessages,
138
            'untranslatedMessages' => $untranslatedMessages,
139
        );
140
    }
141
}
142