Completed
Pull Request — master (#2776)
by
unknown
06:22
created

KunstmaanMediaExtension::prepend()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 20
cts 20
cp 1
rs 8.9599
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kunstmaan\MediaBundle\DependencyInjection;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
9
use Symfony\Component\DependencyInjection\Loader;
10
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
11
use Symfony\Component\Mime\MimeTypes;
12
use Symfony\Component\Yaml\Yaml;
13
14
/**
15
 * This is the class that loads and manages your bundle configuration
16
 *
17
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
18
 */
19
class KunstmaanMediaExtension extends Extension implements PrependExtensionInterface
20
{
21
    /**
22
     * Loads configuration
23
     *
24
     * @param array            $configs   Configuration
25
     * @param ContainerBuilder $container Container
26
     */
27 4
    public function load(array $configs, ContainerBuilder $container)
28
    {
29 4
        $configuration = new Configuration();
30 4
        $config = $this->processConfiguration($configuration, $configs);
31
32 4
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
33
34 4
        $container->setParameter(
35 4
            'twig.form.resources',
36 4
            array_merge(
37 4
                $container->hasParameter('twig.form.resources') ? $container->getParameter('twig.form.resources') : [],
38 4
                ['@KunstmaanMedia/Form/formWidgets.html.twig']
39
            )
40
        );
41 4
        $container->setParameter('kunstmaan_media.soundcloud_api_key', $config['soundcloud_api_key']);
42 4
        $container->setParameter('kunstmaan_media.remote_video', $config['remote_video']);
43 4
        $container->setParameter('kunstmaan_media.enable_pdf_preview', $config['enable_pdf_preview']);
44 4
        $container->setParameter('kunstmaan_media.blacklisted_extensions', $config['blacklisted_extensions']);
45 4
        $container->setParameter('kunstmaan_media.web_root', $config['web_root']);
46 4
        $container->setParameter('kunstmaan_media.full_media_path', $config['web_root'] . '%kunstmaan_media.media_path%');
47
48 4
        $loader->load('services.yml');
49 4
        $loader->load('handlers.yml');
50
51 4
        if ($config['enable_pdf_preview'] === true) {
52 1
            $loader->load('pdf_preview.yml');
53
        }
54
55 4
        $container->setParameter('liip_imagine.filter.loader.background.class', 'Kunstmaan\MediaBundle\Helper\Imagine\BackgroundFilterLoader');
56
57 4
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
58 4
        $loader->load('imagine.xml');
59
60 4
        $container->setAlias('liip_imagine.controller', 'Kunstmaan\MediaBundle\Helper\Imagine\ImagineController')->setPublic(true);
61 4
        $container->setAlias('Liip\ImagineBundle\Controller\ImagineController', 'Kunstmaan\MediaBundle\Helper\Imagine\ImagineController')->setPublic(true);
62 4
        $container->setAlias('liip_imagine.cache.resolver.prototype.web_path', 'Kunstmaan\MediaBundle\Helper\Imagine\WebPathResolver');
63 4
        $container->setAlias('liip_imagine.cache.manager', 'Kunstmaan\MediaBundle\Helper\Imagine\CacheManager')->setPublic(true);
64 4
        $container->setAlias('liip_imagine.filter.loader.background', 'kunstmaan_media.imagine.filter.loader.background')->setPublic(true);
65
66 4
        $this->addAvairyApiKeyParameter($container, $config);
67
68 4
        if (!$container->hasDefinition('mime_types')) {
69 4
            $mimeTypes = new Definition(MimeTypes::class);
70 4
            $mimeTypes->setPublic(true);
71 4
            $container->setDefinition('mime_types', $mimeTypes);
72
        }
73 4
    }
74
75 4
    public function prepend(ContainerBuilder $container)
76
    {
77 4
        if (!$container->hasParameter('kunstmaan_media.upload_dir')) {
78 4
            $container->setParameter('kunstmaan_media.upload_dir', '/uploads/media/');
79
        }
80
81 4
        $twigConfig = [];
82 4
        $twigConfig['globals']['upload_dir'] = $container->getParameter('kunstmaan_media.upload_dir');
83 4
        $twigConfig['globals']['mediabundleisactive'] = true;
84 4
        $twigConfig['globals']['mediamanager'] = '@kunstmaan_media.media_manager';
85 4
        $container->prependExtensionConfig('twig', $twigConfig);
86
87 4
        $liipConfig = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/imagine_filters.yml'));
88 4
        $container->prependExtensionConfig('liip_imagine', $liipConfig['liip_imagine']);
89
90 4
        $defaultLocale = $container->hasParameter('kunstmaan_admin.default_locale') ? $container->getParameter('kunstmaan_admin.default_locale') : 'en';
91
        $stofDoctrineExtensionsConfig = [
92 4
            'default_locale' => $defaultLocale,
93
            'translation_fallback' => true,
94
            'orm' => [
95
                'default' => [
96
                    'translatable' => true,
97
                ],
98
            ],
99
        ];
100
101 4
        $container->prependExtensionConfig('stof_doctrine_extensions', $stofDoctrineExtensionsConfig);
102
103
        $doctrineGedmoEntityConfig = [
104
            'orm' => [
105
                'mappings' => [
106
                    'gedmo_translatable' => [
107 4
                        'type' => 'annotation',
108
                        'prefix' => 'Gedmo\Translatable\Entity',
109
                        'dir' => '%kernel.project_dir%/vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity',
110
                        'alias' => 'GedmoTranslatable',
111
                        'is_bundle' => false,
112
                    ],
113
                ],
114
            ],
115
        ];
116
117 4
        $container->prependExtensionConfig('doctrine', $doctrineGedmoEntityConfig);
118
119
        $frameworkExtensionConfig = [
120
            'assets' => [
121
                'packages' => [
122 4
                    'media' => null,
123
                ],
124
            ],
125
        ];
126 4
        $container->prependExtensionConfig('framework', $frameworkExtensionConfig);
127
128 4
        $configs = $container->getExtensionConfig($this->getAlias());
129 4
        $this->processConfiguration(new Configuration(), $configs);
130 4
    }
131
132
    /**
133
     * @return string
134
     */
135 4
    public function getAlias()
136
    {
137 4
        return 'kunstmaan_media';
138
    }
139
140 4 View Code Duplication
    private function addAvairyApiKeyParameter(ContainerBuilder $container, array $config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        // NEXT_MAJOR: Remove parameter, config option and all aviary related files (twig, js, etc)
143 4
        $aviaryApiKey = $container->hasParameter('aviary_api_key') ? $container->getParameter('aviary_api_key') : null;
144 4
        if (null === $config['aviary_api_key'] && null !== $aviaryApiKey) {
145 1
            @trigger_error('Not providing a value for the "kunstmaan_media.aviary_api_key" config while setting the "aviary_api_key" parameter is deprecated since KunstmaanMediaBundle 5.2, this config value will replace the "aviary_api_key" parameter in KunstmaanMediaBundle 6.0.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
146
        }
147
148 4
        if (null !== $config['aviary_api_key']) {
149 1
            $aviaryApiKey = $config['aviary_api_key'];
150
        }
151
152 4
        $container->setParameter('kunstmaan_media.aviary_api_key', $aviaryApiKey);
153 4
    }
154
}
155