TranslationExtractor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Itkg\TranslationBundle\Extractor;
4
5
use Symfony\Component\DependencyInjection\ContainerAware;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\Finder\Finder;
8
use Symfony\Component\Finder\SplFileInfo;
9
use Symfony\Component\Translation\Loader\LoaderInterface;
10
use Symfony\Component\Translation\MessageCatalogue;
11
12
/**
13
 * Class TranslationExtractor
14
 */
15
class TranslationExtractor extends ContainerAware
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...njection\ContainerAware has been deprecated with message: since version 2.8, to be removed in 3.0. Use the ContainerAwareTrait instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
16
{
17
    /**
18
     * @var Finder
19
     */
20
    private $finder;
21
22
    /**
23
     * @var Filesystem
24
     */
25
    private $filesystem;
26
27
    /**
28
     * @var MessageCatalogue[]
29
     */
30
    private $messageCatalogues = [];
31
32
    /**
33
     * @param Finder     $finder
34
     * @param Filesystem $filesystem
35
     */
36 3
    public function __construct(Finder $finder, Filesystem $filesystem)
37
    {
38 3
        $this->finder = $finder;
39 3
        $this->filesystem = $filesystem;
40 3
    }
41
42
    /**
43
     * @param string      $path
44
     * @param string      $format
45
     * @param null|string $domain
46
     *
47
     * @return MessageCatalogue[]
48
     */
49 3
    public function extract($path, $format, $domain = null)
50
    {
51 3
        $this->messageCatalogues = [];
52 3
        if (!$this->filesystem->exists($path)) {
53
            throw new \RuntimeException(sprintf('Path %s does not exist', $path));
54
        }
55 3
        $files = $this->finder->files()->name('/[a-z]+\.[a-z]{2}\.'.$format.'/')->in($path);
56
57
        /** @var SplFileInfo $file */
58 3
        foreach ($files as $file) {
59 3
            list($translationDomain, $language) = explode('.', $file->getFilename());
60 3
            if ($domain && $domain !== $translationDomain) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $domain of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
61 1
                continue;
62
            }
63 3
            $this->addMessageCatalogue($this->getLoader($format)->load($file->getRealPath(), $language, $translationDomain), $language);
64 3
        }
65 3
        return $this->messageCatalogues;
66
    }
67
68
    /**
69
     * @param MessageCatalogue $messageCatalogue
70
     * @param string           $language
71
     */
72 3
    private function addMessageCatalogue(MessageCatalogue $messageCatalogue, $language)
73
    {
74 3
        if (!$messageCatalogue->all()) {
75
            return;
76
        }
77
78 3
        if (isset($this->messageCatalogues[$language])) {
79
            $this->messageCatalogues[$language]->addCatalogue($messageCatalogue);
80
        } else {
81 3
            $this->messageCatalogues[$language] = $messageCatalogue;
82
        }
83 3
    }
84
85
    /**
86
     * @param string $format
87
     *
88
     * @return LoaderInterface
89
     */
90 3 View Code Duplication
    private function getLoader($format)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92 3
        $service = sprintf('translation.loader.%s', $format);
93
94 3
        if (!$this->container->has($service)) {
95
            throw new \InvalidArgumentException(sprintf('Unable to find Symfony Translation loader for format "%s"', $format));
96
        }
97
98 3
        return $this->container->get($service);
99
    }
100
}
101