Completed
Push — master ( 693e1e...7ea7b3 )
by smiley
03:06
created

QROutputAbstract::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class QROutputAbstract
4
 *
5
 * @filesource   QROutputAbstract.php
6
 * @created      09.12.2015
7
 * @package      chillerlan\QRCode\Output
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2015 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode\Output;
14
15
use chillerlan\QRCode\{
16
	Data\QRMatrix, QROptions
17
};
18
19
/**
20
 *
21
 */
22
abstract class QROutputAbstract implements QROutputInterface{
23
24
	/**
25
	 * @param \chillerlan\QRCode\Data\QRMatrix $matrix
26
	 */
27
	protected $matrix;
28
29
	/**
30
	 * @var int
31
	 */
32
	protected $moduleCount;
33
34
	/**
35
	 * @var object
36
	 */
37
	protected $options;
38
39
	/**
40
	 * @param \chillerlan\QRCode\QROptions      $options
41
	 * @param \chillerlan\QRCode\Data\QRMatrix  $matrix
42
	 *
43
	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
44
	 */
45
	public function __construct(QROptions $options, QRMatrix $matrix){
46
		$this->options = $options;
47
48
		$this->moduleCount = $matrix->size();
49
50
		if($this->moduleCount < 21){  // minimum QR modules @todo: quet zone
51
			throw new QRCodeOutputException('Invalid matrix!');
52
		}
53
54
		$this->matrix = $matrix;
55
	}
56
57
	/**
58
	 * @param string $data
59
	 *
60
	 * @return bool
61
	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
62
	 */
63
	protected function saveToFile(string $data):bool {
64
65
		try{
66
			return (bool)file_put_contents($this->options->cachefile, $data);
67
		}
68
		catch(\Exception $e){
69
			throw new QRCodeOutputException('Could not write data to cache file: '.$e->getMessage());
70
		}
71
72
	}
73
74
}
75