Completed
Push — master ( 65f893...067f9c )
by Michal
03:11
created

ExtractorConfig   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 55%

Importance

Changes 0
Metric Value
wmc 7
eloc 18
dl 0
loc 41
ccs 11
cts 20
cp 0.55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 12 3
A addTokenizer() 0 4 1
A __construct() 0 3 1
A extract() 0 7 2
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\Command\Extractor;
4
5
use Closure;
6
use Efabrica\TranslationsAutomatization\Saver\SaverInterface;
7
use Efabrica\TranslationsAutomatization\Tokenizer\TokenCollection;
8
use Efabrica\TranslationsAutomatization\Tokenizer\Tokenizer;
9
10
class ExtractorConfig
11
{
12
    private $saver;
13
14
    private $tokenizers = [];
15
16 8
    public function __construct(SaverInterface $saver)
17
    {
18 8
        $this->saver = $saver;
19 8
    }
20
21 4
    public function addTokenizer(Tokenizer $tokenizer): ExtractorConfig
22
    {
23 4
        $this->tokenizers[] = $tokenizer;
24 4
        return $this;
25
    }
26
27
    /**
28
     * @return TokenCollection[]
29
     */
30 8
    public function extract(): array
31
    {
32 8
        $tokenCollections = [];
33 8
        foreach ($this->tokenizers as $tokenizer) {
34 4
            $tokenCollections = array_merge($tokenCollections, $tokenizer->tokenize());
35
        }
36 8
        return $tokenCollections;
37
    }
38
39
    public function process(TokenCollection $tokenCollection, Closure $callback): void
40
    {
41
        // tento kod by som mohol dat do nejakeho file updatera
42
        $content = file_get_contents($tokenCollection->getFilePath()) ?: '';
43
        $newTexts = [];
44
        foreach ($tokenCollection->getTokens() as $token) {
45
            $newTexts[$token->getOriginalBlock()] = str_replace($token->getOriginalText(), $token->getTranslationCode(), $token->getOriginalBlock());
46
            $callback($token);
47
        }
48
        $content = str_replace(array_keys($newTexts), array_values($newTexts), $content);
49
        file_put_contents($tokenCollection->getFilePath(), $content);
50
        $this->saver->save($tokenCollection);
51
    }
52
}
53