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

CollectionElementDeleted   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 100
rs 10
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getDeletedElementID() 0 3 1
A getUndoMode() 0 7 2
A setUndoneEvent() 0 13 3
A getUndoEventID() 0 3 1
A getDeletedElementClass() 0 3 1
A __construct() 0 11 2
A isUndoEvent() 0 3 1
A getOldName() 0 3 1
A getCollectionName() 0 3 1
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
namespace App\Entity\LogSystem;
22
23
24
use App\Entity\Base\AbstractDBElement;
25
use App\Entity\Contracts\LogWithEventUndoInterface;
26
use App\Entity\Contracts\NamedElementInterface;
27
use Doctrine\ORM\Mapping as ORM;
28
29
/**
30
 * @ORM\Entity()
31
 * This log entry is created when an element is deleted, that is used in a collection of an other entity.
32
 * This is needed to signal time travel, that it has to undelete the deleted entity.
33
 */
34
class CollectionElementDeleted extends AbstractLogEntry implements LogWithEventUndoInterface
35
{
36
    protected $typeString = 'collection_element_deleted';
37
    protected $level = self::LEVEL_INFO;
38
39
    public function __construct(AbstractDBElement $changed_element, string $collection_name, AbstractDBElement $deletedElement)
40
    {
41
        parent::__construct();
42
43
        $this->level = self::LEVEL_INFO;
44
        $this->setTargetElement($changed_element);
45
        $this->extra['n'] = $collection_name;
46
        $this->extra['c'] = self::targetTypeClassToID(get_class($deletedElement));
47
        $this->extra['i'] = $deletedElement->getID();
48
        if ($deletedElement instanceof NamedElementInterface) {
49
            $this->extra['o'] = $deletedElement->getName();
50
        }
51
    }
52
53
    /**
54
     * Get the name of the collection (on target element) that was changed.
55
     * @return string
56
     */
57
    public function getCollectionName(): string
58
    {
59
        return $this->extra['n'];
60
    }
61
62
    /**
63
     * Gets the name of the element that was deleted.
64
     * Return null, if the element did not have a name.
65
     * @return string|null
66
     */
67
    public function getOldName(): ?string
68
    {
69
        return $this->extra['o'] ?? null;
70
    }
71
72
    /**
73
     * Returns the class of the deleted element.
74
     * @return string
75
     */
76
    public function getDeletedElementClass(): string
77
    {
78
        return self::targetTypeIdToClass($this->extra['c']);
79
    }
80
81
    /**
82
     * Returns the ID of the deleted element.
83
     * @return int
84
     */
85
    public function getDeletedElementID(): int
86
    {
87
        return $this->extra['i'];
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    public function isUndoEvent(): bool
94
    {
95
        return isset($this->extra['u']);
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function getUndoEventID(): ?int
102
    {
103
        return $this->extra['u'] ?? null;
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    public function setUndoneEvent(AbstractLogEntry $event, string $mode = 'undo'): LogWithEventUndoInterface
110
    {
111
        $this->extra['u'] = $event->getID();
112
113
        if ($mode === 'undo') {
114
            $this->extra['um'] = 1;
115
        } elseif ($mode === 'revert') {
116
            $this->extra['um'] = 2;
117
        } else {
118
            throw new \InvalidArgumentException('Passed invalid $mode!');
119
        }
120
121
        return $this;
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127
    public function getUndoMode(): string
128
    {
129
        $mode_int = $this->extra['um'] ?? 1;
130
        if ($mode_int === 1) {
131
            return 'undo';
132
        } else {
133
            return 'revert';
134
        }
135
    }
136
}