Passed
Push — master ( f08540...e0c380 )
by Jan
07:05
created

getAdditionalElement()   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
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 *  Copyright (C) 2019 - 2023 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\Form\Type\Helper;
22
23
use App\Entity\Base\AbstractDBElement;
24
use App\Entity\Base\AbstractStructuralDBElement;
25
use App\Repository\StructuralDBElementRepository;
0 ignored issues
show
Bug introduced by
The type App\Repository\StructuralDBElementRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use App\Services\Trees\NodesListBuilder;
27
use Doctrine\ORM\EntityManagerInterface;
28
use Symfony\Component\Form\ChoiceList\Loader\AbstractChoiceLoader;
29
use Symfony\Component\OptionsResolver\Options;
30
31
class StructuralEntityChoiceLoader extends AbstractChoiceLoader
32
{
33
    private Options $options;
34
    private NodesListBuilder $builder;
35
    private EntityManagerInterface $entityManager;
36
37
    private ?string $additional_element = null;
38
39
    public function __construct(Options $options, NodesListBuilder $builder, EntityManagerInterface $entityManager)
40
    {
41
        $this->options = $options;
42
        $this->builder = $builder;
43
        $this->entityManager = $entityManager;
44
    }
45
46
    protected function loadChoices(): iterable
47
    {
48
        $tmp = [];
49
        if ($this->additional_element) {
50
            $tmp = $this->createNewEntitiesFromValue($this->additional_element);
51
            $this->additional_element = null;
52
        }
53
54
        return array_merge($tmp, $this->builder->typeToNodesList($this->options['class'], null));
55
    }
56
57
    /*public function loadChoicesForValues(array $values, callable $value = null)
58
    {
59
        $tmp = parent::loadChoicesForValues($values, $value);
60
61
        if ($this->options['allow_add'] && empty($tmp)) {
62
            if (count($values) > 1) {
63
                throw new \InvalidArgumentException('Cannot add multiple entities at once.');
64
            }
65
66
            //Dont create a new entity for the empty option
67
            if ($values[0] === "" || $values[0] === null) {
68
                return $tmp;
69
            }
70
71
            return [$this->createNewEntitiesFromValue($values[0])[0]];
72
        }
73
74
        return $tmp;
75
    }*/
76
77
    public function createNewEntitiesFromValue(string $value): array
78
    {
79
        if (!$this->options['allow_add']) {
80
            throw new \RuntimeException('Cannot create new entity, because allow_add is not enabled!');
81
        }
82
83
        if (trim($value) === '') {
84
            throw new \InvalidArgumentException('Cannot create new entity, because the name is empty!');
85
        }
86
87
        $class = $this->options['class'];
88
        /** @var StructuralDBElementRepository $repo */
89
        $repo = $this->entityManager->getRepository($class);
90
91
        $entities = $repo->getNewEntityFromPath($value, '->');
92
93
        $results = [];
94
95
        foreach($entities as $entity) {
96
            //If the entity is newly created (ID null), add it as result and persist it.
97
            if ($entity->getID() === null) {
98
                $this->entityManager->persist($entity);
99
                $results[] = $entity;
100
            }
101
        }
102
103
        return $results;
104
    }
105
106
    public function setAdditionalElement(?string $element): void
107
    {
108
        $this->additional_element = $element;
109
    }
110
111
    public function getAdditionalElement(): ?string
112
    {
113
        return $this->additional_element;
114
    }
115
116
}