Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Apr 23, 2017
1 parent 3854140 commit 2e42111
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

namespace Endroid\QrCode\Exception;

class MissingWriterException extends QrCodeException
class InvalidWriterException extends QrCodeException
{
}
14 changes: 14 additions & 0 deletions src/Exception/UnsupportedExtensionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* (c) Jeroen van den Enden <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Endroid\QrCode\Exception;

class UnsupportedExtensionException extends QrCodeException
{
}
23 changes: 16 additions & 7 deletions src/QrCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
namespace Endroid\QrCode;

use Endroid\QrCode\Exception\InvalidPathException;
use Endroid\QrCode\Exception\MissingWriterException;
use Endroid\QrCode\Exception\InvalidWriterException;
use Endroid\QrCode\Exception\UnsupportedExtensionException;
use Endroid\QrCode\Writer\BinaryWriter;
use Endroid\QrCode\Writer\EpsWriter;
use Endroid\QrCode\Writer\PngDataUriWriter;
Expand Down Expand Up @@ -152,6 +153,14 @@ public function registerWriter(WriterInterface $writer)
}
}

/**
* @return WriterInterface[]
*/
public function getRegisteredWriters()
{
return $this->writers;
}

/**
* @param string $text
* @return $this
Expand Down Expand Up @@ -474,7 +483,7 @@ public function getValidateResult()
/**
* @param string $writerClass
* @return string
* @throws MissingWriterException
* @throws InvalidWriterException
*/
public function getContentType($writerClass)
{
Expand All @@ -485,12 +494,12 @@ public function getContentType($writerClass)

/**
* @param string $writerClass
* @throws MissingWriterException
* @throws InvalidWriterException
*/
protected function assertValidWriterClass($writerClass)
{
if (!isset($this->writers[$writerClass])) {
throw new MissingWriterException('Invalid writer "'.$writerClass.'"');
throw new InvalidWriterException('Invalid writer "'.$writerClass.'"');
}
}

Expand Down Expand Up @@ -535,16 +544,16 @@ public function getWriterByPath($path)
/**
* @param string $extension
* @return WriterInterface
* @throws MissingWriterException
* @throws UnsupportedExtensionException
*/
public function getWriterByExtension($extension)
{
foreach ($this->writers as $writer) {
if (in_array($extension, $writer->getSupportedExtensions())) {
if ($writer->supportsExtension($extension)) {
return $writer;
}
}

throw new MissingWriterException('Missing writer for extension "'.$extension.'"');
throw new UnsupportedExtensionException('Missing writer for extension "'.$extension.'"');
}
}
15 changes: 12 additions & 3 deletions src/Twig/Extension/QrCodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

namespace Endroid\QrCode\Twig\Extension;

use Endroid\QrCode\Exception\UnsupportedExtensionException;
use Endroid\QrCode\Factory\QrCodeFactory;
use Endroid\QrCode\Writer\AbstractDataUriWriter;
use Endroid\QrCode\Writer\PngDataUriWriter;
use Endroid\QrCode\Writer\SvgDataUriWriter;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand Down Expand Up @@ -91,8 +93,9 @@ public function getQrCodeReference($text, array $options = [], $referenceType)

/**
* @param string $text
* @param array $options
* @param array $options
* @return string
* @throws UnsupportedExtensionException
*/
public function qrcodeDataUriFunction($text, array $options = [])
{
Expand All @@ -103,8 +106,14 @@ public function qrcodeDataUriFunction($text, array $options = [])
}

$qrCode = $this->qrCodeFactory->create($text, $options);
$writer = $qrCode->getWriterByExtension($extension);
$internalWriter = $qrCode->getWriterByExtension($extension);

foreach ($qrCode->getRegisteredWriters() as $writer) {
if ($writer instanceof AbstractDataUriWriter && $writer->getInternalWriterClass() == get_class($internalWriter)) {
return $writer->writeString();
}
}

return $writer->writeString();
throw new UnsupportedExtensionException('Extenstion '.$extension.' is not supported by any of the writers');
}
}
2 changes: 1 addition & 1 deletion src/Writer/AbstractDataUriWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getContentType()
/**
* {@inheritdoc}
*/
public function getSupportedExtensions()
protected function getSupportedExtensions()
{
return [];
}
Expand Down
13 changes: 11 additions & 2 deletions src/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,18 @@ public function writeFile($path)
abstract public function getContentType();

/**
* {@inheritdoc}
* @param string $extension
* @return bool
*/
public function supportsExtension($extension)
{
return in_array($extension, $this->getSupportedExtensions());
}

/**
* @return array
*/
public function getSupportedExtensions()
protected function getSupportedExtensions()
{
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Writer/BinaryWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getContentType()
/**
* {@inheritdoc}
*/
public function getSupportedExtensions()
protected function getSupportedExtensions()
{
return ['bin', 'txt'];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Writer/EpsWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getContentType()
/**
* {@inheritdoc}
*/
public function getSupportedExtensions()
protected function getSupportedExtensions()
{
return ['eps'];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Writer/PngWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function getContentType()
/**
* {@inheritdoc}
*/
public function getSupportedExtensions()
protected function getSupportedExtensions()
{
return ['png'];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Writer/SvgWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getContentType()
/**
* {@inheritdoc}
*/
public function getSupportedExtensions()
protected function getSupportedExtensions()
{
return ['svg'];
}
Expand Down
5 changes: 3 additions & 2 deletions src/Writer/WriterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function writeFile($path);
public function getContentType();

/**
* @return array
* @param string $extension
* @return bool
*/
public function getSupportedExtensions();
public function supportsExtension($extension);
}

0 comments on commit 2e42111

Please sign in to comment.