Skip to content

Commit

Permalink
Change return type of convertTo method to url (or null in case of fai…
Browse files Browse the repository at this point in the history
…lure)
  • Loading branch information
ncjoes committed Nov 13, 2016
1 parent 44778ca commit 3338f7f
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 20 deletions.
117 changes: 101 additions & 16 deletions src/OfficeConverter/OfficeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace NcJoes\OfficeConverter;

/**
* Class OfficeConverter
*
* @package NcJoes\OfficeConverter
*/
class OfficeConverter
{
private $file;
Expand All @@ -10,10 +15,69 @@ class OfficeConverter
private $extension;
private $basename;

/**
* OfficeConverter constructor.
*
* @param $filename
* @param null $tempPath
* @param string $bin
*/
public function __construct($filename, $tempPath = null, $bin = 'soffice')
{
$this->file = $this->open($filename);
if ($this->open($filename)) {
$this->setup($tempPath, $bin);
}
}

/**
* @param $filename
*
* @return null|string
* @throws OfficeConverterException
*/
public function convertTo($filename)
{
$outputExtension = pathinfo($filename, PATHINFO_EXTENSION);
$supportedExtensions = $this->getAllowedConverter($this->extension);

if (!in_array($outputExtension, $supportedExtensions)) {
throw new OfficeConverterException("Output extension({$outputExtension}) not supported for input file({$this->basename})");
}

$outdir = $this->tempPath;
shell_exec($this->makeCommand($outdir, $outputExtension));

return $this->prepOutput($outdir, $filename, $outputExtension);
}

/**
* @param $filename
*
* @return bool
* @throws OfficeConverterException
*/
protected function open($filename)
{
if (!file_exists($filename)) {
throw new OfficeConverterException('File does not exist --'.$filename);
}
$this->file = realpath($filename);

return true;
}

/**
* @param $tempPath
* @param $bin
*
* @throws OfficeConverterException
*/
protected function setup($tempPath, $bin)
{
//basename
$this->basename = pathinfo($this->file, PATHINFO_BASENAME);

//extension
$extension = pathinfo($this->file, PATHINFO_EXTENSION);

//Check for valid input file extension
Expand All @@ -26,36 +90,57 @@ public function __construct($filename, $tempPath = null, $bin = 'soffice')
if (!is_dir($tempPath)) {
$tempPath = dirname($this->file);
}
$this->tempPath = $tempPath;
$this->tempPath = realpath($tempPath);

//binary location
$this->bin = $bin;
}

protected function open($filename)
/**
* @param $outputDirectory
* @param $outputExtension
*
* @return string
*/
protected function makeCommand($outputDirectory, $outputExtension)
{
if (!file_exists($filename)) {
throw new OfficeConverterException('File does not exist --'.$filename);
$oriFile = escapeshellarg($this->file);
$outputDirectory = escapeshellarg($outputDirectory);
if (PHP_OS === 'WINNT') {
$cmd = "{$this->bin} --headless -convert-to {$outputExtension} {$oriFile} -outdir {$outputDirectory}";
}
else {
$cmd = "{$this->bin} --headless --convert-to {$outputExtension} {$oriFile} --outdir {$outputDirectory}";
}

return realpath($filename);
return $cmd;
}

public function convertTo($filename)
/**
* @param $outdir
* @param $filename
* @param $outputExtension
*
* @return null|string
*/
protected function prepOutput($outdir, $filename, $outputExtension)
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$allowedExt = $this->getAllowedConverter($this->extension);

if (!in_array($ext, $allowedExt)) {
throw new OfficeConverterException("Output extension({$ext}) not supported for input file({$this->basename})");
$tmpName = str_replace($this->extension, '', $this->basename).$outputExtension;
if (rename($outdir.'\\'.$tmpName, $outdir.'\\'.$filename)) {
return $outdir.'\\'.$filename;
}
elseif (is_file($outdir.'\\'.$tmpName)) {
return $outdir.'\\'.$tmpName;
}

$oriFile = escapeshellarg($this->file);
$outdir = escapeshellarg($this->tempPath);
$cmd = "{$this->bin} --headless -convert-to {$ext} {$oriFile} -outdir {$outdir}";
shell_exec($cmd);
return null;
}

/**
* @param null $extension
*
* @return array|mixed
*/
private function getAllowedConverter($extension = null)
{
$allowedConverter = [
Expand Down
8 changes: 4 additions & 4 deletions tests/OfficeConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public function setUp()

public function testDocxToPdfConversion()
{
$this->converter->convertTo('test1.pdf');
$output = $this->converter->convertTo('result1.pdf');

$this->assertFileExists($this->outDir.'\test1.pdf');
$this->assertFileExists($output);
}

public function testDocxToHtmlConversion()
{
$this->converter->convertTo('test1.html');
$output = $this->converter->convertTo('result1.html');

$this->assertFileExists($this->outDir.'\test1.html');
$this->assertFileExists($output);
}
}

0 comments on commit 3338f7f

Please sign in to comment.