Skip to content

Commit

Permalink
LocalSvgRenderer check for correct svg file
Browse files Browse the repository at this point in the history
  • Loading branch information
jmleroux committed Mar 23, 2016
1 parent b584260 commit 1bf3f01
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 19 deletions.
13 changes: 13 additions & 0 deletions spec/Fixtures/invalid_template/plastic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions spec/Fixtures/xml_template/plastic.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions spec/PUGX/Poser/Render/SvgRenderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ function it_should_render_a_svg()
$this->render($badge)->shouldBeAValidSVGImage();
}

function it_should_not_render_an_invalid_svg($calculator)
{
$templatesDir = __DIR__ . '/../../../Fixtures/invalid_template';
$this->beConstructedWith($calculator, $templatesDir);
$badge = Badge::fromURI('version-stable-97CA00.svg');
$this->shouldThrow(new \RuntimeException('Generated string is not a valid XML'))->duringRender($badge);
}

function it_should_not_render_non_svg_xml($calculator)
{
$templatesDir = __DIR__ . '/../../../Fixtures/xml_template';
$this->beConstructedWith($calculator, $templatesDir);
$badge = Badge::fromURI('version-stable-97CA00.svg');
$this->shouldThrow(new \RuntimeException('Generated xml is not a SVG'))->duringRender($badge);
}

public function getMatchers()
{
return array(
Expand Down
50 changes: 31 additions & 19 deletions src/Render/LocalSvgRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PUGX\Poser\Calculator\GDTextSizeCalculator;
use PUGX\Poser\Calculator\TextSizeCalculatorInterface;
use PUGX\Poser\Image;
use SimpleXMLElement;

/**
* Local SVG renderer.
Expand All @@ -31,16 +32,26 @@ abstract class LocalSvgRenderer implements RenderInterface
*/
private $textSizeCalculator;

/**
* @var string
*/
private $templatesDirectory;

/**
* @param TextSizeCalculatorInterface $textSizeCalculator
* @param null|string $templatesDirectory
*/
public function __construct(TextSizeCalculatorInterface $textSizeCalculator = null)
public function __construct(TextSizeCalculatorInterface $textSizeCalculator = null, $templatesDirectory = null)
{
$this->textSizeCalculator = $textSizeCalculator;

if (null === $this->textSizeCalculator) {
$this->textSizeCalculator = new GDTextSizeCalculator();
}

$this->templatesDirectory = $templatesDirectory;
if (null === $this->templatesDirectory) {
$this->templatesDirectory = __DIR__ . '/../Resources/templates';;
}
}

/**
Expand All @@ -50,7 +61,7 @@ public function __construct(TextSizeCalculatorInterface $textSizeCalculator = nu
*/
public function render(Badge $badge)
{
$template = $this->getTemplate($this->getTemplateName());
$template = $this->getTemplate($this->getTemplateName());
$parameters = $this->buildParameters($badge);

return $this->renderSvg($template, $parameters, $badge->getFormat());
Expand All @@ -68,8 +79,7 @@ abstract protected function getTemplateName();
*/
private function getTemplate($format)
{
$templatesDirectory = __DIR__ . '/../Resources/templates';
$filepath = sprintf('%s/%s.svg', $templatesDirectory, $format);
$filepath = sprintf('%s/%s.svg', $this->templatesDirectory, $format);

if (!file_exists($filepath)) {
throw new \InvalidArgumentException(sprintf('No template for format %s', $format));
Expand All @@ -90,7 +100,7 @@ private function stringWidth($text)

/**
* @param string $render
* @param array $parameters
* @param array $parameters
* @param string $format
*
* @return Image
Expand All @@ -101,11 +111,13 @@ private function renderSvg($render, $parameters, $format)
$render = str_replace(sprintf('{{ %s }}', $key), $variable, $render);
}

// validate svg
libxml_use_internal_errors(true);
$xml = simplexml_load_string($render);
if (false === $xml) {
throw new \RuntimeException('Generated string is not a valid SVG');
try {
$xml = new SimpleXMLElement($render);
} catch (\Exception $e) {
throw new \RuntimeException('Generated string is not a valid XML');
}
if ('svg' !== $xml->getName()) {
throw new \RuntimeException('Generated xml is not a SVG');
}

return Image::createFromString($render, $format);
Expand All @@ -120,15 +132,15 @@ private function buildParameters(Badge $badge)
{
$parameters = array();

$parameters['vendorWidth'] = $this->stringWidth($badge->getSubject());
$parameters['valueWidth'] = $this->stringWidth($badge->getStatus());
$parameters['totalWidth'] = $parameters['valueWidth'] + $parameters['vendorWidth'];
$parameters['vendorColor'] = static::VENDOR_COLOR;
$parameters['valueColor'] = $badge->getHexColor();
$parameters['vendor'] = $badge->getSubject();
$parameters['value'] = $badge->getStatus();
$parameters['vendorWidth'] = $this->stringWidth($badge->getSubject());
$parameters['valueWidth'] = $this->stringWidth($badge->getStatus());
$parameters['totalWidth'] = $parameters['valueWidth'] + $parameters['vendorWidth'];
$parameters['vendorColor'] = static::VENDOR_COLOR;
$parameters['valueColor'] = $badge->getHexColor();
$parameters['vendor'] = $badge->getSubject();
$parameters['value'] = $badge->getStatus();
$parameters['vendorStartPosition'] = round($parameters['vendorWidth'] / 2, 1) + 1;
$parameters['valueStartPosition'] = $parameters['vendorWidth'] + round($parameters['valueWidth'] / 2, 1) - 1;
$parameters['valueStartPosition'] = $parameters['vendorWidth'] + round($parameters['valueWidth'] / 2, 1) - 1;

return $parameters;
}
Expand Down

0 comments on commit 1bf3f01

Please sign in to comment.