Completed
Push — master ( 00951d...4d50ae )
by Tobias
09:16
created

MetadataAwareMerge   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 87
ccs 0
cts 61
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C processDomain() 0 51 10
C mergeMetadata() 0 26 7
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\Operation;
13
14
use Symfony\Component\Translation\Catalogue\AbstractOperation;
15
16
/**
17
 * @author Tobias Nyholm <[email protected]>
18
 * @author Jean-François Simon <[email protected]>
19
 */
20
class MetadataAwareMerge extends AbstractOperation
21
{
22
    protected function processDomain($domain)
23
    {
24
        $this->messages[$domain] = [
25
            'all' => [],
26
            'new' => [],
27
            'obsolete' => [],
28
        ];
29
        $targetMessages = $this->target->all($domain);
30
31
        foreach ($this->source->all($domain) as $id => $message) {
32
            $this->messages[$domain]['all'][$id] = $message;
33
            $this->result->add([$id => $message], $domain);
34
            if (empty($message)) {
35
                $this->messages[$domain]['new'][$id] = $message;
36
            }
37
38
            // If $id is NOT defined in target.
39
            if (!array_key_exists($id, $targetMessages)) {
40
                $this->messages[$domain]['obsolete'][$id] = $message;
41
            }
42
43
            if (null !== $keySourceMetadata = $this->source->getMetadata($id, $domain)) {
44
                $this->result->setMetadata($id, $keySourceMetadata, $domain);
45
            }
46
47
            // Get metadata from target
48
            if (null !== $keyTargetMetadata = $this->target->getMetadata($id, $domain)) {
49
                if (null === $keySourceMetadata) {
50
                    // If there were no metadata in source. Just use target's metadata
51
                    $this->result->setMetadata($id, $keyTargetMetadata, $domain);
52
                    continue;
53
                }
54
55
                // Merge metadata
56
                $resultMetadata = $this->mergeMetaData($keySourceMetadata, $keyTargetMetadata);
57
                $this->result->setMetadata($id, $resultMetadata, $domain);
58
            }
59
        }
60
61
        foreach ($targetMessages as $id => $message) {
62
            if (!$this->source->has($id, $domain)) {
63
                $this->messages[$domain]['all'][$id] = $message;
64
                $this->messages[$domain]['new'][$id] = $message;
65
66
                $this->result->add([$id => $message], $domain);
67
                if (null !== $keyMetadata = $this->target->getMetadata($id, $domain)) {
68
                    $this->result->setMetadata($id, $keyMetadata, $domain);
69
                }
70
            }
71
        }
72
    }
73
74
    /**
75
     * @param array $source
76
     * @param array $target
77
     *
78
     * @return array
79
     */
80
    private function mergeMetadata(array $source, array $target)
81
    {
82
        $toRemove = ['status:new', 'status:obsolete', 'file-source'];
83
        $resultNotes = [];
84
85
        // Remove some old notes
86
        if (isset($source['notes'])) {
87
            foreach ($source['notes'] as $note) {
88
                if (isset($note['content']) && in_array($note['content'], $toRemove)) {
89
                    continue;
90
                }
91
                $resultNotes[] = $note;
92
            }
93
        }
94
95
        if (isset($target['notes'])) {
96
            foreach ($target['notes'] as $note) {
97
                $resultNotes[] = $note;
98
            }
99
        }
100
101
        $result = $source;
102
        $result['notes'] = $resultNotes;
103
104
        return $result;
105
    }
106
}
107