Passed
Push — master ( 2d425f...eb03d1 )
by Jan
04:57 queued 10s
created

ElementEditedLogEntry::getMessage()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\Entity\LogSystem;
44
45
use App\Entity\Base\AbstractDBElement;
46
use App\Entity\Contracts\LogWithCommentInterface;
47
use App\Entity\Contracts\LogWithEventUndoInterface;
48
use App\Entity\Contracts\TimeTravelInterface;
49
use Doctrine\ORM\Mapping as ORM;
50
51
/**
52
 * @ORM\Entity()
53
 */
54
class ElementEditedLogEntry extends AbstractLogEntry implements TimeTravelInterface, LogWithCommentInterface, LogWithEventUndoInterface
55
{
56
    protected $typeString = 'element_edited';
57
58
    public function __construct(AbstractDBElement $changed_element)
59
    {
60
        parent::__construct();
61
        $this->level = self::LEVEL_INFO;
62
63
        $this->setTargetElement($changed_element);
64
    }
65
66
    /**
67
     * Checks if this log contains infos about which fields has changed.
68
     * @return bool
69
     */
70
    public function hasChangedFieldsInfo(): bool
71
    {
72
        return isset($this->extra['f']) || $this->hasOldDataInformations();
73
    }
74
75
    /**
76
     * Return the names of all fields that were changed during the change.
77
     * @return string[]
78
     */
79
    public function getChangedFields(): array
80
    {
81
        if ($this->hasOldDataInformations()) {
82
            return array_keys($this->getOldData());
83
        }
84
85
        if (isset($this->extra['f'])) {
86
            return $this->extra['f'];
87
        }
88
89
        return [];
90
    }
91
92
    /**
93
     * Set the fields that were changed during this element change.
94
     * @param  string[]  $changed_fields The names of the fields that were changed during the elements
95
     * @return $this
96
     */
97
    public function setChangedFields(array $changed_fields): self
98
    {
99
        $this->extra['f'] = $changed_fields;
100
        return $this;
101
    }
102
103
    /**
104
     * Sets the old data for this entry.
105
     * @param array $old_data
106
     * @return $this
107
     */
108
    public function setOldData(array $old_data): self
109
    {
110
        $this->extra['d'] = $old_data;
111
        return $this;
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117
    public function hasOldDataInformations(): bool
118
    {
119
        return !empty($this->extra['d']);
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    public function getOldData(): array
126
    {
127
        return $this->extra['d'] ?? [];
128
    }
129
130
    /**
131
     * @inheritDoc
132
     */
133
    public function hasComment(): bool
134
    {
135
        return isset($this->extra['m']);
136
    }
137
138
    /**
139
     * @inheritDoc
140
     */
141
    public function getComment(): ?string
142
    {
143
        return $this->extra['m'] ?? null;
144
    }
145
146
    /**
147
     * @inheritDoc
148
     */
149
    public function setComment(?string $new_comment): LogWithCommentInterface
150
    {
151
        $this->extra['m'] = $new_comment;
152
        return $this;
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158
    public function isUndoEvent(): bool
159
    {
160
        return isset($this->extra['u']);
161
    }
162
163
    /**
164
     * @inheritDoc
165
     */
166
    public function getUndoEventID(): ?int
167
    {
168
        return $this->extra['u'] ?? null;
169
    }
170
171
    /**
172
     * @inheritDoc
173
     */
174
    public function setUndoneEvent(AbstractLogEntry $event, string $mode = 'undo'): LogWithEventUndoInterface
175
    {
176
        $this->extra['u'] = $event->getID();
177
178
        if ($mode === 'undo') {
179
            $this->extra['um'] = 1;
180
        } elseif ($mode === 'revert') {
181
            $this->extra['um'] = 2;
182
        } else {
183
            throw new \InvalidArgumentException('Passed invalid $mode!');
184
        }
185
186
        return $this;
187
    }
188
189
    /**
190
     * @inheritDoc
191
     */
192
    public function getUndoMode(): string
193
    {
194
        $mode_int = $this->extra['um'] ?? 1;
195
        if ($mode_int === 1) {
196
            return 'undo';
197
        } else {
198
            return 'revert';
199
        }
200
    }
201
}
202