Completed
Push — master ( 740372...dc5b6e )
by Tobias
10:09
created

Metadata::removeAllInCategory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
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\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 4
    public function __construct($metadata)
35
    {
36 4
        if (empty($metadata)) {
37 4
            $metadata = [];
38 4
        }
39
40 4
        $this->metadata = $metadata;
41 4
        if (isset($metadata['notes'])) {
42 2
            $this->notes = $metadata['notes'];
43 2
        }
44 4
    }
45
46
    /**
47
     * @return string|null
48
     */
49 2 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 2
        $notes = $this->getAllInCategory('state');
52 2
        foreach ($notes as $note) {
53 2
            if (isset($note['content'])) {
54 2
                return $note['content'];
55
            }
56 2
        }
57 2
    }
58
59
    /**
60
     * @param string $state
61
     */
62
    public function setState($state)
63
    {
64
        $this->removeAllInCategory('state');
65
        $this->addCategory('state', $state);
66
    }
67
68
    /**
69
     * @return bool
70
     */
71 1 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...
72
    {
73 1
        $notes = $this->getAllInCategory('approved');
74 1
        foreach ($notes as $note) {
75 1
            if (isset($note['content'])) {
76 1
                return $note['content'] === 'true';
77
            }
78 1
        }
79
80 1
        return false;
81
    }
82
83
    /**
84
     * @param bool $bool
85
     */
86
    public function setApproved($bool)
87
    {
88
        $this->removeAllInCategory('approved');
89
        $this->addCategory('approved', $bool ? 'true' : 'false');
90
    }
91
92
    /**
93
     * @return array
94
     */
95 1
    public function getSourceLocations()
96
    {
97 1
        $sources = [];
98 1
        $notes = $this->getAllInCategory('file-source');
99 1
        foreach ($notes as $note) {
100
            if (!isset($note['content'])) {
101
                continue;
102
            }
103
            list($path, $line) = explode(':', $note['content'], 2);
104
            $sources[] = ['path' => $path, 'line' => $line];
105 1
        }
106
107 1
        return $sources;
108
    }
109
110
    /**
111
     * Add metadata.
112
     *
113
     * @param string $name
114
     * @param string $content
115
     */
116
    public function addCategory($name, $content, $priority = 1)
117
    {
118
        $this->notes[] = ['category' => $name, 'content' => $content, 'priority' => $priority];
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    public function toArray()
125
    {
126
        $metadata = $this->metadata;
127
        $metadata['notes'] = $this->notes;
128
129
        return $metadata;
130
    }
131
132
    /**
133
     * Return all notes for one category. It will also order data according to priority.
134
     *
135
     * @param string $category
136
     *
137
     * @return array
138
     */
139 2
    public function getAllInCategory($category)
140
    {
141 2
        $data = [];
142 2
        foreach ($this->notes as &$note) {
143 2
            if (!isset($note['category'])) {
144
                continue;
145
            }
146 2
            if ($note['category'] === $category) {
147 2
                if (!isset($note['priority'])) {
148 2
                    $note['priority'] = '1';
149 2
                }
150 2
                $data[] = $note;
151 2
            }
152 2
        }
153
154 2
        usort($data, function (array $a, array $b) {
155
            return (int) $a['priority'] - (int) $b['priority'];
156 2
        });
157
158 2
        return $data;
159
    }
160
161
    /**
162
     * Remove all metadata in category.
163
     *
164
     * @param string $category
165
     */
166
    public function removeAllInCategory($category)
167
    {
168
        foreach ($this->notes as $i => $note) {
169
            if ($note['category'] === $category) {
170
                unset($this->notes[$i]);
171
            }
172
        }
173
    }
174
}
175