protected function getOnStatsCallback($initialOnStats, $requestId) : \Closure
    {
        return function (TransferStats $stats) use ($initialOnStats, $requestId) {
            if (is_callable($initialOnStats)) {
                call_user_func($initialOnStats, $stats);
            }

            $this->dataCollector->addTotalTime((float)$stats->getTransferTime());

            if ($requestId && $this->logger instanceof Logger) {
                $this->logger->addTransferTimeByRequestId($requestId, (float)$stats->getTransferTime());
            }
        };
    }