private function getTotalNumberOfEntities($entity, $startOffset)
    {
        $objectManager = $this->getObjectManager($entity);
        $repository = $objectManager->getRepository($entity);

        if ($repository instanceof DocumentRepository) {
            $totalSize = $repository->createQueryBuilder()
                ->getQuery()
                ->count();
        } else {
            $dataStoreMetadata = $objectManager->getClassMetadata($entity);

            $identifierFieldNames = $dataStoreMetadata->getIdentifierFieldNames();

            if (!count($identifierFieldNames)) {
                throw new \Exception(sprintf('No primary key found for entity %s', $entity));
            }

            $countableColumn = reset($identifierFieldNames);

            /** @var EntityRepository $repository */
            $totalSize = $repository->createQueryBuilder('size')
                ->select(sprintf('count(size.%s)', $countableColumn))
                ->getQuery()
                ->getSingleScalarResult();
        }

        return $totalSize - $startOffset;
    }