/**
  * Export redirects to CSV
  *
  * This command will export all redirects in CSV format. You can set a preferred
  * filename before the export with the optional ``filename`` argument. If no ``filename`` argument is supplied, the
  * export will be returned within the CLI. This operation requires the package ``league/csv``. Install it by running
  * ``composer require league/csv``.
  *
  * @param string $filename (optional) The filename for the CSV file
  * @param string $host (optional) Only export hosts for a specified host
  * @return void
  */
 public function exportCommand($filename = null, $host = null)
 {
     if (!class_exists(Writer::class)) {
         $this->outputWarningForLeagueCsvPackage();
     }
     $writer = Writer::createFromFileObject(new \SplTempFileObject());
     if ($host !== null) {
         $redirects = $this->redirectStorage->getAll($host);
     } else {
         $redirects = new \AppendIterator();
         foreach ($this->redirectStorage->getDistinctHosts() as $host) {
             $redirects->append($this->redirectStorage->getAll($host));
         }
     }
     /** @var $redirect RedirectInterface */
     foreach ($redirects as $redirect) {
         $writer->insertOne([$redirect->getSourceUriPath(), $redirect->getTargetUriPath(), $redirect->getStatusCode(), $redirect->getHost()]);
     }
     if ($filename === null) {
         $writer->output();
     } else {
         file_put_contents($filename, (string) $writer);
     }
 }