Completed
Push — master ( 3674b7...0e13cf )
by Tobias
33:57
created

Metadata::getState()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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