SfProfilerMessage::getCount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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
use Symfony\Component\VarDumper\Cloner\Data;
15
use Translation\Common\Model\Message;
16
use Translation\Common\Model\MessageInterface;
17
18
/**
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
final class SfProfilerMessage
22
{
23
    /**
24
     * @var int
25
     *
26
     * This is the number of times the message occurs on a specific page
27
     */
28
    private $count;
29
30
    /**
31
     * @var string
32
     *
33
     * The domain the message belongs to
34
     */
35
    private $domain;
36
37
    /**
38
     * @var string
39
     *
40
     * The key/phrase you write in the source code
41
     */
42
    private $key;
43
44
    /**
45
     * @var string
46
     *
47
     * The locale the translations is on
48
     */
49
    private $locale;
50
51
    /**
52
     * @var int
53
     *
54
     * The current state of the translations. See Symfony\Component\Translation\DataCollectorTranslator
55
     *
56
     * MESSAGE_DEFINED = 0;
57
     * MESSAGE_MISSING = 1;
58
     * MESSAGE_EQUALS_FALLBACK = 2;
59
     */
60
    private $state;
61
62
    /**
63
     * @var string
64
     *
65
     * The translated string. This is the preview of the message. Ie no placeholders is visible.
66
     */
67
    private $translation;
68
69
    /**
70
     * @var int
71
     *
72
     * The number which we are feeding a transChoice with
73
     */
74
    private $transChoiceNumber;
75
76
    /**
77
     * @var array
78
     *
79
     * The parameters sent to the translations
80
     */
81
    private $parameters;
82
83
    /**
84
     * @return SfProfilerMessage
85
     */
86
    public static function create(array $data): self
87
    {
88
        $message = new self();
89
        if (isset($data['id'])) {
90
            $message->setKey($data['id']);
91
        }
92
        if (isset($data['domain'])) {
93
            $message->setDomain($data['domain']);
94
        }
95
        if (isset($data['locale'])) {
96
            $message->setLocale($data['locale']);
97
        }
98
        if (isset($data['translation'])) {
99
            $message->setTranslation($data['translation']);
100
        }
101
        if (isset($data['state'])) {
102
            $message->setState($data['state']);
103
        }
104
        if (isset($data['count'])) {
105
            $message->setCount($data['count']);
106
        }
107
        if (isset($data['transChoiceNumber'])) {
108
            $message->setTransChoiceNumber($data['transChoiceNumber']);
109
        }
110
        if (isset($data['parameters'])) {
111
            $message->setParameters($data['parameters']);
112
        }
113
114
        return $message;
115
    }
116
117
    /**
118
     * Convert to a Common\Model\MessageInterface.
119
     */
120
    public function convertToMessage(): MessageInterface
121
    {
122
        $meta = [];
123
124
        if ($this->hasParameters()) {
125
            // Reduce to only get one value of each parameter, not all the usages.
126
            $meta['parameters'] = \array_reduce($this->getParameters(), 'array_merge', []);
127
        }
128
129
        if (!empty($this->getCount())) {
130
            $meta['count'] = $this->getCount();
131
        }
132
133
        if (!empty($this->getTransChoiceNumber())) {
134
            $meta['transChoiceNumber'] = $this->getTransChoiceNumber();
135
        }
136
137
        return new Message(
138
            $this->key,
139
            $this->domain,
140
            $this->locale,
141
            $this->translation,
142
            $meta
143
        );
144
    }
145
146
    public function getCount(): int
147
    {
148
        return $this->count;
149
    }
150
151
    public function setCount(int $count): self
152
    {
153
        $this->count = $count;
154
155
        return $this;
156
    }
157
158
    public function getDomain(): string
159
    {
160
        return $this->domain;
161
    }
162
163
    public function setDomain(string $domain): self
164
    {
165
        $this->domain = $domain;
166
167
        return $this;
168
    }
169
170
    public function getKey(): string
171
    {
172
        return $this->key;
173
    }
174
175
    public function setKey($key): self
176
    {
177
        $this->key = $key;
178
179
        return $this;
180
    }
181
182
    public function getLocale(): string
183
    {
184
        return $this->locale;
185
    }
186
187
    public function setLocale(string $locale): self
188
    {
189
        $this->locale = $locale;
190
191
        return $this;
192
    }
193
194
    public function getState(): int
195
    {
196
        return $this->state;
197
    }
198
199
    public function setState(int $state): self
200
    {
201
        $this->state = $state;
202
203
        return $this;
204
    }
205
206
    public function getTranslation(): string
207
    {
208
        return $this->translation;
209
    }
210
211
    public function setTranslation(string $translation): self
212
    {
213
        $this->translation = $translation;
214
215
        return $this;
216
    }
217
218
    public function getTransChoiceNumber(): ?int
219
    {
220
        return $this->transChoiceNumber;
221
    }
222
223
    public function setTransChoiceNumber(int $transChoiceNumber): self
224
    {
225
        $this->transChoiceNumber = $transChoiceNumber;
226
227
        return $this;
228
    }
229
230
    public function getParameters(): array
231
    {
232
        $pure = [];
233
        foreach ($this->parameters as $p) {
234
            if ($p instanceof Data) {
235
                $p = $p->getValue(true);
236
            }
237
            $pure[] = $p;
238
        }
239
240
        return $pure;
241
    }
242
243
    public function hasParameters(): bool
244
    {
245
        return !empty($this->parameters);
246
    }
247
248
    public function setParameters(array $parameters): self
249
    {
250
        $this->parameters = $parameters;
251
252
        return $this;
253
    }
254
}
255