ImageTypeExtension   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 6
dl 0
loc 39
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildView() 0 17 3
A getExtendedType() 0 4 1
A configureOptions() 0 4 1
1
<?php
2
3
namespace Black\Common\Application\Form\Extension;
4
5
use Symfony\Component\Form\AbstractTypeExtension;
6
use Symfony\Component\Form\FormInterface;
7
use Symfony\Component\Form\FormView;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
use Symfony\Component\PropertyAccess\PropertyAccess;
10
11
/**
12
 * Class ImageTypeExtension
13
 *
14
 * This extension add image option for create a preview near the upload form type
15
 */
16
class ImageTypeExtension extends AbstractTypeExtension
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function buildView(FormView $view, FormInterface $form, array $options)
22
    {
23
        $view->vars['image_url'] = null;
24
25
        if (array_key_exists('image_path', $options)) {
26
            $parentData = $form->getParent()->getData();
27
28
            if (null !== $parentData) {
29
                $accessor = PropertyAccess::createPropertyAccessor();
30
                $imageUrl = $accessor->getValue($parentData, $options['image_path']);
31
            } else {
32
                $imageUrl = null;
33
            }
34
35
            $view->vars['image_url'] = $imageUrl;
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getExtendedType()
43
    {
44
        return 'file';
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function configureOptions(OptionsResolver $resolver)
51
    {
52
        $resolver->setDefined(['image_path']);
53
    }
54
}
55