Skip to content

Commit

Permalink
refactor: Rename method Processor#writeToDisk to saveToFile
Browse files Browse the repository at this point in the history
* Add a brief explanation to the classes in the Processors namespace
  • Loading branch information
MontealegreLuis committed Jan 17, 2018
1 parent 4b05ee3 commit d098706
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Actions/GenerateClassDiagram.php
Expand Up @@ -59,7 +59,7 @@ public function generate(CodeFinder $finder, string $imagePath): void
$this->command()->runningProcessor($this->imageProcessor());
$image = $this->imageProcessor()->process($dotLanguage);
$this->command()->savingResult();
$this->imageProcessor->writeToDisk($image, $imagePath);
$this->imageProcessor->saveToFile($image, $imagePath);
}

/** @throws LogicException If no image processor is provided */
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/GenerateDotFile.php
Expand Up @@ -52,6 +52,6 @@ public function generate(CodeFinder $finder, string $dotFilePath): void
$this->command()->runningProcessor($this->dotProcessor);
$dotLanguage = $this->dotProcessor->process($structure);
$this->command()->savingResult();
$this->dotProcessor->writeToDisk($dotLanguage, $dotFilePath);
$this->dotProcessor->saveToFile($dotLanguage, $dotFilePath);
}
}
2 changes: 1 addition & 1 deletion src/Actions/GenerateStatistics.php
Expand Up @@ -47,6 +47,6 @@ public function generate(CodeFinder $finder, string $filePath): void
$this->command()->runningProcessor($this->processor);
$statistics = $this->processor->process($structure);
$this->command()->savingResult();
$this->processor->writeToDisk($statistics, $filePath);
$this->processor->saveToFile($statistics, $filePath);
}
}
4 changes: 4 additions & 0 deletions src/Processors/DotProcessor.php
Expand Up @@ -4,8 +4,12 @@
*
* This source file is subject to the license that is bundled with this package in the file LICENSE.
*/

namespace PhUml\Processors;

/**
* It creates a `png` class diagram using the `dot` command
*/
class DotProcessor extends ImageProcessor
{
public function command(): string
Expand Down
3 changes: 3 additions & 0 deletions src/Processors/GraphvizProcessor.php
Expand Up @@ -18,6 +18,9 @@
use Twig_Environment as TemplateEngine;
use Twig_Loader_Filesystem as Filesystem;

/**
* It creates a digraph from a `Structure` and returns it as a string in DOT format
*/
class GraphvizProcessor extends Processor
{
/** @var ClassGraphBuilder */
Expand Down
8 changes: 6 additions & 2 deletions src/Processors/ImageGenerationFailure.php
Expand Up @@ -4,14 +4,18 @@
*
* This source file is subject to the license that is bundled with this package in the file LICENSE.
*/

namespace PhUml\Processors;

use RuntimeException;

/**
* It is thrown when either the `dot` or `neato` commands fail
*/
class ImageGenerationFailure extends RuntimeException
{
public function __construct($output)
public static function withOutput(string $errorMessage): ImageGenerationFailure
{
parent::__construct("Execution of external program failed:\n{$output}");
return new ImageGenerationFailure("Execution of external program failed:\n{$errorMessage}");
}
}
16 changes: 15 additions & 1 deletion src/Processors/ImageProcessor.php
Expand Up @@ -4,11 +4,19 @@
*
* This source file is subject to the license that is bundled with this package in the file LICENSE.
*/

namespace PhUml\Processors;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;

/**
* It generates a `png` class diagram from a digraph in DOT format
*
* It takes a digraph in DOT format, saves it to a temporary location and creates a `png` class
* diagram out of it.
* It uses either the `dot` or `neato` command to create the image
*/
abstract class ImageProcessor extends Processor
{
/** @var Process */
Expand All @@ -23,6 +31,9 @@ public function __construct(Process $process = null, Filesystem $fileSystem = nu
$this->fileSystem = $fileSystem ?? new Filesystem();
}

/**
* It returns the contents of a `png` class diagram
*/
public function process(string $digraphInDotFormat): string
{
$dotFile = $this->fileSystem->tempnam('/tmp', 'phuml');
Expand All @@ -41,12 +52,15 @@ public function process(string $digraphInDotFormat): string
return $image;
}

/**
* @throws ImageGenerationFailure If the Grpahviz command failed
*/
public function execute(string $inputFile, string $outputFile): void
{
$this->process->setCommandLine([$this->command(), '-Tpng', '-o', $outputFile, $inputFile]);
$this->process->run();
if (!$this->process->isSuccessful()) {
throw new ImageGenerationFailure($this->process->getErrorOutput());
throw ImageGenerationFailure::withOutput($this->process->getErrorOutput());
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Processors/NeatoProcessor.php
Expand Up @@ -4,8 +4,12 @@
*
* This source file is subject to the license that is bundled with this package in the file LICENSE.
*/

namespace PhUml\Processors;

/**
* It creates a `png` class diagram using the `neato` command
*/
class NeatoProcessor extends ImageProcessor
{
public function command(): string
Expand Down
5 changes: 4 additions & 1 deletion src/Processors/Processor.php
Expand Up @@ -6,9 +6,12 @@
*/
namespace PhUml\Processors;

/**
* Every processor has a name and can write their output to a file
*/
abstract class Processor
{
public function writeToDisk(string $contents, string $filePath): void
public function saveToFile(string $contents, string $filePath): void
{
file_put_contents($filePath, $contents);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Processors/StatisticsProcessor.php
Expand Up @@ -9,6 +9,9 @@
use PhUml\Code\Structure;
use PhUml\Code\Summary;

/**
* It takes a code `Structure` and extracts a `Summary` of its contents as text
*/
class StatisticsProcessor extends Processor
{
public function name(): string
Expand Down

0 comments on commit d098706

Please sign in to comment.