Completed
Push — master ( aa77c9...15a70d )
by Alejandro
27s
created

EntityManagerFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 27
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 19 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Common\Factory;
5
6
use Doctrine\Common\Cache\ArrayCache;
7
use Doctrine\Common\Cache\Cache;
8
use Doctrine\DBAL\DBALException;
9
use Doctrine\DBAL\Types\Type;
10
use Doctrine\ORM\EntityManager;
11
use Doctrine\ORM\ORMException;
12
use Doctrine\ORM\Tools\Setup;
13
use Interop\Container\ContainerInterface;
14
use Shlinkio\Shlink\Common\Type\ChronosDateTimeType;
15
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
16
use Zend\ServiceManager\Exception\ServiceNotFoundException;
17
use Zend\ServiceManager\Factory\FactoryInterface;
18
19
class EntityManagerFactory implements FactoryInterface
20
{
21
    /**
22
     * @throws ServiceNotFoundException if unable to resolve the service.
23
     * @throws ServiceNotCreatedException if an exception is raised when creating a service.
24
     * @throws ORMException
25
     * @throws DBALException
26
     */
27 1
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
28
    {
29 1
        $globalConfig = $container->get('config');
30 1
        $isDevMode = isset($globalConfig['debug']) ? ((bool) $globalConfig['debug']) : false;
31 1
        $cache = $container->has(Cache::class) ? $container->get(Cache::class) : new ArrayCache();
32 1
        $emConfig = $globalConfig['entity_manager'] ?? [];
33 1
        $connectionConfig = $emConfig['connection'] ?? [];
34 1
        $ormConfig = $emConfig['orm'] ?? [];
35
36 1
        if (! Type::hasType(ChronosDateTimeType::CHRONOS_DATETIME)) {
37
            Type::addType(ChronosDateTimeType::CHRONOS_DATETIME, ChronosDateTimeType::class);
38
        }
39
40 1
        return EntityManager::create($connectionConfig, Setup::createAnnotationMetadataConfiguration(
41 1
            $ormConfig['entities_paths'] ?? [],
42 1
            $isDevMode,
43 1
            $ormConfig['proxies_dir'] ?? null,
44 1
            $cache,
45 1
            false
46
        ));
47
    }
48
}
49