Passed
Push — 3.0 ( b611c9...572608 )
by Rubén
03:38
created

EventMessage::factory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Core\Events;
26
27
use SP\Core\Messages\FormatterInterface;
28
use SP\Core\Messages\HtmlFormatter;
29
use SP\Core\Messages\MessageInterface;
30
use SP\Core\Messages\TextFormatter;
31
32
/**
33
 * Class EventMessage
34
 *
35
 * @package SP\Core\Events
36
 */
37
final class EventMessage implements MessageInterface
38
{
39
    /**
40
     * @var array Detalles de la acción en formato "detalle : descripción"
41
     */
42
    protected $details = [];
43
    /**
44
     * @var int
45
     */
46
    protected $descriptionCounter = 0;
47
    /**
48
     * @var int
49
     */
50
    protected $detailsCounter = 0;
51
    /**
52
     * @var array
53
     */
54
    protected $description = [];
55
    /**
56
     * @var array
57
     */
58
    protected $extra = [];
59
60
    /**
61
     * @return static
62
     */
63
    public static function factory()
64
    {
65
        return new static();
66
    }
67
68
    /**
69
     * Devuelve la descripción
70
     *
71
     * @return array
72
     */
73
    public function getDescriptionRaw()
74
    {
75
        return $this->description;
76
    }
77
78
    /**
79
     * Establece los detalles de la acción realizada
80
     *
81
     * @param $key   string
82
     * @param $value string
83
     *
84
     * @return $this
85
     */
86
    public function addDetail($key, $value)
87
    {
88
        if ($value === '' || $key === '') {
89
            return $this;
90
        }
91
92
        $this->details[] = [$this->formatString($key), $this->formatString($value)];
93
94
        $this->detailsCounter++;
95
96
        return $this;
97
    }
98
99
    /**
100
     * Formatear una cadena para guardarla en el registro
101
     *
102
     * @param $string string La cadena a formatear
103
     *
104
     * @return string
105
     */
106
    private function formatString($string)
107
    {
108
        return strip_tags($string);
109
    }
110
111
    /**
112
     * Establece la descripción de la acción realizada
113
     *
114
     * @param string $description
115
     *
116
     * @return $this
117
     */
118
    public function addDescription($description = '')
119
    {
120
        $this->description[] = $this->formatString($description);
121
122
        return $this;
123
    }
124
125
    /**
126
     * Añadir una línea en blanco a la descripción
127
     */
128
    public function addDescriptionLine()
129
    {
130
        $this->descriptionCounter++;
131
132
        return $this;
133
    }
134
135
    /**
136
     * Componer un mensaje en formato texto
137
     *
138
     * @param string $delimiter
139
     *
140
     * @return string
141
     */
142
    public function composeText($delimiter = PHP_EOL)
143
    {
144
        $formatter = new TextFormatter();
145
146
        return implode($delimiter, [
147
            $this->getDescription($formatter, true),
148
            $this->getDetails($formatter, true)
149
        ]);
150
    }
151
152
    /**
153
     * Devuelve la descripción de la acción realizada
154
     *
155
     * @param FormatterInterface $formatter
156
     * @param bool               $translate
157
     *
158
     * @return string
159
     */
160
    public function getDescription(FormatterInterface $formatter, $translate = false)
161
    {
162
        if (count($this->description) === 0) {
163
            return '';
164
        }
165
166
        return $formatter->formatDescription($this->description, $translate);
167
    }
168
169
    /**
170
     * Devuelve los detalles de la acción realizada
171
     *
172
     * @param FormatterInterface $formatter
173
     * @param bool               $translate
174
     *
175
     * @return string
176
     */
177
    public function getDetails(FormatterInterface $formatter, bool $translate = false)
178
    {
179
        if (count($this->details) === 0) {
180
            return '';
181
        }
182
183
        return $formatter->formatDetail($this->details, $translate);
184
    }
185
186
    /**
187
     * Devuelve los detalles
188
     *
189
     * @return array
190
     */
191
    public function getDetailsRaw()
192
    {
193
        return $this->details;
194
    }
195
196
    /**
197
     * Componer un mensaje en formato HTML
198
     *
199
     * @return mixed
200
     */
201
    public function composeHtml()
202
    {
203
        $formatter = new HtmlFormatter();
204
205
        $message = '<div class="event-message">';
206
        $message .= '<div class="event-description">' . $this->getDescription($formatter, true) . '</div>';
207
        $message .= '<div class="event-details">' . $this->getDetails($formatter, true) . '</div>';
208
        $message .= '</div>';
209
210
        return $message;
211
    }
212
213
    /**
214
     * @return int
215
     */
216
    public function getDescriptionCounter()
217
    {
218
        return $this->descriptionCounter;
219
    }
220
221
    /**
222
     * @return int
223
     */
224
    public function getDetailsCounter()
225
    {
226
        return $this->detailsCounter;
227
    }
228
229
    /**
230
     * @return array
231
     */
232
    public function getExtra()
233
    {
234
        return $this->extra;
235
    }
236
237
    /**
238
     * @param       $type
239
     * @param array $data
240
     *
241
     * @return EventMessage
242
     */
243
    public function setExtra($type, array $data)
244
    {
245
        if (isset($this->extra[$type])) {
246
            $this->extra[$type] = array_merge($this->extra[$type], $data);
247
        } else {
248
            $this->extra[$type] = $data;
249
        }
250
251
        return $this;
252
    }
253
254
    /**
255
     * Extra data are stored as an array of values per key, thus each key is unique
256
     *
257
     * @param string $type
258
     * @param mixed  $data
259
     *
260
     * @return EventMessage
261
     */
262
    public function addExtra($type, $data)
263
    {
264
        if (isset($this->extra[$type]) && in_array($data, $this->extra[$type])) {
265
            return $this;
266
        }
267
268
        $this->extra[$type][] = $data;
269
270
        return $this;
271
    }
272
}