Completed
Push — master ( da6bb4...b1a77c )
by Tobias
09:49 queued 02:41
created

Metadata::removeAllInCategory()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 3
nop 1
crap 4
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\Model;
13
14
/**
15
 * Message metadata abstraction.
16
 *
17
 * @author Tobias Nyholm <[email protected]>
18
 */
19
final class Metadata
20
{
21
    /**
22
     * @var array
23
     */
24
    private $metadata;
25
26
    /**
27
     * @var array
28
     */
29
    private $notes = [];
30
31
    /**
32
     * @param array $metadata
33
     */
34 6
    public function __construct($metadata)
35
    {
36 6
        if (empty($metadata)) {
37 4
            $metadata = [];
38
        }
39
40 6
        $this->metadata = $metadata;
41 6
        if (isset($metadata['notes'])) {
42 4
            $this->notes = $metadata['notes'];
43
        }
44 6
    }
45
46
    /**
47
     * @return string|null
48
     */
49 4 View Code Duplication
    public function getState()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51 4
        $notes = $this->getAllInCategory('state');
52 4
        foreach ($notes as $note) {
53 4
            if (isset($note['content'])) {
54 4
                return $note['content'];
55
            }
56
        }
57 4
    }
58
59
    /**
60
     * @param string $state
61
     */
62 1
    public function setState($state)
63
    {
64 1
        $this->removeAllInCategory('state');
65 1
        $this->addCategory('state', $state);
66 1
    }
67
68
    /**
69
     * @return null|string
70
     */
71 1 View Code Duplication
    public function getDesc()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73 1
        $notes = $this->getAllInCategory('desc');
74 1
        foreach ($notes as $note) {
75
            if (isset($note['content'])) {
76
                return $note['content'];
77
            }
78
        }
79
80 1
        return null;
81
    }
82
83
    /**
84
     * Get the extracted translation if any.
85
     *
86
     * @return null|string
87
     */
88 1 View Code Duplication
    public function getTranslation()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90 1
        $notes = $this->getAllInCategory('translation');
91 1
        foreach ($notes as $note) {
92
            if (isset($note['content'])) {
93
                return $note['content'];
94
            }
95
        }
96
97 1
        return null;
98
    }
99
100
    /**
101
     * @return bool
102
     */
103 2 View Code Duplication
    public function isApproved()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105 2
        $notes = $this->getAllInCategory('approved');
106 2
        foreach ($notes as $note) {
107 2
            if (isset($note['content'])) {
108 2
                return 'true' === $note['content'];
109
            }
110
        }
111
112 2
        return false;
113
    }
114
115
    /**
116
     * @param bool $bool
117
     */
118
    public function setApproved($bool)
119
    {
120
        $this->removeAllInCategory('approved');
121
        $this->addCategory('approved', $bool ? 'true' : 'false');
122
    }
123
124
    /**
125
     * @return array
126
     */
127 2
    public function getSourceLocations()
128
    {
129 2
        $sources = [];
130 2
        $notes = $this->getAllInCategory('file-source');
131 2
        foreach ($notes as $note) {
132 2
            if (!isset($note['content'])) {
133
                continue;
134
            }
135 2
            list($path, $line) = explode(':', $note['content'], 2);
136 2
            $sources[] = ['path' => $path, 'line' => $line];
137
        }
138
139 2
        return $sources;
140
    }
141
142
    /**
143
     * Add metadata.
144
     *
145
     * @param string $name
146
     * @param string $content
147
     */
148 1
    public function addCategory($name, $content, $priority = 1)
149
    {
150 1
        $this->notes[] = ['category' => $name, 'content' => $content, 'priority' => $priority];
151 1
    }
152
153
    /**
154
     * @return array
155
     */
156 1
    public function toArray()
157
    {
158 1
        $metadata = $this->metadata;
159 1
        $metadata['notes'] = $this->notes;
160
161 1
        return $metadata;
162
    }
163
164
    /**
165
     * Return all notes for one category. It will also order data according to priority.
166
     *
167
     * @param string $category
168
     *
169
     * @return array
170
     */
171 4
    public function getAllInCategory($category)
172
    {
173 4
        $data = [];
174 4
        foreach ($this->notes as $note) {
175 4
            if (!isset($note['category'])) {
176 2
                continue;
177
            }
178 4
            if ($note['category'] === $category) {
179 4
                if (!isset($note['priority'])) {
180 3
                    $note['priority'] = '1';
181
                }
182 4
                $data[] = $note;
183
            }
184
        }
185
186 4
        usort($data, function (array $a, array $b) {
187
            return (int) $a['priority'] - (int) $b['priority'];
188 4
        });
189
190 4
        return $data;
191
    }
192
193
    /**
194
     * Remove all metadata in category.
195
     *
196
     * @param string $category
197
     */
198 1
    public function removeAllInCategory($category)
199
    {
200 1
        foreach ($this->notes as $i => $note) {
201 1
            if (isset($note['category']) && $note['category'] === $category) {
202 1
                unset($this->notes[$i]);
203
            }
204
        }
205 1
    }
206
}
207