Completed
Push — master ( 54a9d8...e3bbff )
by Tobias
03:08
created

FileStorage::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
crap 1
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\SymfonyStorage;
13
14
use Symfony\Component\Translation\MessageCatalogue;
15
use Symfony\Component\Translation\MessageCatalogueInterface;
16
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
17
use Symfony\Component\Translation\Writer\TranslationWriterInterface;
18
use Translation\Common\Model\Message;
19
use Translation\Common\Model\MessageInterface;
20
use Translation\Common\Storage;
21
use Translation\Common\TransferableStorage;
22
23
/**
24
 * This storage uses Symfony's writer and loader.
25
 *
26
 * @author Tobias Nyholm <[email protected]>
27
 */
28
final class FileStorage implements Storage, TransferableStorage
29
{
30
    /**
31
     * @var TranslationWriterInterface
32
     */
33
    private $writer;
34
35
    /**
36
     * @var TranslationReaderInterface
37
     */
38
    private $reader;
39
40
    /**
41
     * @var array directory path
42
     */
43
    private $dir;
44
45
    /**
46
     * @var array with option to the dumper
47
     */
48
    private $options;
49
50
    /**
51
     * @var MessageCatalogue[] Fetched catalogues
52
     */
53
    private $catalogues;
54
55
    /**
56
     * @param TranslationWriterInterface $writer
57
     * @param TranslationReaderInterface $reader
58
     * @param array                      $dir
59
     * @param array                      $options
60
     */
61 10
    public function __construct($writer, $reader, array $dir, array $options = [])
62
    {
63
        // Create a wrapper for legacy writer
64 10
        if (!$writer instanceof TranslationWriterInterface) {
65
            $writer = new LegacyTranslationWriter($writer);
66
        }
67
68
        // Create a wrapper for legacy reader
69 10
        if (!$reader instanceof TranslationReaderInterface) {
70 1
            $reader = new LegacyTranslationReader($reader);
71
        }
72
73 9
        if (empty($dir)) {
74 1
            throw new \LogicException('Third parameter of FileStorage cannot be empty');
75
        }
76
77 8
        if (!array_key_exists('xliff_version', $options)) {
78
            // Set default value for xliff version.
79 8
            $options['xliff_version'] = '2.0';
80
        }
81
82 8
        $this->writer = $writer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $writer can also be of type object<Translation\Symfo...egacyTranslationWriter>. However, the property $writer is declared as type object<Symfony\Component...slationWriterInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
83 8
        $this->reader = $reader;
0 ignored issues
show
Documentation Bug introduced by
It seems like $reader can also be of type object<Translation\Symfo...egacyTranslationReader>. However, the property $reader is declared as type object<Symfony\Component...slationReaderInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
84 8
        $this->dir = $dir;
85 8
        $this->options = $options;
86 8
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 1
    public function get($locale, $domain, $key)
92
    {
93 1
        $catalogue = $this->getCatalogue($locale);
94 1
        $translation = $catalogue->get($key, $domain);
95
96 1
        return new Message($key, $domain, $locale, $translation);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 2
    public function create(MessageInterface $m)
103
    {
104 2
        $catalogue = $this->getCatalogue($m->getLocale());
105 2
        if (!$catalogue->defines($m->getKey(), $m->getDomain())) {
106 2
            $catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain());
107 2
            $this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain());
108
        }
109 2
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 1
    public function update(MessageInterface $m)
115
    {
116 1
        $catalogue = $this->getCatalogue($m->getLocale());
117 1
        $catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain());
118 1
        $this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain());
119 1
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 1
    public function delete($locale, $domain, $key)
125
    {
126 1
        $catalogue = $this->getCatalogue($locale);
127 1
        $messages = $catalogue->all($domain);
128 1
        unset($messages[$key]);
129
130 1
        $catalogue->replace($messages, $domain);
131 1
        $this->writeCatalogue($catalogue, $locale, $domain);
132 1
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 1
    public function export(MessageCatalogueInterface $catalogue)
138
    {
139 1
        $locale = $catalogue->getLocale();
140 1
        $catalogue->addCatalogue($this->getCatalogue($locale));
141 1
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 1
    public function import(MessageCatalogueInterface $catalogue)
147
    {
148 1
        $domains = $catalogue->getDomains();
149 1
        foreach ($domains as $domain) {
150 1
            $this->writeCatalogue($catalogue, $catalogue->getLocale(), $domain);
151
        }
152 1
    }
153
154
    /**
155
     * Save catalogue back to file.
156
     *
157
     * @param MessageCatalogueInterface $catalogue
158
     * @param string                    $domain
159
     */
160 5
    private function writeCatalogue(MessageCatalogueInterface $catalogue, $locale, $domain)
161
    {
162 5
        $resources = $catalogue->getResources();
163 5
        $options = $this->options;
164 5
        $written = false;
165 5
        foreach ($resources as $resource) {
166 3
            $path = (string) $resource;
167 3
            if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) {
168 3
                $options['path'] = str_replace($matches[0], '', $path);
169 3
                $this->writer->write($catalogue, $matches[1], $options);
0 ignored issues
show
Compatibility introduced by
$catalogue of type object<Symfony\Component...sageCatalogueInterface> is not a sub-type of object<Symfony\Component...ation\MessageCatalogue>. It seems like you assume a concrete implementation of the interface Symfony\Component\Transl...ssageCatalogueInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
170 3
                $written = true;
171
            }
172
        }
173
174 5
        if ($written) {
175
            // We have written the translation to a file.
176 3
            return;
177
        }
178
179 2
        $options['path'] = reset($this->dir);
180 2
        $format = isset($options['default_output_format']) ? $options['default_output_format'] : 'xlf';
181 2
        $this->writer->write($catalogue, $format, $options);
0 ignored issues
show
Compatibility introduced by
$catalogue of type object<Symfony\Component...sageCatalogueInterface> is not a sub-type of object<Symfony\Component...ation\MessageCatalogue>. It seems like you assume a concrete implementation of the interface Symfony\Component\Transl...ssageCatalogueInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
182 2
    }
183
184
    /**
185
     * @param string $locale
186
     *
187
     * @return MessageCatalogue
188
     */
189 6
    private function getCatalogue($locale)
190
    {
191 6
        if (empty($this->catalogues[$locale])) {
192 6
            $this->loadCatalogue($locale, $this->dir);
193
        }
194
195 6
        return $this->catalogues[$locale];
196
    }
197
198
    /**
199
     * Load catalogue from files.
200
     *
201
     * @param string $locale
202
     * @param array  $dirs
203
     */
204 6
    private function loadCatalogue($locale, array $dirs)
205
    {
206 6
        $currentCatalogue = new MessageCatalogue($locale);
207 6
        foreach ($dirs as $path) {
208 6
            if (is_dir($path)) {
209 6
                $this->reader->read($path, $currentCatalogue);
210
            }
211
        }
212
213 6
        $this->catalogues[$locale] = $currentCatalogue;
214 6
    }
215
}
216