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

EntityManagerFactory::__invoke()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0047

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 8
nop 3
dl 0
loc 19
ccs 14
cts 15
cp 0.9333
crap 4.0047
rs 9.7998
c 0
b 0
f 0
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