FreshDoctrineEnumBundle::boot()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.3222
c 0
b 0
f 0
cc 5
nc 4
nop 0
1
<?php
2
/*
3
 * This file is part of the FreshDoctrineEnumBundle.
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\DoctrineEnumBundle;
14
15
use Doctrine\Persistence\ManagerRegistry;
16
use Fresh\DoctrineEnumBundle\Exception\InvalidArgumentException;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
use Symfony\Component\HttpKernel\Bundle\Bundle;
19
20
/**
21
 * FreshDoctrineEnumBundle.
22
 *
23
 * @author Artem Henvald <[email protected]>
24
 */
25
class FreshDoctrineEnumBundle extends Bundle
26
{
27
    /**
28
     * {@inheritdoc}
29
     *
30
     * @throws InvalidArgumentException
31
     * @throws \Doctrine\DBAL\DBALException
32
     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
33
     * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
34
     */
35
    public function boot(): void
36
    {
37
        parent::boot();
38
39
        $doctrine = $this->container->get('doctrine', ContainerInterface::NULL_ON_INVALID_REFERENCE);
40
        if (!$doctrine instanceof ManagerRegistry) {
41
            throw new InvalidArgumentException('Service "doctrine" is missed in container');
42
        }
43
44
        /** @var \Doctrine\DBAL\Connection $connection */
45
        foreach ($doctrine->getConnections() as $connection) {
46
            /** @var \Doctrine\DBAL\Platforms\AbstractPlatform $databasePlatform */
47
            $databasePlatform = $connection->getDatabasePlatform();
48
49
            if (!$databasePlatform->hasDoctrineTypeMappingFor('enum') || 'string' !== $databasePlatform->getDoctrineTypeMapping('enum')) {
50
                $databasePlatform->registerDoctrineTypeMapping('enum', 'string');
51
            }
52
        }
53
    }
54
}
55