EntityManagerFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 36
ccs 18
cts 18
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 18 2
A registerTypes() 0 7 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Common\Doctrine;
6
7
use Doctrine\Common\Cache\ArrayCache;
8
use Doctrine\Common\Cache\Cache;
9
use Doctrine\DBAL\DBALException;
10
use Doctrine\DBAL\Types\Type;
11
use Doctrine\ORM\EntityManager;
12
use Doctrine\ORM\ORMException;
13
use Doctrine\ORM\Tools\Setup;
14
use Psr\Container\ContainerInterface;
15
use Shlinkio\Shlink\Common\Doctrine\Mapping\EnhancedPHPDriver;
16
17
class EntityManagerFactory
18
{
19
    /**
20
     * @throws ORMException
21
     * @throws DBALException
22
     */
23 5
    public function __invoke(ContainerInterface $container): EntityManager
24
    {
25 5
        $globalConfig = $container->get('config');
26 5
        $isDevMode = (bool) ($globalConfig['debug'] ?? false);
27 5
        $cache = $container->has(Cache::class) ? $container->get(Cache::class) : new ArrayCache();
28 5
        $emConfig = $globalConfig['entity_manager'] ?? [];
29 5
        $connectionConfig = $emConfig['connection'] ?? [];
30 5
        $ormConfig = $emConfig['orm'] ?? [];
31 5
        $funcStyle = $ormConfig['load_mappings_using_functional_style'] ?? false;
32
33 5
        $this->registerTypes($ormConfig);
34
35 5
        $config = Setup::createConfiguration($isDevMode, $ormConfig['proxies_dir'] ?? null, $cache);
36 5
        $config->setMetadataDriverImpl(
37 5
            new EnhancedPHPDriver($ormConfig['entities_mappings'] ?? [], $emConfig, $funcStyle),
0 ignored issues
show
Bug introduced by
new Shlinkio\Shlink\Comm... $emConfig, $funcStyle) of type Shlinkio\Shlink\Common\D...pping\EnhancedPHPDriver is incompatible with the type Doctrine\Common\Persiste...ng\Driver\MappingDriver expected by parameter $driverImpl of Doctrine\ORM\Configurati...setMetadataDriverImpl(). ( Ignorable by Annotation )

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

37
            /** @scrutinizer ignore-type */ new EnhancedPHPDriver($ormConfig['entities_mappings'] ?? [], $emConfig, $funcStyle),
Loading history...
38
        );
39
40 5
        return EntityManager::create($connectionConfig, $config);
41
    }
42
43
    /**
44
     * @throws DBALException
45
     */
46 5
    private function registerTypes(array $ormConfig): void
47
    {
48 5
        $types = $ormConfig['types'] ?? [];
49
50 5
        foreach ($types as $name => $className) {
51 5
            if (! Type::hasType($name)) {
52 5
                Type::addType($name, $className);
53
            }
54
        }
55
    }
56
}
57