LocaleListener   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getSubscribedEvents() 0 3 1
A onKernelRequest() 0 7 2
A registerCapsule() 0 20 1
A registerLDAP() 0 6 1
1
<?php
2
3
/*
4
 * @author  Xavier Chopin <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace App\Event;
11
12
use Jenssegers\Mongodb\Connection;
13
use Psr\Container\ContainerInterface;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
16
use Symfony\Component\HttpKernel\HttpKernel;
17
use Symfony\Component\HttpKernel\KernelEvents;
18
use Illuminate\Database\Capsule\Manager;
19
20
21
class LocaleListener
22
{
23
    private $container;
24
25
    public function __construct(ContainerInterface $container)
26
    {
27
        $this->container = $container;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function onKernelRequest(GetResponseEvent $event)
34
    {
35
        if (HttpKernel::MASTER_REQUEST != $event->getRequestType())
36
            return;
37
38
        $this->registerLDAP();
39
        $this->registerCapsule();
40
41
    }
42
43
    /**
44
     * Registers a LDAP Instance into the container.
45
     */
46
    private function registerLDAP()
47
    {
48
        $ldapInstance = ldap_connect(env('LDAP_HOST'), env('LDAP_PORT'));
0 ignored issues
show
Bug introduced by
It seems like env('LDAP_PORT') can also be of type string and boolean; however, parameter $port of ldap_connect() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        $ldapInstance = ldap_connect(env('LDAP_HOST'), /** @scrutinizer ignore-type */ env('LDAP_PORT'));
Loading history...
49
        ldap_set_option($ldapInstance, LDAP_OPT_PROTOCOL_VERSION, 3);
50
        ldap_set_option($ldapInstance, LDAP_OPT_REFERRALS, 0);
51
        $this->container->set('ldap', /** @scrutinizer ignore-type */ $ldapInstance);
0 ignored issues
show
Bug introduced by
The method set() does not exist on Psr\Container\ContainerInterface. It seems like you code against a sub-type of Psr\Container\ContainerInterface such as Symfony\Component\Depend...tion\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        $this->container->/** @scrutinizer ignore-call */ 
52
                          set('ldap', /** @scrutinizer ignore-type */ $ldapInstance);
Loading history...
52
    }
53
54
    /**
55
     * Sets globally and registers the Eloquent ORM's Capsule Manager into the container.
56
     */
57
    private function registerCapsule()
58
    {
59
        $capsule = new Manager();
60
        $capsule->getDatabaseManager()->extend('mongodb', function($config) {
61
            return new Connection($config);
62
        });
63
        $capsule->addConnection([
64
            'driver' => 'mongodb',
65
            'host' => env('DB_HOST'),
66
            'port' => intval(env('DB_PORT')),
67
            'database' => env('DB_NAME'),
68
            'username' => env('DB_USERNAME'),
69
            'password' => env('DB_PASSWORD'),
70
            'options' => [
71
                'database' => env('DB_NAME'),
72
            ]
73
        ]);
74
        $capsule->setAsGlobal();
75
        $capsule->bootEloquent();
76
        $this->container->set('capsule', $capsule);
77
    }
78
79
    public static function getSubscribedEvents()
80
    {
81
        return [ KernelEvents::REQUEST => [['onKernelRequest', 200]] ];
82
    }
83
}