Completed
Pull Request — master (#19)
by Michal
06:24
created

TranslationFinder::translate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 0
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\TranslationFinder;
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(): int
26
    {
27
        $tokensReplaced = 0;
28
        foreach ($this->tokenizers as $tokenizer) {
29
            foreach ($tokenizer->tokenize() as $tokenCollection) {
30
                // tento kod by som mohol dat do nejakeho file updatera
31
                $content = file_get_contents($tokenCollection->getFilePath());
32
                $newTexts = [];
33
                foreach ($tokenCollection->getTokens() as $token) {
34
                    $newTexts[$token->getOriginalBlock()] = str_replace($token->getOriginalText(), $token->getTranslationCode(), $token->getOriginalBlock());
35
                    $tokensReplaced++;
36
                }
37
                $content = str_replace(array_keys($newTexts), array_values($newTexts), $content);
38
                file_put_contents($tokenCollection->getFilePath(), $content);
39
40
                $this->saver->save($tokenCollection);
41
            }
42
        }
43
        return $tokensReplaced;
44
    }
45
}
46