Skip to content

Commit

Permalink
reduce code complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
alexszeliga committed Feb 16, 2024
1 parent 4166802 commit b579ee1
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/Exportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Generator;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use InvalidArgumentException;
use OpenSpout\Common\Entity\Row;
use OpenSpout\Common\Entity\Style\Style;
Expand Down Expand Up @@ -36,7 +35,6 @@ abstract protected function setOptions(&$options);
/**
* @param string $path
* @param callable|null $callback
* @param string $ext
*
* @throws \OpenSpout\Common\Exception\InvalidArgumentException
* @throws \OpenSpout\Common\Exception\UnsupportedTypeException
Expand All @@ -45,17 +43,16 @@ abstract protected function setOptions(&$options);
*
* @return string
*/
public function export($path, callable $callback = null, $ext = null)
public function export($path, callable $callback = null)
{
self::exportOrDownload($path, 'openToFile', $callback, $ext);
self::exportOrDownload($path, 'openToFile', $callback);

return realpath($path) ?: $path;
}

/**
* @param $path
* @param callable|null $callback
* @param string $ext
*
* @throws \OpenSpout\Common\Exception\InvalidArgumentException
* @throws \OpenSpout\Common\Exception\UnsupportedTypeException
Expand All @@ -64,14 +61,14 @@ public function export($path, callable $callback = null, $ext = null)
*
* @return \Symfony\Component\HttpFoundation\StreamedResponse|string
*/
public function download($path, callable $callback = null, $ext = null)
public function download($path, callable $callback = null)
{
if (method_exists(response(), 'streamDownload')) {
return response()->streamDownload(function () use ($path, $callback) {
self::exportOrDownload($path, 'openToBrowser', $callback);
}, $path);
}
self::exportOrDownload($path, 'openToBrowser', $callback, $ext);
self::exportOrDownload($path, 'openToBrowser', $callback);

return '';
}
Expand All @@ -89,10 +86,10 @@ public function download($path, callable $callback = null, $ext = null)
*/
private function exportOrDownload($path, $function, callable $callback = null, $ext = null)
{
if (Str::endsWith($path, 'csv') || $ext === 'csv') {
if (str_ends_with($path, 'csv')) {
$options = new \OpenSpout\Writer\CSV\Options();
$writer = new \OpenSpout\Writer\CSV\Writer($options);
} elseif (Str::endsWith($path, 'ods') || $ext === 'ods') {
} elseif (str_ends_with($path, 'ods')) {
$options = new \OpenSpout\Writer\ODS\Options();
$writer = new \OpenSpout\Writer\ODS\Writer($options);
} else {
Expand All @@ -101,6 +98,12 @@ private function exportOrDownload($path, $function, callable $callback = null, $
}

$this->setOptions($options);

// extract file type for writing to php://output
if (str_starts_with($path,'php://export')) {
$path = explode(';', $path)[0];
}

/* @var \OpenSpout\Writer\WriterInterface $writer */
$writer->$function($path);

Expand Down

0 comments on commit b579ee1

Please sign in to comment.