Skip to content

Commit 1ec3a38

Browse files
author
Martin Brecht-Precht
committed
Switched from internal exception to PHP Common Excpetions.
Updated readme. Code cleanup. Added php5-curl to the vargrant setup to fullfill the composer dependency requirements.
1 parent 2cc1b61 commit 1ec3a38

8 files changed

+79
-51
lines changed

README.md

+51-22
Original file line numberDiff line numberDiff line change
@@ -34,44 +34,62 @@ require_once('path/to/vendor/autoload.php');
3434
### Encoding data as QR code block data
3535

3636
```{php}
37-
use QrCodeSuite\QrEncode\QrEncoder;
37+
use CommonException\IoException;
38+
use QrCodeSuite\QrEncode;
3839
3940
// Encode the data as QR code block data
40-
$encoder = new QrEncoder();
41-
$qrCodeData = $encoder
42-
->setLevel(QrEncoder::QR_CODE_LEVEL_LOW)
43-
->setTempDir('path/to/writable/directory')
44-
->encodeQrCode('https://github.com/markenwerk/php-qr-code-suite');
41+
try {
42+
$encoder = new QrEncode\QrEncoder();
43+
$qrCodeData = $encoder
44+
->setLevel(QrEncoder::QR_CODE_LEVEL_LOW)
45+
->setTempDir('path/to/writable/directory')
46+
->encodeQrCode('https://github.com/markenwerk/php-qr-code-suite');
47+
} catch(\InvalidArgumentException $exception) {
48+
// QR code content contains invalid characters or is too large
49+
} catch(IoException\FolderWritableException $exception) {
50+
// Temp dir is not writable
51+
} catch(QrEncode\Exception\QrEncoderException $exception) {
52+
// QR encoder failed
53+
}
4554
```
4655

4756
### Render encoded QR code block data as image
4857

4958
```{php}
59+
use CommonException\IoException;
5060
use QrCodeSuite\QrRender;
5161
use QrCodeSuite\QrRender\Color\RgbColor;
5262
use QrCodeSuite\QrRender\Color\CmykColor;
5363
5464
// Render the encoded QR code block data as RGB PNG
5565
// Width and height should measure about 800 pixels
56-
$renderer = new QrRender\QrCodeRendererPng();
57-
$renderer
58-
->setApproximateSize(800)
59-
->setForegroundColor(new RgbColor(255, 0, 255))
60-
->setBackgroundColor(new RgbColor(51, 51, 51))
61-
->render($qrCodeData, 'path/to/qr-code.png');
66+
try {
67+
$renderer = new QrRender\QrCodeRendererPng();
68+
$renderer
69+
->setApproximateSize(800)
70+
->setForegroundColor(new RgbColor(255, 0, 255))
71+
->setBackgroundColor(new RgbColor(51, 51, 51))
72+
->render($qrCodeData, 'path/to/qr-code.png');
73+
} catch(IoException\FileWritableException $exception) {
74+
// Output file not writable
75+
}
6276
6377
// Get the effective width and height of the rendered image
6478
$imageWidth = $renderer->getWidth();
6579
$imageHeight = $renderer->getHeight();
6680
6781
// Render the encoded QR code block data as CMYK TIFF
6882
// Width and height should measure about 800 pixels
69-
$renderer = new QrRender\QrCodeRendererTiff();
70-
$renderer
71-
->setApproximateSize(800)
72-
->setForegroundColor(new CmykColor(0, 100, 0, 0))
73-
->setBackgroundColor(new CmykColor(0, 100, 100, 0))
74-
->render($qrCodeData, 'path/to/qr-code.tif');
83+
try {
84+
$renderer = new QrRender\QrCodeRendererTiff();
85+
$renderer
86+
->setApproximateSize(800)
87+
->setForegroundColor(new CmykColor(0, 100, 0, 0))
88+
->setBackgroundColor(new CmykColor(0, 100, 100, 0))
89+
->render($qrCodeData, 'path/to/qr-code.tif');
90+
} catch(IoException\FileWritableException $exception) {
91+
// Output file not writable
92+
}
7593
7694
// Get the effective width and height of the rendered image
7795
$imageWidth = $renderer->getWidth();
@@ -80,14 +98,25 @@ $imageHeight = $renderer->getHeight();
8098
// Render the encoded QR code block data as CMYK vectorized EPS
8199
// EPS has no background color. It is just the blocks on blank cnavas.
82100
// EPS also has no approximate size. Scale the vectorized image as you like.
83-
$renderer = new QrRender\QrCodeRendererEps();
84-
$renderer
85-
->setForegroundColor(new CmykColor(0, 100, 0, 0))
86-
->render($qrCodeData, 'path/to/qr-code.eps');
101+
try {
102+
$renderer = new QrRender\QrCodeRendererEps();
103+
$renderer
104+
->setForegroundColor(new CmykColor(0, 100, 0, 0))
105+
->render($qrCodeData, 'path/to/qr-code.eps');
106+
} catch(IoException\FileWritableException $exception) {
107+
// Output file not writable
108+
}
87109
```
88110

89111
---
90112

113+
## Exception handling
114+
115+
PHP Google Geocoder provides different exceptions provided by the PHP Common Exceptions project for proper handling.
116+
You can find more information about [PHP Common Exceptions at Github](https://github.com/markenwerk/php-common-exceptions).
117+
118+
---
119+
91120
## Contribution
92121

93122
Contributing to our projects is always very appreciated.

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"require": {
3131
"php": ">=5.3",
3232
"ext-gd": "*",
33-
"ext-imagick": "*"
33+
"ext-imagick": "*",
34+
"markenwerk/common-exceptions": "~2.0"
3435
},
3536
"require-dev": {
3637
"phpunit/phpunit": "~4.8",

src/QrEncode/QrEncoder.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace QrCodeSuite\QrEncode;
44

5+
use CommonException\IoException;
56
use QrCodeSuite\QrEncode\Exception\QrEncoderException;
67
use QrCodeSuite\QrEncode\QrCode\QrCode;
78
use QrCodeSuite\QrEncode\QrCode\QrCodePointRow;
@@ -79,16 +80,24 @@ public function setLevel($level)
7980
/**
8081
* @param string $contents
8182
* @return QrCode
83+
* @throws IoException\FolderWritableException
8284
* @throws QrEncoderException
8385
*/
8486
public function encodeQrCode($contents)
8587
{
8688
$chars = str_split($contents);
8789
foreach ($chars as $char) {
8890
if (0 == ord($char)) {
89-
throw new QrEncoderException('Encoding null character failed');
91+
throw new \InvalidArgumentException('QR code contents contains a not allowed null character.');
9092
}
9193
}
94+
// Validate temp dir
95+
if (!is_dir($this->tempDir)) {
96+
throw new IoException\FolderWritableException('Temp dir not exists');
97+
}
98+
if (!is_writable($this->tempDir)) {
99+
throw new IoException\FolderWritableException('Temp dir not writable');
100+
}
92101
// Build temp name
93102
$pngPath = rtrim($this->tempDir, '/') . '/qr_' . time() . rand(1000, 9999) . '.png';
94103
// Build command
@@ -114,12 +123,13 @@ public function encodeQrCode($contents)
114123
$err = stream_get_contents($pipes[2]);
115124
fclose($pipes[2]);
116125
proc_close($process);
117-
if ($this->startsWith($err, "Failed to encode the input data")) {
118-
throw new QrEncoderException('Too much content');
126+
if ($this->startsWith($err, 'Failed to encode the input data')) {
127+
throw new \InvalidArgumentException('Too much content');
119128
}
120129
} else {
121130
throw new QrEncoderException('QR encoder internal error');
122131
}
132+
123133
$image = @imagecreatefrompng($pngPath);
124134
if ($image === false) {
125135
throw new QrEncoderException('GD lib internal error');

src/QrRender/Exception/IoException.php

-13
This file was deleted.

src/QrRender/QrCodeRendererEps.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace QrCodeSuite\QrRender;
44

5+
use CommonException\IoException;
56
use QrCodeSuite\QrEncode\QrCode\QrCode;
67
use QrCodeSuite\QrRender\Color\CmykColor;
7-
use QrCodeSuite\QrRender\Exception\IoException;
88
use QrCodeSuite\QrRender\PathFinder\PathPoint;
99
use QrCodeSuite\QrRender\PathFinder\QrCodePathFinder;
1010

@@ -50,12 +50,12 @@ public function setForegroundColor(CmykColor $foregroundColor)
5050
/**
5151
* @param QrCode $qrCode
5252
* @param string $filename
53-
* @throws IoException
53+
* @throws IoException\FileWritableException
5454
*/
5555
public function render(QrCode $qrCode, $filename)
5656
{
5757
if (!is_dir(dirname($filename)) || !is_writable(dirname($filename))) {
58-
throw new IoException('QR code path not writable.');
58+
throw new IoException\FileWritableException('QR code path not writable.');
5959
}
6060

6161
$pathFinder = new QrCodePathFinder();
@@ -99,7 +99,7 @@ public function render(QrCode $qrCode, $filename)
9999

100100
$bytes = @file_put_contents($filename, implode("\n", $epsSource));
101101
if ($bytes === false) {
102-
throw new IoException('QR code output file not writable.');
102+
throw new IoException\FileWritableException('QR code output file not writable.');
103103
}
104104
}
105105

src/QrRender/QrCodeRendererPng.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace QrCodeSuite\QrRender;
44

5+
use CommonException\IoException;
56
use QrCodeSuite\QrEncode\QrCode\QrCode;
67
use QrCodeSuite\QrRender\Color\RgbColor;
7-
use QrCodeSuite\QrRender\Exception\IoException;
88

99
/**
1010
* Class QrCodeRendererPng
@@ -126,12 +126,12 @@ public function getHeight()
126126
/**
127127
* @param QrCode $qrCode
128128
* @param string $filename
129-
* @throws IoException
129+
* @throws IoException\FileWritableException
130130
*/
131131
public function render(QrCode $qrCode, $filename)
132132
{
133133
if (!is_dir(dirname($filename)) || !is_writable(dirname($filename))) {
134-
throw new IoException('QR code path not writable.');
134+
throw new IoException\FileWritableException('QR code path not writable.');
135135
}
136136

137137
// Get basic info
@@ -177,7 +177,7 @@ public function render(QrCode $qrCode, $filename)
177177
$canvas->destroy();
178178

179179
if ($writeSuccess !== true) {
180-
throw new IoException('QR code output file not writable.');
180+
throw new IoException\FileWritableException('QR code output file not writable.');
181181
}
182182
}
183183

src/QrRender/QrCodeRendererTiff.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace QrCodeSuite\QrRender;
44

5+
use CommonException\IoException;
56
use QrCodeSuite\QrEncode\QrCode\QrCode;
67
use QrCodeSuite\QrRender\Color\CmykColor;
7-
use QrCodeSuite\QrRender\Exception\IoException;
88

99
/**
1010
* Class QrCodeRendererTiff
@@ -124,12 +124,12 @@ public function getHeight()
124124
/**
125125
* @param QrCode $qrCode
126126
* @param string $filename
127-
* @throws IoException
127+
* @throws IoException\FileWritableException
128128
*/
129129
public function render(QrCode $qrCode, $filename)
130130
{
131131
if (!is_dir(dirname($filename)) || !is_writable(dirname($filename))) {
132-
throw new IoException('QR code path not writable.');
132+
throw new IoException\FileWritableException('QR code path not writable.');
133133
}
134134

135135
// Get basic info
@@ -180,7 +180,7 @@ public function render(QrCode $qrCode, $filename)
180180
$block->destroy();
181181

182182
if ($writeSuccess !== true) {
183-
throw new IoException('QR code output file not writable');
183+
throw new IoException\FileWritableException('QR code output file not writable');
184184
}
185185
}
186186

vagrant-bootstrap.sh

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ apt-get install -y apache2
1010
apt-get install -y php5
1111
apt-get install -y libapache2-mod-php5
1212
apt-get install -y php5-cli
13+
apt-get install -y php5-curl
1314
apt-get install -y php5-gd
1415
apt-get install -y php5-imagick
1516
apt-get install -y qrencode

0 commit comments

Comments
 (0)