Completed
Push — master ( 3562af...a77174 )
by Alejandro
20s queued 10s
created

EntityManagerFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 25
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 17 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\Common\Persistence\Mapping\Driver\PHPDriver;
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 Interop\Container\ContainerInterface;
15
use Shlinkio\Shlink\Common\Type\ChronosDateTimeType;
16
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
17
use Zend\ServiceManager\Exception\ServiceNotFoundException;
18
use Zend\ServiceManager\Factory\FactoryInterface;
19
20
class EntityManagerFactory implements FactoryInterface
21
{
22
    /**
23
     * @throws ServiceNotFoundException if unable to resolve the service.
24
     * @throws ServiceNotCreatedException if an exception is raised when creating a service.
25
     * @throws ORMException
26
     * @throws DBALException
27
     */
28 1
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
29
    {
30 1
        $globalConfig = $container->get('config');
31 1
        $isDevMode = isset($globalConfig['debug']) ? ((bool) $globalConfig['debug']) : false;
32 1
        $cache = $container->has(Cache::class) ? $container->get(Cache::class) : new ArrayCache();
33 1
        $emConfig = $globalConfig['entity_manager'] ?? [];
34 1
        $connectionConfig = $emConfig['connection'] ?? [];
35 1
        $ormConfig = $emConfig['orm'] ?? [];
36
37 1
        if (! Type::hasType(ChronosDateTimeType::CHRONOS_DATETIME)) {
38 1
            Type::addType(ChronosDateTimeType::CHRONOS_DATETIME, ChronosDateTimeType::class);
39
        }
40
41 1
        $config = Setup::createConfiguration($isDevMode, $ormConfig['proxies_dir'] ?? null, $cache);
42 1
        $config->setMetadataDriverImpl(new PHPDriver($ormConfig['entities_mappings'] ?? []));
43
44 1
        return EntityManager::create($connectionConfig, $config);
45
    }
46
}
47