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

TranslationFileExplorer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 47
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setFileFormats() 0 4 1
A find() 0 17 2
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