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

DBElementRepository::setField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
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
namespace App\Repository;
22
23
24
use App\Entity\Base\AbstractDBElement;
25
use Doctrine\ORM\EntityRepository;
26
27
class DBElementRepository extends EntityRepository
28
{
29
    /**
30
     * Changes the ID of the given element to a new value.
31
     * You should only use it to undelete former existing elements, everything else is most likely a bad idea!
32
     * @param  AbstractDBElement  $element The element whose ID should be changed
33
     * @param  int  $new_id The new ID
34
     */
35
    public function changeID(AbstractDBElement $element, int $new_id): void
36
    {
37
        $qb = $this->createQueryBuilder('element');
38
        $q = $qb->update(get_class($element), 'element')
39
            ->set('element.id', $new_id)
40
            ->where('element.id = ?1')
41
            ->setParameter(1, $element->getID())
42
            ->getQuery();
43
44
        $p = $q->execute();
0 ignored issues
show
Unused Code introduced by
The assignment to $p is dead and can be removed.
Loading history...
45
46
        $this->setField($element, 'id', $new_id);
47
    }
48
49
    protected function setField(AbstractDBElement $element, string $field, $new_value)
50
    {
51
        $reflection = new \ReflectionClass(get_class($element));
52
        $property = $reflection->getProperty($field);
53
        $property->setAccessible(true);
54
        $property->setValue($element, $new_value);
55
    }
56
}