Completed
Push — master ( a09b4a...382e94 )
by Tobias
08:18
created

FileStorage::__construct()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 13
cts 14
cp 0.9286
rs 8.439
c 0
b 0
f 0
cc 5
eloc 13
nc 12
nop 4
crap 5.009
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\Storage;
20
use Translation\Common\TransferableStorage;
21
22
/**
23
 * This storage uses Symfony's writer and loader.
24
 *
25
 * @author Tobias Nyholm <[email protected]>
26
 */
27
final class FileStorage implements Storage, TransferableStorage
28
{
29
    /**
30
     * @var TranslationWriterInterface
31
     */
32
    private $writer;
33
34
    /**
35
     * @var TranslationReaderInterface
36
     */
37
    private $reader;
38
39
    /**
40
     * @var array directory path
41
     */
42
    private $dir;
43
44
    /**
45
     * @var array with option to the dumper
46
     */
47
    private $options;
48
49
    /**
50
     * @var MessageCatalogue[] Fetched catalogues
51
     */
52
    private $catalogues;
53
54
    /**
55
     * @param TranslationWriterInterface $writer
56
     * @param TranslationReaderInterface $reader
57
     * @param array                      $dir
58
     * @param array                      $options
59
     */
60 10
    public function __construct($writer, $reader, array $dir, array $options = [])
61
    {
62
        // Create a wrapper for legacy writer
63 10
        if (!$writer instanceof TranslationWriterInterface) {
64
            $writer = new LegacyTranslationWriter($writer);
65
        }
66
67
        // Create a wrapper for legacy reader
68 10
        if (!$reader instanceof TranslationReaderInterface) {
69 1
            $reader = new LegacyTranslationReader($reader);
70
        }
71
72 9
        if (empty($dir)) {
73 1
            throw new \LogicException('Third parameter of FileStorage cannot be empty');
74
        }
75
76 8
        if (!array_key_exists('xliff_version', $options)) {
77
            // Set default value for xliff version.
78 8
            $options['xliff_version'] = '2.0';
79
        }
80
81 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...
82 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...
83 8
        $this->dir = $dir;
84 8
        $this->options = $options;
85 8
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 1
    public function get($locale, $domain, $key)
91
    {
92 1
        $catalogue = $this->getCatalogue($locale);
93 1
        $translation = $catalogue->get($key, $domain);
94
95 1
        return new Message($key, $domain, $locale, $translation);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 2
    public function create(Message $m)
102
    {
103 2
        $catalogue = $this->getCatalogue($m->getLocale());
104 2
        if (!$catalogue->defines($m->getKey(), $m->getDomain())) {
105 2
            $catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain());
106 2
            $this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain());
107
        }
108 2
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 1
    public function update(Message $m)
114
    {
115 1
        $catalogue = $this->getCatalogue($m->getLocale());
116 1
        $catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain());
117 1
        $this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain());
118 1
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 1
    public function delete($locale, $domain, $key)
124
    {
125 1
        $catalogue = $this->getCatalogue($locale);
126 1
        $messages = $catalogue->all($domain);
127 1
        unset($messages[$key]);
128
129 1
        $catalogue->replace($messages, $domain);
130 1
        $this->writeCatalogue($catalogue, $locale, $domain);
131 1
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 1
    public function export(MessageCatalogueInterface $catalogue)
137
    {
138 1
        $locale = $catalogue->getLocale();
139 1
        $catalogue->addCatalogue($this->getCatalogue($locale));
140 1
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 1
    public function import(MessageCatalogueInterface $catalogue)
146
    {
147 1
        $domains = $catalogue->getDomains();
148 1
        foreach ($domains as $domain) {
149 1
            $this->writeCatalogue($catalogue, $catalogue->getLocale(), $domain);
150
        }
151 1
    }
152
153
    /**
154
     * Save catalogue back to file.
155
     *
156
     * @param MessageCatalogueInterface $catalogue
157
     * @param string                    $domain
158
     */
159 5
    private function writeCatalogue(MessageCatalogueInterface $catalogue, $locale, $domain)
160
    {
161 5
        $resources = $catalogue->getResources();
162 5
        $options = $this->options;
163 5
        $written = false;
164 5
        foreach ($resources as $resource) {
165 3
            $path = (string) $resource;
166 3
            if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) {
167 3
                $options['path'] = str_replace($matches[0], '', $path);
168 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...
169 3
                $written = true;
170
            }
171
        }
172
173 5
        if ($written) {
174
            // We have written the translation to a file.
175 3
            return;
176
        }
177
178 2
        $options['path'] = reset($this->dir);
179 2
        $format = isset($options['default_output_format']) ? $options['default_output_format'] : 'xlf';
180 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...
181 2
    }
182
183
    /**
184
     * @param string $locale
185
     *
186
     * @return MessageCatalogue
187
     */
188 6
    private function getCatalogue($locale)
189
    {
190 6
        if (empty($this->catalogues[$locale])) {
191 6
            $this->loadCatalogue($locale, $this->dir);
192
        }
193
194 6
        return $this->catalogues[$locale];
195
    }
196
197
    /**
198
     * Load catalogue from files.
199
     *
200
     * @param string $locale
201
     * @param array  $dirs
202
     */
203 6
    private function loadCatalogue($locale, array $dirs)
204
    {
205 6
        $currentCatalogue = new MessageCatalogue($locale);
206 6
        foreach ($dirs as $path) {
207 6
            if (is_dir($path)) {
208 6
                $this->reader->read($path, $currentCatalogue);
209
            }
210
        }
211
212 6
        $this->catalogues[$locale] = $currentCatalogue;
213 6
    }
214
}
215