Passed
Pull Request — master (#19)
by Michal
02:04
created

TranslationFinder::addTokenizer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 6
    public function __construct(SaverInterface $saver)
15
    {
16 6
        $this->saver = $saver;
17 6
    }
18
19 4
    public function addTokenizer(Tokenizer $tokenizer): TranslationFinder
20
    {
21 4
        $this->tokenizers[] = $tokenizer;
22 4
        return $this;
23
    }
24
25 6
    public function translate(): int
26
    {
27 6
        $tokensReplaced = 0;
28 6
        foreach ($this->tokenizers as $tokenizer) {
29 4
            foreach ($tokenizer->tokenize() as $tokenCollection) {
30
                // tento kod by som mohol dat do nejakeho file updatera
31 4
                $content = file_get_contents($tokenCollection->getFilePath());
32 4
                $newTexts = [];
33 4
                foreach ($tokenCollection->getTokens() as $token) {
34 4
                    $newTexts[$token->getOriginalBlock()] = str_replace($token->getOriginalText(), $token->getTranslationCode(), $token->getOriginalBlock());
35 4
                    $tokensReplaced++;
36
                }
37 4
                $content = str_replace(array_keys($newTexts), array_values($newTexts), $content);
38 4
                file_put_contents($tokenCollection->getFilePath(), $content);
39
40 4
                $this->saver->save($tokenCollection);
41
            }
42
        }
43 6
        return $tokensReplaced;
44
    }
45
}
46