Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

DependencyInjection/KunstmaanAdminExtension.php (4 issues)

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
8
use Kunstmaan\AdminBundle\Helper\DomainConfiguration;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
13
use Symfony\Component\DependencyInjection\Loader;
14
use Symfony\Component\DependencyInjection\Reference;
15
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
16
17
18
/**
19
 * This is the class that loads and manages your bundle configuration
20
 *
21
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
22
 */
23
class KunstmaanAdminExtension extends Extension implements PrependExtensionInterface
24
{
25
    /**
26
     * Loads a specific configuration.
27
     *
28
     * @param array            $configs   An array of configuration values
29
     * @param ContainerBuilder $container A ContainerBuilder instance
30
     *
31
     * @throws InvalidArgumentException When provided tag is not defined in this extension
32
     */
33
    public function load(array $configs, ContainerBuilder $container)
34
    {
35
        $container->setParameter('version_checker.url', 'https://bundles.kunstmaan.be/version-check');
36
        $container->setParameter('version_checker.timeframe', 60*60*24);
37
        $container->setParameter('version_checker.enabled', true);
38
39
        $configuration = new Configuration();
40
        $config = $this->processConfiguration($configuration, $configs);
41
42
        if (array_key_exists('dashboard_route', $config)) {
43
            $container->setParameter('kunstmaan_admin.dashboard_route', $config['dashboard_route']);
44
        }
45
        if (array_key_exists('admin_password', $config)) {
46
            $container->setParameter('kunstmaan_admin.admin_password', $config['admin_password']);
47
        }
48
        $container->setParameter('kunstmaan_admin.admin_locales', $config['admin_locales']);
49
        $container->setParameter('kunstmaan_admin.default_admin_locale', $config['default_admin_locale']);
50
51
        $container->setParameter('kunstmaan_admin.session_security.ip_check', $config['session_security']['ip_check']);
52
        $container->setParameter('kunstmaan_admin.session_security.user_agent_check', $config['session_security']['user_agent_check']);
53
54
        $container->setParameter('kunstmaan_admin.admin_prefix', $this->normalizeUrlSlice($config['admin_prefix']));
55
56
        $container->setParameter('kunstmaan_admin.admin_exception_excludes', $config['admin_exception_excludes']);
57
58
        $container->setParameter('kunstmaan_admin.google_signin.enabled', $config['google_signin']['enabled']);
59
        $container->setParameter('kunstmaan_admin.google_signin.client_id', $config['google_signin']['client_id']);
60
        $container->setParameter('kunstmaan_admin.google_signin.client_secret', $config['google_signin']['client_secret']);
61
        $container->setParameter('kunstmaan_admin.google_signin.hosted_domains', $config['google_signin']['hosted_domains']);
62
63
        $container->setParameter('kunstmaan_admin.password_restrictions.min_digits' , $config['password_restrictions']['min_digits']);
64
        $container->setParameter('kunstmaan_admin.password_restrictions.min_uppercase' , $config['password_restrictions']['min_uppercase']);
65
        $container->setParameter('kunstmaan_admin.password_restrictions.min_special_characters' , $config['password_restrictions']['min_special_characters']);
66
        $container->setParameter('kunstmaan_admin.password_restrictions.min_length' , $config['password_restrictions']['min_length']);
67
        $container->setParameter('kunstmaan_admin.password_restrictions.max_length' , $config['password_restrictions']['max_length']);
68
        $container->setParameter('kunstmaan_admin.enable_toolbar_helper', $config['enable_toolbar_helper']);
69
        $container->setParameter('kunstmaan_admin.toolbar_firewall_names', !empty($config['provider_keys']) ? $config['provider_keys'] : $config['toolbar_firewall_names']);
70
        $container->setParameter('kunstmaan_admin.admin_firewall_name', $config['admin_firewall_name']);
71
72
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
73
        $loader->load('services.yml');
74
        $loader->load('commands.yml');
75
76
        if (!empty($config['enable_console_exception_listener']) && $config['enable_console_exception_listener']) {
77
            $loader->load('console_listener.yml');
78
        }
79
80
        if (0 !== count($config['menu_items'])) {
81
            $this->addSimpleMenuAdaptor($container, $config['menu_items']);
82
        }
83
    }
84
85
    public function prepend(ContainerBuilder $container)
86
    {
87
        $knpMenuConfig['twig']              = true; // set to false to disable the Twig extension and the TwigRenderer
0 ignored issues
show
Coding Style Comprehensibility introduced by
$knpMenuConfig was never initialized. Although not strictly required by PHP, it is generally a good practice to add $knpMenuConfig = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
88
        $knpMenuConfig['templating']        = false; // if true, enables the helper for PHP templates
89
        $knpMenuConfig['default_renderer']  = 'twig'; // The renderer to use, list is also available by default
90
        $container->prependExtensionConfig('knp_menu', $knpMenuConfig);
91
92
        $fosUserConfig['db_driver']                     = 'orm'; // other valid values are 'mongodb', 'couchdb'
0 ignored issues
show
Coding Style Comprehensibility introduced by
$fosUserConfig was never initialized. Although not strictly required by PHP, it is generally a good practice to add $fosUserConfig = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
93
        $fosUserConfig['firewall_name']                 = 'main';
94
        $fosUserConfig['user_class']                    = 'Kunstmaan\AdminBundle\Entity\User';
95
        $fosUserConfig['group']['group_class']          = 'Kunstmaan\AdminBundle\Entity\Group';
96
        $fosUserConfig['resetting']['token_ttl']        = 86400;
97
        // Use this node only if you don't want the global email address for the resetting email
98
        $fosUserConfig['resetting']['email']['from_email']['address']        = '[email protected]';
99
        $fosUserConfig['resetting']['email']['from_email']['sender_name']    = 'admin';
100
        $fosUserConfig['resetting']['email']['template']    = 'FOSUserBundle:Resetting:email.txt.twig';
101
        $fosUserConfig['resetting']['form']['type']                 = ResettingFormType::class;
102
        $fosUserConfig['resetting']['form']['name']                 = 'fos_user_resetting_form';
103
        $fosUserConfig['resetting']['form']['validation_groups']    = ['ResetPassword'];
104
        $container->prependExtensionConfig('fos_user', $fosUserConfig);
105
106
        $monologConfig['handlers']['main']['type']  = 'rotating_file';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$monologConfig was never initialized. Although not strictly required by PHP, it is generally a good practice to add $monologConfig = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
107
        $monologConfig['handlers']['main']['path']  = sprintf('%s/%s', $container->getParameter('kernel.logs_dir'), $container->getParameter('kernel.environment'));
108
        $monologConfig['handlers']['main']['level'] = 'debug';
109
        $container->prependExtensionConfig('monolog', $monologConfig);
110
111
        $twigConfig['paths'][] = ['value' => dirname(__DIR__).'/Resources/views', 'namespace' => 'FOSUser'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$twigConfig was never initialized. Although not strictly required by PHP, it is generally a good practice to add $twigConfig = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
112
        $container->prependExtensionConfig('twig', $twigConfig);
113
114
        $configs = $container->getExtensionConfig($this->getAlias());
115
        $this->processConfiguration(new Configuration(), $configs);
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121
    public function getNamespace()
122
    {
123
        return 'http://bundles.kunstmaan.be/schema/dic/admin';
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     */
129
    public function getXsdValidationBasePath()
130
    {
131
        return __DIR__.'/../Resources/config/schema';
132
    }
133
134
    private function addSimpleMenuAdaptor(ContainerBuilder $container, array $menuItems)
135
    {
136
        $definition = new Definition('Kunstmaan\AdminBundle\Helper\Menu\SimpleMenuAdaptor', [
137
            new Reference('security.authorization_checker'),
138
            $menuItems
139
        ]);
140
        $definition->addTag('kunstmaan_admin.menu.adaptor');
141
142
        $container->setDefinition('kunstmaan_admin.menu.adaptor.simple', $definition);
143
    }
144
145
    /**
146
     * @param string $urlSlice
147
     *
148
     * @return string
149
     */
150
    protected function normalizeUrlSlice($urlSlice)
151
    {
152
        /* Get rid of exotic characters that would break the url */
153
        $urlSlice = filter_var($urlSlice, FILTER_SANITIZE_URL);
154
155
        /* Remove leading and trailing slashes */
156
        $urlSlice = trim($urlSlice, '/');
157
158
        /* Make sure our $urlSlice is literally used in our regex */
159
        $urlSlice = preg_quote($urlSlice);
160
161
        return $urlSlice;
162
    }
163
}
164