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

CurrencyEntityType::generateChoiceAttr()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 4
dl 0
loc 21
rs 9.8666
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 - 2022 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
namespace App\Form\Type;
24
25
use App\Entity\Attachments\AttachmentType;
26
use App\Entity\Base\AbstractStructuralDBElement;
27
use App\Entity\PriceInformations\Currency;
28
use App\Services\Attachments\AttachmentURLGenerator;
29
use App\Services\Trees\NodesListBuilder;
30
use Doctrine\ORM\EntityManagerInterface;
31
use RuntimeException;
32
use Symfony\Component\Intl\Currencies;
33
use Symfony\Component\OptionsResolver\Options;
34
use Symfony\Component\OptionsResolver\OptionsResolver;
35
36
/**
37
 * An entity to select a currency shortly
38
 */
39
class CurrencyEntityType extends StructuralEntityType
40
{
41
    protected ?string $base_currency;
42
43
    public function __construct(EntityManagerInterface $em, NodesListBuilder $builder, AttachmentURLGenerator $attachmentURLGenerator, ?string $base_currency)
44
    {
45
        parent::__construct($em, $builder, $attachmentURLGenerator);
46
        $this->base_currency = $base_currency;
47
    }
48
49
    public function configureOptions(OptionsResolver $resolver): void
50
    {
51
        //Important to call the parent resolver!
52
        parent::configureOptions($resolver);
53
54
        $resolver->setDefault('class', Currency::class);
55
        $resolver->setDefault('disable_not_selectable', true);
56
        $resolver->setDefault('choice_translation_domain', false);
57
58
        // This options allows you to override the currency shown for the null value
59
        $resolver->setDefault('base_currency', null);
60
61
        $resolver->setDefault('empty_message', function (Options $options) {
62
            //By default we use the global base currency:
63
            $iso_code = $this->base_currency;
64
65
            if ($options['base_currency']) { //Allow to override it
66
                $iso_code = $options['base_currency'];
67
            }
68
69
            return Currencies::getSymbol($iso_code);
0 ignored issues
show
Bug introduced by
It seems like $iso_code can also be of type null; however, parameter $currency of Symfony\Component\Intl\Currencies::getSymbol() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

69
            return Currencies::getSymbol(/** @scrutinizer ignore-type */ $iso_code);
Loading history...
70
        });
71
72
        $resolver->setDefault('used_to_select_parent', false);
73
74
        //If short is set to true, then the name of the entity will only shown in the dropdown list not in the selected value.
75
        $resolver->setDefault('short', false);
76
    }
77
78
    protected function generateChoiceAttr(AbstractStructuralDBElement $choice, $key, $value, $options): array
79
    {
80
        $tmp = parent::generateChoiceAttr($choice, $key, $value, $options);
81
82
        if(!empty($choice->getIsoCode())) {
0 ignored issues
show
Bug introduced by
The method getIsoCode() does not exist on App\Entity\Base\AbstractStructuralDBElement. It seems like you code against a sub-type of App\Entity\Base\AbstractStructuralDBElement such as App\Entity\PriceInformations\Currency. ( Ignorable by Annotation )

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

82
        if(!empty($choice->/** @scrutinizer ignore-call */ getIsoCode())) {
Loading history...
83
            $symbol = Currencies::getSymbol($choice->getIsoCode());
84
        } else {
85
            $symbol = null;
86
        }
87
88
        if ($options['short']) {
89
            $tmp['data-short'] = $symbol;
90
        } else {
91
            $tmp['data-short'] = $choice->getName();
92
        }
93
94
        $tmp += [
95
            'data-symbol' => $symbol,
96
        ];
97
98
        return $tmp;
99
    }
100
101
    protected function getChoiceContent(AbstractStructuralDBElement $choice, $key, $value, $options): string
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

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

101
    protected function getChoiceContent(AbstractStructuralDBElement $choice, $key, /** @scrutinizer ignore-unused */ $value, $options): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

101
    protected function getChoiceContent(AbstractStructuralDBElement $choice, /** @scrutinizer ignore-unused */ $key, $value, $options): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103
        if(!$choice instanceof Currency) {
104
            throw new RuntimeException('$choice must be an instance of Currency!');
105
        }
106
107
        //Generate the level spacing
108
        /** @var AbstractStructuralDBElement|null $parent */
109
        $parent = $options['subentities_of'];
110
        /*** @var AbstractStructuralDBElement $choice */
111
        $level = $choice->getLevel();
112
        //If our base entity is not the root level, we need to change the level, to get zero position
113
        if (null !== $options['subentities_of']) {
114
            $level -= $parent->getLevel() - 1;
0 ignored issues
show
Bug introduced by
The method getLevel() does not exist on null. ( Ignorable by Annotation )

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

114
            $level -= $parent->/** @scrutinizer ignore-call */ getLevel() - 1;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
        }
116
117
        $tmp = str_repeat('<span class="picker-level"></span>', $level);
118
119
        //Show currency symbol or ISO code and the name of the currency
120
        if(!empty($choice->getIsoCode())) {
121
            $tmp .= Currencies::getSymbol($choice->getIsoCode());
122
            //Add currency name as badge
123
            $tmp .= sprintf('<span class="badge bg-primary ms-2 %s">%s</span>', $options['short'] ? 'picker-hs' : '' , htmlspecialchars($choice->getName()));
124
        } else {
125
            $tmp .= htmlspecialchars($choice->getName());
126
        }
127
128
        return $tmp;
129
    }
130
131
}
132