Completed
Push — master ( 0e13cf...da6bb4 )
by Tobias
09:42
created

Metadata::getDesc()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 11
loc 11
ccs 4
cts 6
cp 0.6667
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
crap 3.3332
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
     * @return bool
85
     */
86 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...
87
    {
88 2
        $notes = $this->getAllInCategory('approved');
89 2
        foreach ($notes as $note) {
90 2
            if (isset($note['content'])) {
91 2
                return 'true' === $note['content'];
92
            }
93
        }
94
95 2
        return false;
96
    }
97
98
    /**
99
     * @param bool $bool
100
     */
101
    public function setApproved($bool)
102
    {
103
        $this->removeAllInCategory('approved');
104
        $this->addCategory('approved', $bool ? 'true' : 'false');
105
    }
106
107
    /**
108
     * @return array
109
     */
110 2
    public function getSourceLocations()
111
    {
112 2
        $sources = [];
113 2
        $notes = $this->getAllInCategory('file-source');
114 2
        foreach ($notes as $note) {
115 2
            if (!isset($note['content'])) {
116
                continue;
117
            }
118 2
            list($path, $line) = explode(':', $note['content'], 2);
119 2
            $sources[] = ['path' => $path, 'line' => $line];
120
        }
121
122 2
        return $sources;
123
    }
124
125
    /**
126
     * Add metadata.
127
     *
128
     * @param string $name
129
     * @param string $content
130
     */
131 1
    public function addCategory($name, $content, $priority = 1)
132
    {
133 1
        $this->notes[] = ['category' => $name, 'content' => $content, 'priority' => $priority];
134 1
    }
135
136
    /**
137
     * @return array
138
     */
139 1
    public function toArray()
140
    {
141 1
        $metadata = $this->metadata;
142 1
        $metadata['notes'] = $this->notes;
143
144 1
        return $metadata;
145
    }
146
147
    /**
148
     * Return all notes for one category. It will also order data according to priority.
149
     *
150
     * @param string $category
151
     *
152
     * @return array
153
     */
154 4
    public function getAllInCategory($category)
155
    {
156 4
        $data = [];
157 4
        foreach ($this->notes as $note) {
158 4
            if (!isset($note['category'])) {
159 2
                continue;
160
            }
161 4
            if ($note['category'] === $category) {
162 4
                if (!isset($note['priority'])) {
163 3
                    $note['priority'] = '1';
164
                }
165 4
                $data[] = $note;
166
            }
167
        }
168
169 4
        usort($data, function (array $a, array $b) {
170
            return (int) $a['priority'] - (int) $b['priority'];
171 4
        });
172
173 4
        return $data;
174
    }
175
176
    /**
177
     * Remove all metadata in category.
178
     *
179
     * @param string $category
180
     */
181 1
    public function removeAllInCategory($category)
182
    {
183 1
        foreach ($this->notes as $i => $note) {
184 1
            if (isset($note['category']) && $note['category'] === $category) {
185 1
                unset($this->notes[$i]);
186
            }
187
        }
188 1
    }
189
}
190