Completed
Push — master ( 216072...caf6f7 )
by Team eFabrica
02:21
created

TranslationFinder::translate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 0
dl 0
loc 14
ccs 0
cts 10
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization;
4
5
use Efabrica\TranslationsAutomatization\Saver\SaverInterface;
6
use Efabrica\TranslationsAutomatization\Tokenizer\Tokenizer;
7
8
class TranslationFinder
9
{
10
    private $saver;
11
12
    private $tokenizers = [];
13
14
    public function __construct(SaverInterface $saver)
15
    {
16
        $this->saver = $saver;
17
    }
18
19
    public function addTokenizer(Tokenizer $tokenizer): TranslationFinder
20
    {
21
        $this->tokenizers[] = $tokenizer;
22
        return $this;
23
    }
24
25
    public function translate()
26
    {
27
        foreach ($this->tokenizers as $tokenizer) {
28
            foreach ($tokenizer->tokenize() as $tokenCollection) {
29
                // tento kod by som mohol dat do nejakeho file updatera
30
                $content = file_get_contents($tokenCollection->getFilePath());
31
                $newTexts = [];
32
                foreach ($tokenCollection->getTokens() as $token) {
33
                    $newTexts[$token->getOriginalBlock()] = str_replace($token->getOriginalText(), $token->getTranslationCode(), $token->getOriginalBlock());
34
                }
35
                $content = str_replace(array_keys($newTexts), array_values($newTexts), $content);
36
                file_put_contents($tokenCollection->getFilePath(), $content);
37
38
                $this->saver->save($tokenCollection);
39
            }
40
        }
41
    }
42
}
43