protected function createEntityManager(ConfigRepository $config)
    {
        if (!isset($config['doctrine'])) {
            throw new Exception('Doctrine configuration not registered.');
        }

        if (!isset($config['database'])) {
            throw new Exception('Database configuration not registered.');
        }

        $doctrineConfig = $config['doctrine'];
        $databaseConfig = $config['database'];

        $connectionConfig = $this->createConnectionConfig($doctrineConfig, $databaseConfig);

        $type              = array_get($doctrineConfig, 'mapping', self::METADATA_ANNOTATIONS);
        $paths             = array_get($doctrineConfig, 'paths', [base_path('app/Entities')]);
        $debug             = $config['app.debug'];
        $proxyDir          = array_get($doctrineConfig, 'proxy.directory');
        $simpleAnnotations = array_get($doctrineConfig, 'simple_annotations', false);

        $metadataConfiguration = $this->createMetadataConfiguration($type, $paths, $debug, $proxyDir, null,
            $simpleAnnotations);

        $this->configureMetadataConfiguration($metadataConfiguration, $doctrineConfig);

        $eventManager = new EventManager;

        $this->configureEventManager($doctrineConfig, $eventManager);

        $entityManager = EntityManager::create($connectionConfig, $metadataConfiguration, $eventManager);

        $this->configureEntityManager($doctrineConfig, $entityManager);

        return $entityManager;
    }