Passed
Push — master ( 459db1...1dc4c7 )
by smiley
02:05
created

QROutputAbstract::dump()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 9.6666
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, QRCode, QROptions
17
};
18
19
/**
20
 *
21
 */
22
abstract class QROutputAbstract implements QROutputInterface{
23
24
	/**
25
	 * @var int
26
	 */
27
	protected $moduleCount;
28
29
	/**
30
	 * @param \chillerlan\QRCode\Data\QRMatrix $matrix
31
	 */
32
	protected $matrix;
33
34
	/**
35
	 * @var \chillerlan\QRCode\QROptions
36
	 */
37
	protected $options;
38
39
	/**
40
	 * @var string
41
	 */
42
	protected $outputMode;
43
44
	/**
45
	 * @var string;
46
	 */
47
	protected $defaultMode;
48
49
	/**
50
	 * QROutputAbstract constructor.
51
	 *
52
	 * @param \chillerlan\QRCode\QROptions     $options
53
	 * @param \chillerlan\QRCode\Data\QRMatrix $matrix
54
	 */
55
	public function __construct(QROptions $options, QRMatrix $matrix){
56
		$this->options     = $options;
57
		$this->matrix      = $matrix;
58
		$this->moduleCount = $this->matrix->size();
59
60
		$class = get_called_class();
61
62
		if(array_key_exists($class, QRCode::OUTPUT_MODES) && in_array($this->options->outputType, QRCode::OUTPUT_MODES[$class])){
63
			$this->outputMode = $this->options->outputType;
64
		}
65
66
	}
67
68
	/**
69
	 * @see file_put_contents()
70
	 *
71
	 * @param string $data
72
	 *
73
	 * @return bool|int
74
	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
75
	 */
76
	protected function saveToFile(string $data) {
77
78 View Code Duplication
		if(!is_writable(dirname($this->options->cachefile))){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
			throw new QRCodeOutputException('Could not write data to cache file: '.$this->options->cachefile);
80
		}
81
82
		return file_put_contents($this->options->cachefile, $data);
83
	}
84
85
	/**
86
	 * @return string
87
	 */
88
	public function dump(){
89
		$data = call_user_func([$this, $this->outputMode ?? $this->defaultMode]);
90
91
		if($this->options->cachefile !== null){
92
			$this->saveToFile($data);
93
		}
94
95
		return $data;
96
	}
97
98
}
99