Passed
Push — master ( 0de292...c7f65a )
by Christian
02:22
created

DownloadTypeExtension::getExtendedType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\Form\Extension;
13
14
use Symfony\Component\Form\AbstractTypeExtension;
15
use Symfony\Component\Form\Extension\Core\Type\FileType;
16
use Symfony\Component\Form\FormBuilderInterface;
17
use Symfony\Component\Form\FormInterface;
18
use Symfony\Component\Form\FormView;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
use Symfony\Component\PropertyAccess\PropertyAccess;
21
use Symfony\Component\PropertyAccess\PropertyPath;
22
23
final class DownloadTypeExtension extends AbstractTypeExtension
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function configureOptions(OptionsResolver $resolver): void
29
    {
30
        $resolver
31
            ->setDefaults([
32
                'download_path' => null,
33
                'download_text' => 'link_download',
34
            ])
35
            ->setAllowedTypes('download_path', ['null', 'string'])
36
            ->setAllowedTypes('download_text', ['null', 'string'])
37
        ;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function buildForm(FormBuilderInterface $builder, array $options): void
44
    {
45
        if (null !== $options['download_path']) {
46
            $builder->setAttribute('download_path', $options['download_path']);
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function buildView(FormView $view, FormInterface $form, array $options): void
54
    {
55
        if (null !== $options['download_path'] && null !== $form->getParent()) {
56
            $parentData = $form->getParent()->getData();
57
58
            if (null !== $parentData) {
59
                $propertyAccessor = PropertyAccess::createPropertyAccessor();
60
                $propertyPath     = new PropertyPath($options['download_path']);
61
62
                $downloadPath = $propertyAccessor->getValue($parentData, $propertyPath);
63
            } else {
64
                $downloadPath = null;
65
            }
66
            // set an "download_path" variable that will be available when rendering this field
67
            $view->vars['download_path'] = $downloadPath;
68
        }
69
        $view->vars['download_text'] = $options['download_text'];
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getExtendedType()
76
    {
77
        foreach (static::getExtendedTypes() as $extendedType) {
78
            return $extendedType;
79
        }
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public static function getExtendedTypes(): iterable
86
    {
87
        return [
88
            FileType::class,
89
        ];
90
    }
91
}
92