Completed
Push — master ( 4c839e...4fda94 )
by Ruud
12:51 queued 10s
created

DependencyInjection/KunstmaanAdminExtension.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\DependencyInjection;
4
5
use FOS\UserBundle\Form\Type\ResettingFormType;
6
use InvalidArgumentException;
7
use Kunstmaan\AdminBundle\Helper\Menu\MenuAdaptorInterface;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Definition;
11
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
12
use Symfony\Component\DependencyInjection\Loader;
13
use Symfony\Component\DependencyInjection\Reference;
14
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
15
16
/**
17
 * This is the class that loads and manages your bundle configuration
18
 *
19
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
20
 */
21
class KunstmaanAdminExtension extends Extension implements PrependExtensionInterface
22
{
23
    /**
24
     * Loads a specific configuration.
25
     *
26
     * @param array            $configs   An array of configuration values
27
     * @param ContainerBuilder $container A ContainerBuilder instance
28
     *
29
     * @throws InvalidArgumentException When provided tag is not defined in this extension
30
     */
31 14
    public function load(array $configs, ContainerBuilder $container)
32
    {
33 14
        $container->setParameter('version_checker.url', 'https://bundles.kunstmaan.be/version-check');
34 14
        $container->setParameter('version_checker.timeframe', 60 * 60 * 24);
35 14
        $container->setParameter('version_checker.enabled', true);
36
37 14
        $configuration = new Configuration();
38 14
        $config = $this->processConfiguration($configuration, $configs);
39
40 14
        if (array_key_exists('dashboard_route', $config)) {
41 1
            $container->setParameter('kunstmaan_admin.dashboard_route', $config['dashboard_route']);
42
        }
43 14
        if (array_key_exists('admin_password', $config)) {
44 1
            $container->setParameter('kunstmaan_admin.admin_password', $config['admin_password']);
45
        }
46 14
        $container->setParameter('kunstmaan_admin.admin_locales', $config['admin_locales']);
47 14
        $container->setParameter('kunstmaan_admin.default_admin_locale', $config['default_admin_locale']);
48
49 14
        $container->setParameter('kunstmaan_admin.session_security.ip_check', $config['session_security']['ip_check']);
50 14
        $container->setParameter('kunstmaan_admin.session_security.user_agent_check', $config['session_security']['user_agent_check']);
51
52 14
        $container->setParameter('kunstmaan_admin.admin_prefix', $this->normalizeUrlSlice($config['admin_prefix']));
53
54 14
        $container->setParameter('kunstmaan_admin.admin_exception_excludes', $config['admin_exception_excludes']);
55
56 14
        $container->setParameter('kunstmaan_admin.google_signin.enabled', $config['google_signin']['enabled']);
57 14
        $container->setParameter('kunstmaan_admin.google_signin.client_id', $config['google_signin']['client_id']);
58 14
        $container->setParameter('kunstmaan_admin.google_signin.client_secret', $config['google_signin']['client_secret']);
59 14
        $container->setParameter('kunstmaan_admin.google_signin.hosted_domains', $config['google_signin']['hosted_domains']);
60
61 14
        $container->setParameter('kunstmaan_admin.password_restrictions.min_digits', $config['password_restrictions']['min_digits']);
62 14
        $container->setParameter('kunstmaan_admin.password_restrictions.min_uppercase', $config['password_restrictions']['min_uppercase']);
63 14
        $container->setParameter('kunstmaan_admin.password_restrictions.min_special_characters', $config['password_restrictions']['min_special_characters']);
64 14
        $container->setParameter('kunstmaan_admin.password_restrictions.min_length', $config['password_restrictions']['min_length']);
65 14
        $container->setParameter('kunstmaan_admin.password_restrictions.max_length', $config['password_restrictions']['max_length']);
66 14
        $container->setParameter('kunstmaan_admin.enable_toolbar_helper', $config['enable_toolbar_helper']);
67 14
        $container->setParameter('kunstmaan_admin.toolbar_firewall_names', !empty($config['provider_keys']) ? $config['provider_keys'] : $config['toolbar_firewall_names']);
68 14
        $container->setParameter('kunstmaan_admin.admin_firewall_name', $config['admin_firewall_name']);
69
70 14
        $container->registerForAutoconfiguration(MenuAdaptorInterface::class)
71 14
            ->addTag('kunstmaan_admin.menu.adaptor');
72
73 14
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
74 14
        $loader->load('services.yml');
75 14
        $loader->load('commands.yml');
76
77 14
        if (!empty($config['enable_console_exception_listener']) && $config['enable_console_exception_listener']) {
78 14
            $loader->load('console_listener.yml');
79
        }
80
81 14
        if (0 !== count($config['menu_items'])) {
82 1
            $this->addSimpleMenuAdaptor($container, $config['menu_items']);
83
        }
84
85 14
        $this->addWebsiteTitleParameter($container, $config);
86 14
        $this->addMultiLanguageParameter($container, $config);
87 14
        $this->addRequiredLocalesParameter($container, $config);
88 14
        $this->addDefaultLocaleParameter($container, $config);
89 14
    }
90
91 14
    public function prepend(ContainerBuilder $container)
92
    {
93 14
        $knpMenuConfig['twig'] = true; // set to false to disable the Twig extension and the TwigRenderer
94 14
        $knpMenuConfig['templating'] = false; // if true, enables the helper for PHP templates
95 14
        $knpMenuConfig['default_renderer'] = 'twig'; // The renderer to use, list is also available by default
96 14
        $container->prependExtensionConfig('knp_menu', $knpMenuConfig);
97
98 14
        $fosUserOriginalConfig = $container->getExtensionConfig('fos_user');
99 14
        if (!isset($fosUserOriginalConfig[0]['db_driver'])) {
100 14
            $fosUserConfig['db_driver'] = 'orm'; // other valid values are 'mongodb', 'couchdb'
101 14
        }
102 14
        $fosUserConfig['from_email']['address'] = '[email protected]';
0 ignored issues
show
The variable $fosUserConfig does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
103 14
        $fosUserConfig['from_email']['sender_name'] = 'KunstmaanCMS';
104 14
        $fosUserConfig['firewall_name'] = 'main';
105
        $fosUserConfig['user_class'] = 'Kunstmaan\AdminBundle\Entity\User';
106 14
        $fosUserConfig['group']['group_class'] = 'Kunstmaan\AdminBundle\Entity\Group';
107 14
        $fosUserConfig['resetting']['token_ttl'] = 86400;
108 14
        // Use this node only if you don't want the global email address for the resetting email
109 14
        $fosUserConfig['resetting']['email']['from_email']['address'] = '[email protected]';
110 14
        $fosUserConfig['resetting']['email']['from_email']['sender_name'] = 'KunstmaanCMS';
111 14
        $fosUserConfig['resetting']['email']['template'] = 'FOSUserBundle:Resetting:email.txt.twig';
112 14
        $fosUserConfig['resetting']['form']['type'] = ResettingFormType::class;
113
        $fosUserConfig['resetting']['form']['name'] = 'fos_user_resetting_form';
114 14
        $fosUserConfig['resetting']['form']['validation_groups'] = ['ResetPassword'];
115 14
        $container->prependExtensionConfig('fos_user', $fosUserConfig);
116 14
117 14
        $monologConfig['handlers']['main']['type'] = 'rotating_file';
118
        $monologConfig['handlers']['main']['path'] = sprintf('%s/%s', $container->getParameter('kernel.logs_dir'), $container->getParameter('kernel.environment'));
119 14
        $monologConfig['handlers']['main']['level'] = 'debug';
120 14
        $container->prependExtensionConfig('monolog', $monologConfig);
121
122 14
        $twigConfig['paths'][] = ['value' => dirname(__DIR__).'/Resources/views', 'namespace' => 'FOSUser'];
123 14
        $container->prependExtensionConfig('twig', $twigConfig);
124
125 14
        $frameworkConfig['templating']['engines'] = ['twig'];
126 14
        $container->prependExtensionConfig('framework', $frameworkConfig);
127 14
128
        $configs = $container->getExtensionConfig($this->getAlias());
129
        $this->processConfiguration(new Configuration(), $configs);
130
    }
131
132 15
    /**
133
     * {@inheritdoc}
134 15
     */
135
    public function getNamespace()
136
    {
137
        return 'http://bundles.kunstmaan.be/schema/dic/admin';
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getXsdValidationBasePath()
144
    {
145 1
        return __DIR__.'/../Resources/config/schema';
146
    }
147 1
148 1
    private function addSimpleMenuAdaptor(ContainerBuilder $container, array $menuItems)
149 1
    {
150
        $definition = new Definition('Kunstmaan\AdminBundle\Helper\Menu\SimpleMenuAdaptor', [
151 1
            new Reference('security.authorization_checker'),
152
            $menuItems,
153 1
        ]);
154 1
        $definition->addTag('kunstmaan_admin.menu.adaptor');
155
156
        $container->setDefinition('kunstmaan_admin.menu.adaptor.simple', $definition);
157
    }
158
159
    /**
160
     * @param string $urlSlice
161 14
     *
162
     * @return string
163
     */
164 14
    protected function normalizeUrlSlice($urlSlice)
165
    {
166
        /* Get rid of exotic characters that would break the url */
167 14
        $urlSlice = filter_var($urlSlice, FILTER_SANITIZE_URL);
168
169
        /* Remove leading and trailing slashes */
170 14
        $urlSlice = trim($urlSlice, '/');
171
172 14
        /* Make sure our $urlSlice is literally used in our regex */
173
        $urlSlice = preg_quote($urlSlice);
174
175 14
        return $urlSlice;
176
    }
177 14
178 14 View Code Duplication
    private function addWebsiteTitleParameter(ContainerBuilder $container, array $config)
179 13
    {
180
        $websiteTitle = $config['website_title'];
181 13
        if (null === $config['website_title']) {
182
            @trigger_error('Not providing a value for the "kunstmaan_admin.website_title" config is deprecated since KunstmaanAdminBundle 5.2, this config value will be required in KunstmaanAdminBundle 6.0.', E_USER_DEPRECATED);
183
184 14
            $websiteTitle = $container->hasParameter('websitetitle') ? $container->getParameter('websitetitle') : '';
185 14
        }
186
187 14
        $container->setParameter('kunstmaan_admin.website_title', $websiteTitle);
188
    }
189 14
190 14 View Code Duplication
    private function addMultiLanguageParameter(ContainerBuilder $container, array $config)
191 12
    {
192
        $multilanguage = $config['multi_language'];
193 12
        if (null === $multilanguage) {
194
            @trigger_error('Not providing a value for the "kunstmaan_admin.multi_language" config is deprecated since KunstmaanAdminBundle 5.2, this config value will be required in KunstmaanAdminBundle 6.0.', E_USER_DEPRECATED);
195
196 14
            $multilanguage = $container->hasParameter('multilanguage') ? $container->getParameter('multilanguage') : '';
197 14
        }
198
199 14
        $container->setParameter('kunstmaan_admin.multi_language', $multilanguage);
200
    }
201 14
202 14 View Code Duplication
    private function addRequiredLocalesParameter(ContainerBuilder $container, array $config)
203 13
    {
204
        $requiredLocales = $config['required_locales'];
205 13
        if (null === $config['required_locales']) {
206
            @trigger_error('Not providing a value for the "kunstmaan_admin.required_locales" config is deprecated since KunstmaanAdminBundle 5.2, this config value will be required in KunstmaanAdminBundle 6.0.', E_USER_DEPRECATED);
207
208 14
            $requiredLocales = $container->hasParameter('requiredlocales') ? $container->getParameter('requiredlocales') : '';
209 14
        }
210 14
211
        $container->setParameter('kunstmaan_admin.required_locales', $requiredLocales);
212 14
        $container->setParameter('requiredlocales', $requiredLocales); //Keep old parameter for to keep BC with routing config
213
    }
214 14
215 14 View Code Duplication
    private function addDefaultLocaleParameter(ContainerBuilder $container, array $config)
216 13
    {
217
        $defaultLocale = $config['default_locale'];
218 13
        if (null === $config['default_locale']) {
219
            @trigger_error('Not providing a value for the "kunstmaan_admin.default_locale" config is deprecated since KunstmaanAdminBundle 5.2, this config value will be required in KunstmaanAdminBundle 6.0.', E_USER_DEPRECATED);
220
221 14
            $defaultLocale = $container->hasParameter('defaultlocale') ? $container->getParameter('defaultlocale') : '';
222 14
        }
223
224
        $container->setParameter('kunstmaan_admin.default_locale', $defaultLocale);
225
    }
226
}
227