protected function getConnection(Configuration $config)
    {
        $connection = $this->getOption('connection');

        switch (true) {
            case is_array($connection):
                $connection = new Connection(
                    array_key_exists('server', $connection) ? $connection['server'] : null,
                    array_key_exists('options', $connection) ? $connection['options'] : [],
                    $config,
                    $this->getEventManager()
                );
                break;

            case $connection instanceof Connection:
                if ($connection->getEventManager() !== $this->getEventManager()) {
                    throw new \RuntimeException(
                        'Cannot use different EventManager instances for DocumentManager and Connection.'
                    );
                }
                break;

            default:
                throw new \InvalidArgumentException('Invalid argument: ' . $connection);
        }

        return $connection;
    }