Skip to content

Commit

Permalink
Add input option to convert documents to PDF.
Browse files Browse the repository at this point in the history
  • Loading branch information
hamidghorashi committed Aug 7, 2020
1 parent 4ba1a38 commit 8836e17
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Changelog

## v1.0.0 (05/08/2020)
*No changelog for this release.*
3 changes: 1 addition & 2 deletions config/application.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
return [
// App info
'AppName' => 'PreviewMaker',
'Version' => '1.0.0',
'Version' => '1.1.0',

// App settings
'settings' => require __DIR__ . "/settings.config.php",
Expand All @@ -14,7 +14,6 @@

// Commands
'commands' => [
// Command\ConvertCommand::class,
Command\PreviewCommand::class,
],
];
26 changes: 0 additions & 26 deletions src/Application/Command/ConvertCommand.php

This file was deleted.

28 changes: 12 additions & 16 deletions src/Application/Command/PreviewCommand.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php
namespace Module\Application\Command;

use Module\Application\Service\ffmpegService;
use Core\Command;
use Module\Application\Service\PreviewService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -15,27 +14,24 @@ protected function configure()
{
$this->setName('preview')
->setDescription('This creates a preview image from an input file.')
->setHelp('Demonstration of custom commands created by Symfony Console component.')
->addArgument('filepath', InputArgument::REQUIRED, 'Path to the file you want to make preview from')
->setHelp('This command is to create an image or pdf preview from an input file.')
->addArgument('inputFile', InputArgument::REQUIRED, 'Path to the file you want to make preview from')
->addOption('pdf', null, InputOption::VALUE_NONE, 'Convert given document to pdf')
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Path to output file');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$filePath = $input->getArgument('filepath');
$service = $this->getPreviewService()->setFile($filePath);
$inputFile = $input->getArgument('inputFile');
$service = $this->getPreviewService()->setFile($inputFile);

$startTime = microtime(true);
$file = $service->preview($input->getOption('output'));
$endTime = microtime(true);
if ($input->getOption('pdf')) {
$file = $service->toPdf($input->getOption('output'));
} else {
$file = $service->preview($input->getOption('output'));
}

$output->writeln('');
$output->writeln(sprintf('<info>File name:</info> %s', $file->getName()));
$output->writeln(sprintf('<info>Directory:</info> %s', $file->getDir()));
$output->writeln(sprintf('<info>Preview:</info> %s', $file->getPath()));
$output->writeln('');
$output->writeln(sprintf('<fg=yellow;options=bold>Execution time: %s sec</>', number_format($endTime - $startTime, 5)));
$output->writeln('');
$this->writeSummary($output, $file);
}

private function getPreviewService(): PreviewService
Expand Down
28 changes: 28 additions & 0 deletions src/Application/Service/PreviewService.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function getFile(): InputFile
/**
* Create an image preview from the given file
*
* @param string $output
* @return InputFile
* @throws RuntimeException
*/
Expand All @@ -99,4 +100,31 @@ public function preview($output = null)

throw new RuntimeException(sprintf("Error: Unsupported file type! The mime type %s is not supported.", $this->file->getMetadata()->getMimeType()));
}

/**
* Create a PDF preview from the given file
*
* @param string $output
* @return void
*/
public function toPdf($output = null)
{
if (!$this->file instanceof InputFile) {
throw new RuntimeException("Error: No file has been selected.");
}

$output = !$output && isset($this->options['TmpDir']) ? $this->options['TmpDir'] : $output;

if (!$output) {
throw new RuntimeException("Error: Invalid output path.");
}

$strategy = new Strategy\DocumentStrategy($this->file);

if ($strategy->match()) {
return $strategy->toPdf($output);
}

throw new RuntimeException(sprintf("Error: Unable to convert given file to PDF. The mimetype %s is not supported.", $this->file->getMetadata()->getMimeType()));
}
}
69 changes: 69 additions & 0 deletions src/Core/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Core;

use Module\Application\FileSystem\FileInterface;
use Symfony\Component\Console\Command\Command as ConsoleCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Command extends ConsoleCommand
{
/**
* @var string|float
*/
protected $startTime;

/**
* @var string|float
*/
protected $endTime;

/**
* @inheritDoc
*/
public function __construct($name = null)
{
parent::__construct($name = null);
}

/**
* @inheritDoc
*/
public function run(InputInterface $input, OutputInterface $output)
{
$this->startTime = microtime(true);
$result = parent::run($input, $output);

return $result;
}

/**
* Calculate commmand execution time in seconds
*
* @return float
*/
protected function getExecutionTime()
{
$this->endTime = microtime(true);
return number_format($this->endTime - $this->startTime, 5);
}

/**
* Write a file summary to the output
*
* @param mixed $output
* @param mixed $file
* @return void
*/
public function writeSummary(OutputInterface $output, FileInterface $file)
{
$output->writeln('');
$output->writeln(sprintf('<info>File name:</info> %s', $file->getName()));
$output->writeln(sprintf('<info>Directory:</info> %s', $file->getDir()));
$output->writeln(sprintf('<info>Preview:</info> %s', $file->getPath()));
$output->writeln('');
$output->writeln(sprintf('<fg=yellow;options=bold>Execution time: %s sec</>', $this->getExecutionTime()));
$output->writeln('');
}
}
12 changes: 12 additions & 0 deletions src/Core/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ private function checkRequirements()
return $this;
}

/**
* Registers a new command.
*
* @param string $name The command name
*
* @return Command The newly created command
*/
public function register($name)
{
return $this->add(new Command($name));
}

/**
* Register available commands
*
Expand Down

0 comments on commit 8836e17

Please sign in to comment.