ConfigSubscriber::onConfigSave()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 2
nop 1
1
<?php
2
/**
3
 * @package     Mautic
4
 * @copyright   2019 Monogramm. All rights reserved
5
 * @author      Monogramm
6
 * @link        https://www.monogramm.io
7
 * @license     GNU/AGPLv3 http://www.gnu.org/licenses/agpl.html
8
 */
9
10
namespace MauticPlugin\MauticLdapAuthBundle\EventListener;
11
12
use Mautic\ConfigBundle\ConfigEvents;
13
use Mautic\ConfigBundle\Event\ConfigBuilderEvent;
14
use Mautic\ConfigBundle\Event\ConfigEvent;
15
use Mautic\CoreBundle\EventListener\CommonSubscriber;
16
17
/**
18
 * Class ConfigSubscriber.
19
 */
20
class ConfigSubscriber extends CommonSubscriber
21
{
22
    /**
23
     * @return array
24
     */
25
    public static function getSubscribedEvents()
26
    {
27
        return [
28
            ConfigEvents::CONFIG_ON_GENERATE => ['onConfigGenerate', 0],
29
            ConfigEvents::CONFIG_PRE_SAVE    => ['onConfigSave', 0],
30
        ];
31
    }
32
33
    /**
34
     * @param ConfigBuilderEvent $event
35
     */
36
    public function onConfigGenerate(ConfigBuilderEvent $event)
37
    {
38
        $event->addForm(
39
            [
40
                'bundle'     => 'MauticLdapAuthBundle',
41
                'formAlias'  => 'ldapconfig',
42
                'formTheme'  => 'MauticLdapAuthBundle:FormTheme\Config',
43
                'parameters' => $event->getParametersFromConfig('MauticLdapAuthBundle'),
44
            ]
45
        );
46
    }
47
48
    /**
49
     * @param ConfigEvent $event
50
     */
51
    public function onConfigSave(ConfigEvent $event)
52
    {
53
        $data = $event->getConfig('ldapconfig');
54
55
        // Manipulate the values
56
        if (!empty($data['ldap_auth_host']) && substr($data['ldap_auth_host'], 0, 8) === 'ldaps://') {
57
            $data['ldap_auth_ssl'] = true;
58
        }
59
60
        $event->setConfig($data, 'ldapconfig');
61
    }
62
}
63