Passed
Push — v5 ( 93618e...84eb31 )
by smiley
01:52
created

QRCodeReader   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 66
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 2
A readFile() 0 11 5
A readResource() 0 2 1
A readBlob() 0 11 2
A decode() 0 7 2
1
<?php
2
/**
3
 * Class QRCodeReader
4
 *
5
 * @created      17.01.2021
6
 * @author       ZXing Authors
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2021 Smiley
9
 * @license      Apache-2.0
10
 *
11
 * @noinspection PhpComposerExtensionStubsInspection
12
 */
13
14
namespace chillerlan\QRCode;
15
16
use Imagick, InvalidArgumentException;
17
use chillerlan\QRCode\Decoder\{Decoder, DecoderResult, GDLuminanceSource, IMagickLuminanceSource};
18
use function extension_loaded, file_exists, file_get_contents, imagecreatefromstring, is_file, is_readable;
19
20
final class QRCodeReader{
21
22
	private bool $useImagickIfAvailable;
23
24
	public function __construct(bool $useImagickIfAvailable = true){
25
		$this->useImagickIfAvailable = $useImagickIfAvailable && extension_loaded('imagick');
26
	}
27
28
	/**
29
	 * @param \Imagick|\GdImage|resource $im
30
	 *
31
	 * @return \chillerlan\QRCode\Decoder\DecoderResult
32
	 * @phan-suppress PhanUndeclaredTypeParameter (GdImage)
33
	 */
34
	protected function decode($im):DecoderResult{
35
36
		$source = $this->useImagickIfAvailable
37
			? new IMagickLuminanceSource($im)
0 ignored issues
show
Bug introduced by
It seems like $im can also be of type GdImage and resource; however, parameter $imagick of chillerlan\QRCode\Decode...ceSource::__construct() does only seem to accept Imagick, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
			? new IMagickLuminanceSource(/** @scrutinizer ignore-type */ $im)
Loading history...
38
			: new GDLuminanceSource($im);
0 ignored issues
show
Bug introduced by
It seems like $im can also be of type Imagick; however, parameter $gdImage of chillerlan\QRCode\Decode...ceSource::__construct() does only seem to accept GdImage|resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
			: new GDLuminanceSource(/** @scrutinizer ignore-type */ $im);
Loading history...
39
40
		return (new Decoder)->decode($source);
41
	}
42
43
	/**
44
	 * @param string $imgFilePath
45
	 *
46
	 * @return \chillerlan\QRCode\Decoder\DecoderResult
47
	 */
48
	public function readFile(string $imgFilePath):DecoderResult{
49
50
		if(!file_exists($imgFilePath) || !is_file($imgFilePath) || !is_readable($imgFilePath)){
51
			throw new InvalidArgumentException('invalid file: '.$imgFilePath);
52
		}
53
54
		$im = $this->useImagickIfAvailable
55
			? new Imagick($imgFilePath)
56
			: imagecreatefromstring(file_get_contents($imgFilePath));
57
58
		return $this->decode($im);
59
	}
60
61
	/**
62
	 * @param string $imgBlob
63
	 *
64
	 * @return \chillerlan\QRCode\Decoder\DecoderResult
65
	 */
66
	public function readBlob(string $imgBlob):DecoderResult{
67
68
		if($this->useImagickIfAvailable){
69
			$im = new Imagick;
70
			$im->readImageBlob($imgBlob);
71
		}
72
		else{
73
			$im = imagecreatefromstring($imgBlob);
74
		}
75
76
		return $this->decode($im);
77
	}
78
79
	/**
80
	 * @param \Imagick|\GdImage|resource $imgSource
81
	 *
82
	 * @return \chillerlan\QRCode\Decoder\DecoderResult
83
	 */
84
	public function readResource($imgSource):DecoderResult{
85
		return $this->decode($imgSource);
86
	}
87
88
}
89