BasePriceUnitChoiceType   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 51
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A configureOptions() 0 26 4
A getParent() 0 4 1
A getBlockPrefix() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecocode\SyliusBasePricePlugin\Form\Type;
6
7
use Symfony\Component\Form\AbstractType;
8
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
use Symfony\Contracts\Translation\TranslatorInterface;
11
use UnitConverter\UnitConverter;
12
13
/**
14
 * Class BasePriceUnitChoiceType
15
 * @package Ecocode\SyliusBasePricePlugin\Form\Type
16
 */
17
class BasePriceUnitChoiceType extends AbstractType
18
{
19
    /** @var TranslatorInterface */
20
    private $translator;
21
22
    /** @var UnitConverter */
23
    private $converter;
24
25 3
    public function __construct(TranslatorInterface $translator, UnitConverter $converter)
26
    {
27 3
        $this->translator = $translator;
28 3
        $this->converter  = $converter;
29 3
    }
30
31 1
    public function configureOptions(OptionsResolver $resolver): void
32
    {
33 1
        $translated = ['choice_translation_domain' => false];
34 1
        $registry   = $this->converter->getRegistry();
35
36
        /** @var string[] $measurements */
37 1
        $measurements = $registry->listMeasurements();
38
39 1
        foreach ($measurements as $measurement) {
40
            /** @var string[] $units */
41 1
            $units = $registry->listUnits($measurement);
42 1
            foreach ($units as $symbol) {
43 1
                $unit = $registry->loadUnit($symbol);
44 1
                if ($unit === null) {
45
                    continue;
46
                }
47
48 1
                $symbol = (string)$unit->getSymbol();
49 1
                $text   = sprintf('%s (%s)', (string)$unit->getName(), $symbol);
50
51 1
                $translated['choices'][$measurement][$text] = $symbol;
52
            }
53
        }
54
55 1
        $resolver->setDefaults($translated);
56 1
    }
57
58 1
    public function getParent(): string
59
    {
60 1
        return ChoiceType::class;
61
    }
62
63 1
    public function getBlockPrefix(): string
64
    {
65 1
        return 'sylius_base_price_unit_choice';
66
    }
67
}
68