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

FilePathToKeyTokenModifier::modifyAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\TokenModifier;
4
5
use Efabrica\TranslationsAutomatization\Tokenizer\TokenCollection;
6
7
class FilePathToKeyTokenModifier implements TokenModifierInterface
8
{
9
    private $basePath;
10
11
    private $excludeDirectoryNames;
12
13 6
    public function __construct(string $basePath, array $excludeDirectoryNames = [])
14
    {
15 6
        $this->basePath = rtrim($basePath, '/');
16 6
        $this->excludeDirectoryNames = array_map('strtolower', $excludeDirectoryNames);
17 6
    }
18
19 6
    public function modifyAll(TokenCollection $tokenCollection): TokenCollection
20
    {
21 6
        $pathParts = explode('/', str_replace($this->basePath . '/', '', $tokenCollection->getFilePath()));
22 6
        array_pop($pathParts);
23 6
        foreach ($tokenCollection->getTokens() as $token) {
24
            $newKeyParts = array_filter(array_map('strtolower', $pathParts), function ($pathPart) {
25 6
                return !in_array($pathPart, $this->excludeDirectoryNames);
26 6
            });
27 6
            $newKeyParts[] = $token->getTranslationKey();
28 6
            $token->changeTranslationKey(implode('.', $newKeyParts));
29
        }
30 6
        return $tokenCollection;
31
    }
32
}
33