MessageCatalogueWriter::getDumper()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace Itkg\TranslationBundle\Writer;
4
5
use Symfony\Component\DependencyInjection\ContainerAware;
6
use Symfony\Component\Translation\MessageCatalogue;
7
use Symfony\Component\Translation\Writer\TranslationWriter;
8
9
/**
10
 * Class MessageCatalogWriter
11
 */
12
class MessageCatalogueWriter 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...
13
{
14
    /**
15
     * @var TranslationWriter
16
     */
17
    private $translationWriter;
18
19
    /**
20
     * @param TranslationWriter $translationWriter
21
     */
22 3
    public function __construct(TranslationWriter $translationWriter)
23
    {
24 3
        $this->translationWriter = $translationWriter;
25 3
    }
26
27
    /**
28
     * @param array       $messageCatalogues
29
     * @param string      $format
30
     * @param null|string $path
31
     */
32 3
    public function write(array $messageCatalogues, $format, $path = null)
33
    {
34 3
        $this->translationWriter->addDumper($format, $this->getDumper($format));
35
36
        /** @var MessageCatalogue $messageCatalogue */
37 3
        foreach ($messageCatalogues as $messageCatalogue) {
38 3
            $this->translationWriter->writeTranslations($messageCatalogue, $format, array(
39 3
                'path' => $this->getTranslationPath($path))
40 3
            );
41 3
        }
42 3
    }
43
    /**
44
     * Returns Symfony requested format dumper
45
     *
46
     * @param string $format
47
     *
48
     * @return \Symfony\Component\Translation\Dumper\DumperInterface
49
     *
50
     * @throws \InvalidArgumentException
51
     */
52 3 View Code Duplication
    private function getDumper($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...
53
    {
54 3
        $service = sprintf('translation.dumper.%s', $format);
55
56 3
        if (!$this->container->has($service)) {
57
            throw new \InvalidArgumentException(sprintf('Unable to find Symfony Translation dumper for format "%s"', $format));
58
        }
59
60 3
        return $this->container->get($service);
61
    }
62
63
    /**
64
     * Returns translation path
65
     *
66
     * @param string $outputPath
67
     *
68
     * @return string
69
     */
70 3
    private function getTranslationPath($outputPath = null)
71
    {
72 3
        if ($outputPath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $outputPath of type string|null 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...
73 3
            return $outputPath;
74
        }
75
76
        return $this->container->get('kernel')->getRootDir() . '/Resources/translations';
77
    }
78
}
79