Doctrine\MongoDB\Query\Query::withReadPreference PHP Method

withReadPreference() private method

Executes a closure with a temporary read preference on a database or collection.
private withReadPreference ( Doctrine\MongoDB\Database | Doctrine\MongoDB\Collection $object, Closure $closure ) : mixed
$object Doctrine\MongoDB\Database | Doctrine\MongoDB\Collection
$closure Closure
return mixed
    private function withReadPreference($object, \Closure $closure)
    {
        if (!isset($this->query['readPreference'])) {
            return $closure();
        }
        $prevReadPref = $object->getReadPreference();
        $object->setReadPreference($this->query['readPreference'], $this->query['readPreferenceTags']);
        try {
            $result = $closure();
        } catch (\Exception $e) {
        }
        $prevTags = !empty($prevReadPref['tagsets']) ? $prevReadPref['tagsets'] : null;
        $object->setReadPreference($prevReadPref['type'], $prevTags);
        if (isset($e)) {
            throw $e;
        }
        return $result;
    }