Conditions | 12 |
Paths | 20 |
Total Lines | 52 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
44 | protected function execute(InputInterface $input, OutputInterface $output) |
||
45 | { |
||
46 | $container = $this->getContainer(); |
||
47 | $locales = $container->getParameter('sleepness_uber_translation.supported_locales'); // prepare locales from parameters |
||
48 | $bundle = $container->get('kernel')->getBundle($input->getArgument('bundle')); // get bundle props |
||
49 | |||
50 | if (!is_dir($bundle->getPath() . '/Resources/translations')) { |
||
51 | $output->writeln("\033[37;43m There is no folder with translations in " . $input->getArgument('bundle') . " \033[0m"); |
||
52 | return; |
||
53 | } |
||
54 | |||
55 | $loader = $container->get('translation.loader'); // get translator loader |
||
56 | $uberMemcached = $container->get('uber.memcached'); // get uber memcached |
||
57 | $catalogues = array(); // prepare array for catalogues |
||
58 | |||
59 | foreach ($locales as $locale) { // run through locales |
||
60 | if (!preg_match('/^[a-z]{2}_[a-zA-Z]{2}$|[a-z]{2}/', $locale)) { |
||
61 | $output->writeln("\033[37;43m Make sure you define all locales properly \033[0m \n"); |
||
62 | return; |
||
63 | } |
||
64 | $currentCatalogue = new MessageCatalogue($locale); // Load defined messages for given locale |
||
65 | $loader->loadMessages($bundle->getPath() . '/Resources/translations', $currentCatalogue); // load messages from catalogue |
||
66 | $catalogues[$locale] = $currentCatalogue; // pass MessageCatalogue instance into $catalogues array |
||
67 | } |
||
68 | |||
69 | foreach ($catalogues as $locale => $catalogue) { // run over all catalogues |
||
70 | $catalogueMessages = $catalogue->all(); // get messages from current catalogue |
||
71 | $memcacheMessages = $uberMemcached->getItem($locale); // get existing messages from the memcache by locale |
||
72 | |||
73 | if (!$memcacheMessages) { // if for now no messages in memcache |
||
74 | $uberMemcached->addItem($locale, $catalogueMessages); // just upload them |
||
75 | } else { |
||
76 | foreach ($memcacheMessages as $domain => $messagesArray) { // run over messages in cache |
||
77 | foreach ($catalogueMessages as $d => $catMess) { // run over all in catalogue |
||
78 | if ($domain == $d) { // if domains equals |
||
79 | foreach ($messagesArray as $messKey => $trans) { //run over messages |
||
80 | foreach ($catMess as $ymlKey => $transl) { |
||
81 | if ($messKey == $ymlKey) { // if keys equals |
||
82 | unset($catalogueMessages[$d][$messKey]); // unset the value because we don`t want to repeated values |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | $mergedMessages = array_merge_recursive($memcacheMessages, $catalogueMessages); // merge recursive message arrays |
||
90 | $uberMemcached->addItem($locale, $mergedMessages); // set merged messages to memcache |
||
91 | } |
||
92 | } |
||
93 | |||
94 | $output->writeln("\033[37;42m Translations from " . $input->getArgument('bundle') . " imported successfully! \033[0m"); |
||
95 | } |
||
96 | } |
||
97 |