MediaAutocompleteType::configureOptions()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4.0406

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 19
cts 22
cp 0.8636
rs 9.344
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 4.0406
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Form\Type;
4
5
use MediaMonks\SonataMediaBundle\Admin\MediaAdmin;
6
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType as BaseModelAutocompleteType;
7
use Symfony\Component\OptionsResolver\Options;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
use Twig\Environment;
10
11
class MediaAutocompleteType extends BaseModelAutocompleteType
12
{
13
    /**
14
     * @var MediaAdmin
15
     */
16
    private $mediaAdmin;
17
18
    /**
19
     * @var Environment
20
     */
21
    private $twig;
22
23
    /**
24
     * @param MediaAdmin $mediaAdmin
25
     * @param Environment $twig
26
     */
27 1
    public function __construct(MediaAdmin $mediaAdmin, Environment $twig)
28
    {
29 1
        $this->mediaAdmin = $mediaAdmin;
30 1
        $this->twig = $twig;
31 1
    }
32
33
    /**
34
     * @param OptionsResolver $resolver
35
     */
36 1
    public function configureOptions(OptionsResolver $resolver)
37
    {
38 1
        parent::configureOptions($resolver);
39
40
        // Tell sonata we are passing a previously escaped string
41 1
        if ($resolver->hasDefault('safe_label')) {
42 1
            $resolver->setDefault('safe_label', true);
43
        }
44
45 1
        $resolver->setDefaults(
46
            [
47 1
                'type' => null,
48
                'provider' => null,
49 1
                'property' => 'title',
50 1
                'to_string_callback' => function ($media, $property) {
0 ignored issues
show
Unused Code introduced by
The parameter $property is not used and could be removed.

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

Loading history...
51
                    return $this->twig->render(
52
                        '@MediaMonksSonataMedia/CRUD/autocomplete.html.twig',
53
                        [
54
                            'media' => $media,
55
                        ]
56
                    );
57 1
                },
58 1
                'route' => function(Options $options) {
59 1
                    $parameters = [];
60 1
                    if (isset($options['type'])) {
61 1
                        $parameters['type'] = $options['type'];
62
                    }
63 1
                    if (isset($options['provider'])) {
64 1
                        $parameters['provider'] = $options['provider'];
65
                    }
66 1
                    return ['name' => 'mediamonks_media_autocomplete', 'parameters' => $parameters];
67 1
                },
68 1
                'model_manager' => $this->mediaAdmin->getModelManager()
69
            ]
70
        );
71 1
    }
72
}
73