Tokenizer::tokenize()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 10
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\Tokenizer;
4
5
use Efabrica\TranslationsAutomatization\FileFinder\FileFinderInterface;
6
use Efabrica\TranslationsAutomatization\TextFinder\TextFinderInterface;
7
use Efabrica\TranslationsAutomatization\TokenModifier\CompositeTokenModifier;
8
use Efabrica\TranslationsAutomatization\TokenModifier\TokenModifierInterface;
9
10
class Tokenizer
11
{
12
    private $fileFinder;
13
14
    private $textFinder;
15
16
    private $tokenModifier;
17
18 10
    public function __construct(
19
        FileFinderInterface $fileFinder,
20
        TextFinderInterface $textFinder
21
    ) {
22 10
        $this->fileFinder = $fileFinder;
23 10
        $this->textFinder = $textFinder;
24 10
        $this->tokenModifier = new CompositeTokenModifier();
25 10
    }
26
27 6
    public function addTokenModifier(TokenModifierInterface $tokenModifier)
28
    {
29 6
        $this->tokenModifier->addTokenModifier($tokenModifier);
30 6
        return $this;
31
    }
32
33
    /**
34
     * @return TokenCollection[]
35
     */
36 10
    public function tokenize(): array
37
    {
38 10
        $tokenCollections = [];
39 10
        foreach ($this->fileFinder->find() as $file) {
40 10
            $texts = $this->textFinder->find(file_get_contents($file));
41 10
            $tokenCollection = new TokenCollection($file);
42 10
            foreach ($texts as $originalBlock => $originalText) {
43 10
                $tokenCollection->addToken(new Token($originalText, $originalBlock));
44
            }
45 10
            $tokenCollections[] = $this->tokenModifier->modifyAll($tokenCollection);
46
        }
47 10
        return $tokenCollections;
48
    }
49
}
50