protected function ensureDirectory(string $directory): void
    {
        if (!is_dir($directory)) {
            $success = @mkdir($directory, 0775, true);
            if (!$success && !is_dir($directory)) {
                throw new ExportDataException('Unable to create directory ' . $directory . '.');
            }
        }

        if (!is_writable($directory)) {
            throw new ExportDataException('Directory ' . $directory . ' is not writable.');
        }
    }