Passed
Pull Request — develop (#20)
by Pieter van der
17:16
created

Tiqr_AutoLoader   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 26.79%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
eloc 60
c 2
b 0
f 0
dl 0
loc 96
ccs 15
cts 56
cp 0.2679
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setIncludePath() 0 6 1
B setOptions() 0 39 7
A __construct() 0 5 2
A getInstance() 0 6 2
B autoload() 0 21 7
1
<?php
2
class Tiqr_AutoLoader {
3
4
	protected static $instance;
5
6
	protected $tiqrPath;
7
	protected $qrcodePath;
8
	protected $zendPath;
9
10
	protected function __construct($options) {
11
		if ($options !== NULL) {
12
			$this->setOptions($options);
13
		}
14
		spl_autoload_register(array(__CLASS__, 'autoload'));
15
	}
16
17 3
	public static function getInstance($options = NULL) {
18 3
		if (null === self::$instance) {
19
			self::$instance = new self($options);
20
		}
21
22 3
		return self::$instance;
23
	}
24
25 3
	public static function autoload($className) {
26 3
		if($className === NULL) {
27
			return;
28
		}
29
30 3
		$self = self::getInstance();
31
32 3
		$substr5 = substr($className, 0, 5);
33
34 3
		if ($substr5 === 'Tiqr_' || $substr5 === 'OATH_') {
35 2
			$file = $self->tiqrPath . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
36 1
		} elseif ($className === 'QRcode') {
37
			$file = $self->qrcodePath . DIRECTORY_SEPARATOR . 'qrlib.php';
38 1
		} elseif ($substr5 === 'Zend_') {
39
			$file = $self->zendPath . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
40
		} else {
41 1
			return;
42
		}
43
44 2
		if (file_exists($file)) {
45 2
			require_once($file);
46
		}
47 2
	}
48
49
	public function setOptions($options) {
50
		if (isset($options["tiqr.path"])) {
51
			$tiqr_dir = $options["tiqr.path"];
52
			$tiqr_path = realpath($tiqr_dir);
53
		} else {
54
			$tiqr_dir = dirname(__FILE__);
55
			$tiqr_path = $tiqr_dir;
56
		}
57
		if (is_dir($tiqr_path)) {
58
			$this->tiqrPath = $tiqr_path;
59
		} else {
60
			throw new Exception('Directory not found: ' . var_export($tiqr_dir, TRUE));
61
		}
62
63
		if (isset($options["phpqrcode.path"])) {
64
			$qrcode_dir = $options["phpqrcode.path"];
65
			$qrcode_path = realpath($qrcode_dir);
66
		} else {
67
			$qrcode_dir = dirname(dirname(dirname(__FILE__))) . '/phpqrcode';
68
			$qrcode_path = $qrcode_dir;
69
		}
70
71
		if (is_dir($qrcode_path)) {
72
			$this->qrcodePath = $qrcode_path;
73
		} else {
74
			throw new Exception('Directory not found: ' . var_export($qrcode_dir, TRUE));
75
		}
76
77
		if (isset($options["zend.path"])) {
78
			$zend_dir = $options["zend.path"];
79
			$zend_path = realpath($zend_dir);
80
		} else {
81
			$zend_dir = dirname(dirname(dirname(__FILE__))) . "/zend";
82
			$zend_path = $zend_dir;
83
		}
84
		if (is_dir($zend_path)) {
85
			$this->zendPath = $zend_path;
86
		} else {
87
			throw new Exception('Directory not found: ' . var_export($zend_dir, TRUE));
88
		}
89
	}
90
91
92
	public function setIncludePath() {
93
		set_include_path(implode(PATH_SEPARATOR, array(
94
			$this->tiqrPath,
95
			$this->zendPath,
96
			$this->qrcodePath,
97
			get_include_path(),
98
		)));
99
	}
100
}
101