FlysystemXliffDumper::setRelativePathTemplate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
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 Translation\PlatformAdapter\Flysystem\Dumper;
13
14
use Symfony\Component\Translation\Dumper\XliffFileDumper;
15
use Symfony\Component\Translation\MessageCatalogue;
16
use Symfony\Component\Translation\Exception\InvalidArgumentException;
17
use League\Flysystem\Filesystem;
18
use Symfony\Component\Translation\Exception\RuntimeException;
19
20
/**
21
 * FileDumper is an implementation of DumperInterface that dump a message catalogue to file(s).
22
 * Performs backup of already existing files.
23
 *
24
 * Options:
25
 * - path (mandatory): the directory where the files should be saved
26
 *
27
 * @author Michel Salib <[email protected]>
28
 * @author Tobias Nyholm <[email protected]>
29
 */
30
final class FlysystemXliffDumper extends XliffFileDumper
31
{
32
    /**
33
     * @var Filesystem
34
     */
35
    private $filesystem;
36
37
    /**
38
     * A template for the relative paths to files.
39
     *
40
     * @var string
41
     */
42
    protected $relativePathTemplate = '%domain%.%locale%.%extension%';
43
44
    /**
45
     * Make file backup before the dump.
46
     *
47
     * @var bool
48
     */
49
    private $backup = true;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
50
51
    /**
52
     * @param Filesystem $filesystem
53
     */
54 1
    public function setFilesystem($filesystem)
55
    {
56 1
        $this->filesystem = $filesystem;
57 1
    }
58
59
    /**
60
     * Sets the template for the relative paths to files.
61
     *
62
     * @param string $relativePathTemplate A template for the relative paths to files
63
     */
64
    public function setRelativePathTemplate($relativePathTemplate)
65
    {
66
        $this->relativePathTemplate = $relativePathTemplate;
67
    }
68
69
    /**
70
     * Sets backup flag.
71
     *
72
     * @param bool
73
     */
74
    public function setBackup($backup)
75
    {
76
        $this->backup = $backup;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function dump(MessageCatalogue $messages, $options = [])
83
    {
84
        if (!array_key_exists('path', $options)) {
85
            throw new InvalidArgumentException('The file dumper needs a path option.');
86
        }
87
88
        // save a file for each domain
89
        foreach ($messages->getDomains() as $domain) {
90
            // backup
91
            $fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
92
            if ($this->filesystem->has($fullpath)) {
93
                if ($this->backup) {
94
                    @trigger_error('Creating a backup while dumping a message catalogue is deprecated since version 3.1 and will be removed in 4.0. Use TranslationWriter::disableBackup() to disable the backup.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
95
                    $this->filesystem->copy($fullpath, $fullpath.'~');
96
                }
97
            } else {
98
                $directory = dirname($fullpath);
99
                if (!$this->filesystem->has($directory) && !@mkdir($directory, 0777, true)) {
100
                    try {
101
                        $this->filesystem->createDir($directory);
102
                    } catch (\Exception $e) {
103
                        throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory), 0, $e);
104
                    }
105
                }
106
            }
107
            // save file
108
            $this->filesystem->write($fullpath, $this->formatCatalogue($messages, $domain, $options));
109
        }
110
    }
111
112
    /**
113
     * Gets the relative file path using the template.
114
     *
115
     * @param string $domain The domain
116
     * @param string $locale The locale
117
     *
118
     * @return string The relative file path
119
     */
120
    private function getRelativePath($domain, $locale)
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
121
    {
122
        return strtr($this->relativePathTemplate, [
123
            '%domain%' => $domain,
124
            '%locale%' => $locale,
125
            '%extension%' => $this->getExtension(),
126
        ]);
127
    }
128
}
129