Skip to content

Commit

Permalink
Code review by @stof
Browse files Browse the repository at this point in the history
  • Loading branch information
jmleroux committed Mar 23, 2016
1 parent 8ec56fe commit 1b70bbe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
25 changes: 15 additions & 10 deletions src/Render/LocalSvgRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,9 @@ abstract class LocalSvgRenderer implements RenderInterface
/**
* @var TextSizeCalculatorInterface
*/
protected $textSizeCalculator;
private $textSizeCalculator;

/**
* @var string
*/
protected $templateName;

/**
* Constructor.
*
* @param TextSizeCalculatorInterface $textSizeCalculator
*/
public function __construct(TextSizeCalculatorInterface $textSizeCalculator = null)
Expand All @@ -57,12 +50,17 @@ public function __construct(TextSizeCalculatorInterface $textSizeCalculator = nu
*/
public function render(Badge $badge)
{
$template = $this->getTemplate($this->templateName);
$template = $this->getTemplate($this->getTemplateName());
$parameters = $this->buildParameters($badge);

return $this->renderSvg($template, $parameters, $badge->getFormat());
}

/**
* @return string
*/
abstract protected function getTemplateName();

/**
* @param $format
*
Expand Down Expand Up @@ -103,6 +101,13 @@ protected 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');
}

return Image::createFromString($render, $format);
}

Expand All @@ -111,7 +116,7 @@ protected function renderSvg($render, $parameters, $format)
*
* @return array
*/
protected function buildParameters(Badge $badge)
private function buildParameters(Badge $badge)
{
$parameters = array();

Expand Down
7 changes: 5 additions & 2 deletions src/Render/SvgFlatRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
class SvgFlatRender extends LocalSvgRenderer
{
protected $templateName = 'flat';

/**
* A list of all supported formats.
*
Expand All @@ -29,4 +27,9 @@ public function supportedFormats()
{
return array('flat', 'svg');
}

protected function getTemplateName()
{
return 'flat';
}
}
7 changes: 5 additions & 2 deletions src/Render/SvgFlatSquareRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
class SvgFlatSquareRender extends LocalSvgRenderer
{
protected $templateName = 'flat-square';

/**
* A list of all supported formats.
*
Expand All @@ -29,4 +27,9 @@ public function supportedFormats()
{
return array('flat-square');
}

protected function getTemplateName()
{
return 'flat-square';
}
}
7 changes: 5 additions & 2 deletions src/Render/SvgRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
*/
class SvgRender extends LocalSvgRenderer
{
protected $templateName = 'plastic';

/**
* A list of all supported formats.
*
Expand All @@ -32,4 +30,9 @@ public function supportedFormats()
{
return array('plastic');
}

protected function getTemplateName()
{
return 'plastic';
}
}

0 comments on commit 1b70bbe

Please sign in to comment.