Completed
Push — master ( b453a3...b1e9d6 )
by Ruud
40:05 queued 27:14
created

TranslationFileExplorer::setFileFormats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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