Extractor::extract()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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