KunstmaanMediaExtension   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 10.24 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 9
dl 13
loc 127
ccs 61
cts 61
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlias() 0 4 1
A prepend() 0 47 3
A load() 0 47 4
A addAvairyApiKeyParameter() 13 14 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 4
        $configs = $container->getExtensionConfig($this->getAlias());
120 4
        $this->processConfiguration(new Configuration(), $configs);
121 4
    }
122
123
    /**
124
     * @return string
125
     */
126 4
    public function getAlias()
127
    {
128 4
        return 'kunstmaan_media';
129
    }
130
131 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...
132
    {
133
        // NEXT_MAJOR: Remove parameter, config option and all aviary related files (twig, js, etc)
134 4
        $aviaryApiKey = $container->hasParameter('aviary_api_key') ? $container->getParameter('aviary_api_key') : null;
135 4
        if (null === $config['aviary_api_key'] && null !== $aviaryApiKey) {
136 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...
137
        }
138
139 4
        if (null !== $config['aviary_api_key']) {
140 1
            $aviaryApiKey = $config['aviary_api_key'];
141
        }
142
143 4
        $container->setParameter('kunstmaan_media.aviary_api_key', $aviaryApiKey);
144 4
    }
145
}
146