Completed
Pull Request — 5.4 (#2670)
by
unknown
18:53 queued 12:41
created

getDefaultTranslationFolder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Kunstmaan\TranslatorBundle\Service;
4
5
use Kunstmaan\TranslatorBundle\Service\Exception\TranslationsNotFoundException;
6
use Symfony\Component\Finder\Finder;
7
use Symfony\Component\HttpKernel\Kernel;
8
9
class TranslationFileExplorer
10
{
11
    /**
12
     * An array of supported file formats to look for
13
     *
14
     * @var array
15
     */
16
    private $fileFormats = array();
17
18
    /**
19
     *  Looks in the path for Resources/translation files and returns a finder object with the result
20
     *
21
     * @param string $path
22
     * @param array  $locales
23
     *
24
     * @return \Symfony\Component\Finder\Finder
25
     */
26 2
    public function find($path, array $locales, $translationDirectory = null)
27
    {
28 2
        $finder = new Finder();
29 2
        $translationDirectory = $translationDirectory ?? $this->getDefaultTranslationFolder();
30
31 2
        $exploreDir = $path . '/' . $translationDirectory;
32
33 2
        if (is_dir($exploreDir)) {
34 2
            $finder->files()
35 2
                ->name(sprintf('/(.*(%s)\.(%s))/', implode('|', $locales), implode('|', $this->fileFormats)))
36 2
                ->in($exploreDir);
37
38 2
            return $finder;
39
        }
40
41
        throw new TranslationsNotFoundException('Directory `' . $exploreDir . '` does not exist, translations could not be found.');
42
    }
43
44 6
    public function setFileFormats($fileFormats)
45
    {
46 6
        $this->fileFormats = $fileFormats;
47 6
    }
48
49
    protected function getDefaultTranslationFolder(): string
50
    {
51
        if (Kernel::VERSION_ID >= 40000) {
52
            return 'translations';
53
        }
54
55
        return 'Resources/translations';
56
    }
57
}
58