protected function saveResetTokens($tokens, FileWriter $writer = null)
    {
        // Before saving, remove all expired tokens
        $time = time();
        foreach ($tokens as $id => $token) {
            if ($time > $token['valid']) {
                unset($tokens[$id]);
            }
        }

        $fileName = $this->dir . self::RESET_TOKENS;
        $yaml = \Symfony\Component\Yaml\Yaml::dump($tokens, 1, 2);
        
        if ($writer && $writer->isOpened()) {
            // An exclusive lock is already held, then use the given writer instance
            $writer->write($yaml); // Will throw on write error
        } else {
            self::preparePath($this->dir, dirname(self::RESET_TOKENS));
            if ((self::writeFile($fileName, $yaml) === false)) {
                throw new \RuntimeException("Unable to save token file (".self::RESET_TOKENS.").");
            }
        }
    }