Completed
Push — master ( 13f2dd...a39436 )
by Tobias
05:16
created

MetadataWriter::exists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 2
crap 12
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\Bundle\Catalogue;
13
14
use Symfony\Component\Translation\MessageCatalogue;
15
16
class MetadataWriter
17
{
18
    /**
19
     * @param MessageCatalogue $catalogue
20
     * @param string           $key       message key
21
     * @param string           $domain
22
     * @param string           $type      the type of metadata. "notes" is a common one
23
     * @param mixed            $value
24
     */
25
    public function write(MessageCatalogue $catalogue, $key, $domain, $type, $value)
26
    {
27
        $meta = $catalogue->getMetadata($key, $domain);
28
        if (!isset($meta[$type])) {
29
            $meta[$type] = [];
30
        }
31
32
        if ($this->exists($meta[$type], $value)) {
33
            return;
34
        }
35
36
        $meta[$type][] = $value;
37
        $catalogue->setMetadata($key, $meta, $domain);
38
    }
39
40
    /**
41
     * @param array $metadata
42
     * @param mixed $value
43
     *
44
     * @return bool
45
     */
46
    private function exists($metadata, $value)
47
    {
48
        foreach ($metadata as $data) {
49
            if ($data == $value) {
50
                return true;
51
            }
52
        }
53
54
        return false;
55
    }
56
}
57