AbstractBuilder   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 56
ccs 19
cts 19
cp 1
rs 10
c 2
b 0
f 0
eloc 18
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getOptions() 0 3 1
A twig() 0 23 1
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Inputs\Builder;
11
12
use Symfony\Component\Form\FormBuilderInterface;
13
14
abstract class AbstractBuilder implements BuilderInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $name;
20
21
    /**
22
     * @var array<string>
23
     */
24
    protected $options;
25
26 67
    public function getName(): string
27
    {
28 67
        return \preg_replace('/(\s)+/', '_', \trim($this->name ?? '')) ?? $this->name;
29
    }
30
31
    /**
32
     * @return array<string>
33
     */
34 7
    public function getOptions(): array
35
    {
36 7
        return $this->options;
37
    }
38
39
    /**
40
     * @return FormBuilderInterface<string>
41
     */
42
    abstract protected function factory(): FormBuilderInterface;
43
44
    /**
45
     * @return \Twig\Environment
46
     */
47 67
    protected function twig()
48
    {
49 67
        $appVariable = new \ReflectionClass('\Symfony\Bridge\Twig\AppVariable');
50 67
        $twigBridgeDirectory = \dirname((string)$appVariable->getFileName());
51
52 67
        $loader = new \Twig\Loader\FilesystemLoader([
53 67
            $twigBridgeDirectory . '/Resources/views/Form',
54
        ]);
55
56 67
        $twig = new \Twig\Environment($loader);
57 67
        $twig->addExtension(new \Symfony\Bridge\Twig\Extension\FormExtension());
58 67
        $twig->addExtension(new \Symfony\Bridge\Twig\Extension\TranslationExtension(
59 67
            new \Symfony\Component\Translation\Translator('en')
60
        ));
61
62 67
        $formEngine = new \Symfony\Bridge\Twig\Form\TwigRendererEngine(['bootstrap_4_layout.html.twig'], $twig);
63 67
        $twig->addRuntimeLoader(new \Twig\RuntimeLoader\FactoryRuntimeLoader([
64 67
            \Symfony\Component\Form\FormRenderer::class => function () use ($formEngine) {
65 65
                return new \Symfony\Component\Form\FormRenderer($formEngine);
66 67
            },
67
        ]));
68
69 67
        return $twig;
70
    }
71
}
72