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

LogMessage::addDetailsHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
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\Messages;
26
27
/**
28
 * Class LogMessage
29
 *
30
 * @package SP\Core\Messages
31
 */
32
final class LogMessage extends MessageBase
33
{
34
    /**
35
     * @var string Acción realizada
36
     */
37
    protected $action;
38
    /**
39
     * @var array Detalles de la acción en formato "detalle : descripción"
40
     */
41
    protected $details = [];
42
    /**
43
     * @var int
44
     */
45
    protected $descriptionCounter = 0;
46
    /**
47
     * @var int
48
     */
49
    protected $detailsCounter = 0;
50
51
    /**
52
     * Establece los detalles de la acción realizada
53
     *
54
     * @param $key   string
55
     * @param $value string
56
     *
57
     * @return $this
58
     */
59
    public function addDetails($key, $value)
60
    {
61
        if ($value === '' || $key === '') {
62
            return $this;
63
        }
64
65
        $this->details[] = [$this->formatString($key), $this->formatString($value)];
66
        $this->detailsCounter++;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Formatear una cadena para guardarla en el registro
73
     *
74
     * @param $string string La cadena a formatear
75
     *
76
     * @return string
77
     */
78
    private function formatString($string)
79
    {
80
        return strip_tags($string);
81
    }
82
83
    /**
84
     * Establece la descripción de la acción realizada
85
     *
86
     * @param string $description
87
     *
88
     * @return $this
89
     */
90
    public function addDescription($description = '')
91
    {
92
        $this->description[] = $this->formatString($description);
93
94
        return $this;
95
    }
96
97
    /**
98
     * Añadir una línea en blanco a la descripción
99
     */
100
    public function addDescriptionLine()
101
    {
102
        $this->description[] = '';
103
        $this->descriptionCounter++;
104
105
        return $this;
106
    }
107
108
    /**
109
     * Componer un mensaje en formato texto
110
     *
111
     * @param string $delimiter
112
     *
113
     * @return string
114
     */
115
    public function composeText($delimiter = PHP_EOL)
116
    {
117
        $formatter = new TextFormatter();
118
119
        $message[] = $this->getAction(true);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$message was never initialized. Although not strictly required by PHP, it is generally a good practice to add $message = array(); before regardless.
Loading history...
120
        $message[] = $this->getDescription($formatter, true);
121
        $message[] = $this->getDetails($formatter, true);
122
123
        return implode(PHP_EOL, $message);
124
    }
125
126
    /**
127
     * Devuelve la acción realizada
128
     *
129
     * @param bool $translate
130
     *
131
     * @return string
132
     */
133
    public function getAction($translate = false)
134
    {
135
        return $translate ? __($this->action) : $this->action;
136
    }
137
138
    /**
139
     * Establece la acción realizada
140
     *
141
     * @param string $action
142
     *
143
     * @return $this
144
     */
145
    public function setAction($action)
146
    {
147
        $this->action = $this->formatString($action);
148
149
        return $this;
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): string
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, $translate = false): string
178
    {
179
        if (count($this->details) === 0) {
180
            return '';
181
        }
182
183
        return $formatter->formatDetail($this->details, $translate);
184
    }
185
186
    /**
187
     * Componer un mensaje en formato HTML
188
     *
189
     * @return mixed
190
     */
191
    public function composeHtml()
192
    {
193
        $formatter = new HtmlFormatter();
194
195
        $message = '<div class="log-message">';
196
        $message .= '<h1>' . $this->action . '</h1>';
197
        $message .= '<div class="log-description">' . $this->getDescription($formatter, true) . '</div>';
198
        $message .= '<div class="log-details">' . $this->getDetails($formatter, true) . '</div>';
199
        $message .= '<footer>' . $this->footer . '</footer>';
0 ignored issues
show
Bug introduced by
Are you sure $this->footer of type array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

199
        $message .= '<footer>' . /** @scrutinizer ignore-type */ $this->footer . '</footer>';
Loading history...
200
        $message .= '</div>';
201
202
        return $message;
203
    }
204
205
    /**
206
     * Restablecer la variable de descripcion
207
     */
208
    public function resetDescription()
209
    {
210
        $this->description = [];
211
        $this->descriptionCounter = 0;
212
213
        return $this;
214
    }
215
216
    /**
217
     * Restablecer la variable de detalles
218
     */
219
    public function resetDetails()
220
    {
221
        $this->details = [];
222
        $this->detailsCounter = 0;
223
224
        return $this;
225
    }
226
227
    /**
228
     * @return int
229
     */
230
    public function getDescriptionCounter()
231
    {
232
        return $this->descriptionCounter;
233
    }
234
235
    /**
236
     * @return int
237
     */
238
    public function getDetailsCounter()
239
    {
240
        return $this->detailsCounter;
241
    }
242
}