Completed
Push — master ( 3231e7...cf6a99 )
by Tobias
01:28
created

Extractor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 55
ccs 20
cts 24
cp 0.8333
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A extract() 0 4 1
A extractFromDirectory() 0 7 1
A addFileExtractor() 0 4 1
A doExtract() 0 11 3
A getRelevantExtractorForFile() 0 17 4
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Extractor;
13
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\Finder\SplFileInfo;
16
use Translation\Extractor\FileExtractor\FileExtractor;
17
use Translation\Extractor\Model\SourceCollection;
18
19
/**
20
 * Main class for all extractors. This is the service that will be loaded with file
21
 * extractors.
22
 *
23
 * @author Tobias Nyholm <[email protected]>
24
 */
25
final class Extractor
26
{
27
    /**
28
     * @var FileExtractor[]
29
     */
30
    private $fileExtractors = [];
31
32 1
    public function extract(Finder $finder): SourceCollection
33
    {
34 1
        return $this->doExtract($finder);
35
    }
36
37
    public function extractFromDirectory(string $dir): SourceCollection
38
    {
39
        $finder = new Finder();
40
        $finder->files()->in($dir);
41
42
        return $this->doExtract($finder);
43
    }
44
45 1
    public function addFileExtractor(FileExtractor $fileExtractor): void
46
    {
47 1
        $this->fileExtractors[] = $fileExtractor;
48 1
    }
49
50 1
    private function doExtract(Finder $finder): SourceCollection
51
    {
52 1
        $collection = new SourceCollection();
53 1
        foreach ($finder as $file) {
54 1
            if (null !== $extractor = $this->getRelevantExtractorForFile($file)) {
55 1
                $extractor->getSourceLocations($file, $collection);
56
            }
57
        }
58
59 1
        return $collection;
60
    }
61
62 1
    private function getRelevantExtractorForFile(SplFileInfo $file): ?FileExtractor
63
    {
64 1
        $filename = $file->getFilename();
65 1
        if (preg_match('|.+\.blade\.php$|', $filename)) {
66 1
            $ext = 'blade.php';
67
        } else {
68 1
            $ext = $file->getExtension();
69
        }
70
71 1
        foreach ($this->fileExtractors as $extractor) {
72 1
            if ($extractor->supportsExtension($ext)) {
73 1
                return $extractor;
74
            }
75
        }
76
77 1
        return null;
78
    }
79
}
80