Completed
Push — theme-bundle ( 69304b...bce4cb )
by Kamil
18:15
created

TranslationFilesFinder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
c 1
b 1
f 0
lcom 1
cbo 3
dl 0
loc 61
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findTranslationFiles() 0 15 3
A getThemeFiles() 0 10 1
A isTranslationFile() 0 5 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\ThemeBundle\Translation;
13
14
use Sylius\Bundle\ThemeBundle\Factory\FinderFactoryInterface;
15
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
16
17
/**
18
 * @author Kamil Kokot <[email protected]>
19
 */
20
final class TranslationFilesFinder implements TranslationFilesFinderInterface
21
{
22
    /**
23
     * @var FinderFactoryInterface
24
     */
25
    private $finderFactory;
26
27
    /**
28
     * @param FinderFactoryInterface $finderFactory
29
     */
30
    public function __construct(FinderFactoryInterface $finderFactory)
31
    {
32
        $this->finderFactory = $finderFactory;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function findTranslationFiles(ThemeInterface $theme)
39
    {
40
        $themeFiles = $this->getThemeFiles($theme);
41
42
        $translationsFiles = [];
43
        foreach ($themeFiles as $themeFile) {
44
            if (!$this->isTranslationFile($themeFile)) {
45
                continue;
46
            }
47
48
            $translationsFiles[] = $themeFile;
49
        }
50
51
        return $translationsFiles;
52
    }
53
54
    /**
55
     * @param ThemeInterface $theme
56
     *
57
     * @return array
58
     */
59
    private function getThemeFiles(ThemeInterface $theme)
60
    {
61
        $finder = $this->finderFactory->create();
62
63
        $finder
64
            ->ignoreUnreadableDirs()
65
            ->in($theme->getPath());
66
67
        return $finder;
68
    }
69
70
    /**
71
     * @param string $file
72
     *
73
     * @return bool
74
     */
75
    private function isTranslationFile($file)
76
    {
77
        return false !== strpos($file, 'translations/')
78
            && (bool) preg_match('/^[^\.]+?\.[a-zA-Z_]{2,}?\.[a-z0-9]{2,}?$/', basename($file));
79
    }
80
}
81