mageekguy\atoum\mock\stream: lines coverage

95% of 424

OPs

98% of 60

Lines

87% of 71

Branches

74% of 38

Paths
Method OPs OPs % Lines Line % Branches Branches % Paths Path %
mageekguy\atoum\mock\stream::__call() 22 100% 1 100% 1 100% 1 100%
mageekguy\atoum\mock\stream::getAdapter() 17 100% 1 100% 1 100% 1 100%
mageekguy\atoum\mock\stream::setAdapter() 8 100% 2 100% 1 0% 1 100%
mageekguy\atoum\mock\stream::get() 114 99% 16 100% 13 92% 12 58%
mageekguy\atoum\mock\stream::getSubStream() 28 100% 1 100% 1 100% 1 100%
mageekguy\atoum\mock\stream::getProtocol() 33 100% 7 100% 4 100% 2 100%
mageekguy\atoum\mock\stream::setDirectorySeparator() 47 100% 2 100% 5 100% 2 100%
mageekguy\atoum\mock\stream::setControllerForMethod() 65 98% 14 100% 28 96% 10 90%
mageekguy\atoum\mock\stream::getController() 12 100% 1 100% 1 100% 1 100%
mageekguy\atoum\mock\stream::getControllerFromArguments() 51 78% 9 89% 7 71% 3 67%
mageekguy\atoum\mock\stream::findControllerForStream() 27 63% 6 100% 9 56% 4 25%
#
1
<?php
2

                    
3
namespace mageekguy\atoum\mock;
4

                    
5
use
6
	mageekguy\atoum\adapter,
7
	mageekguy\atoum\exceptions\logic,
8
	mageekguy\atoum\exceptions\runtime
9
;
10

                    
11
class stream
12
{
13
	const defaultProtocol = 'atoum';
14
	const protocolSeparator = '://';
15

                    
16
	protected $streamController = null;
17

                    
18
	protected static $adapter = null;
19
	protected static $streams = array();
20
	protected static $protocols = array();
21

                    
22
	public function __call($method, array $arguments)100%
23
	{
24
		return call_user_func_array(array($this->setControllerForMethod($method, $arguments)->streamController, $method), $arguments);
25
	}
26

                    
27
	public static function getAdapter()100%
28
	{
29
		return (static::$adapter = static::$adapter ?: new adapter());
30
	}
31

                    
32
	public static function setAdapter(adapter $adapter)100%
33
	{
34
		static::$adapter = $adapter;
35
	}
36

                    
37
	public static function get($name = null)100%
38
	{
39
		$name = static::setDirectorySeparator($name ?: uniqid());
40

                    
41
		$adapter = static::getAdapter();
42

                    
43
		if (($protocol = static::getProtocol($name)) === null)
44
		{
45
			$protocol = static::defaultProtocol;
46
			$name = $protocol . static::protocolSeparator . $name;
47
		}
48

                    
49
		if (in_array($protocol, $adapter->stream_get_wrappers()) === false && $adapter->stream_wrapper_register($protocol, get_called_class(), 0) === false)
50
		{
51
			throw new runtime('Unable to register ' . $protocol . ' stream');
52
		}
53

                    
54
		$stream = static::findControllerForStream($name);
55

                    
56
		if ($stream === null)
57
		{
58
			static::$streams[] = $stream = static::getController($name);
59
		}
60

                    
61
		return $stream;
62
	}
63

                    
64
	public static function getSubStream(stream\controller $controller, $stream = null)100%
65
	{
66
		return static::get($controller . DIRECTORY_SEPARATOR . static::setDirectorySeparator($stream ?: uniqid()));
67
	}
68

                    
69
	public static function getProtocol($stream)100%
70
	{
71
		$scheme = null;
72

                    
73
		$schemeSeparator = strpos($stream, static::protocolSeparator);
74

                    
75
		if ($schemeSeparator !== false)
76
		{
77
			$scheme = substr($stream, 0, $schemeSeparator);
78
		}
79

                    
80
		return $scheme;
81
	}
82

                    
83
	public static function setDirectorySeparator($stream, $directorySeparator = DIRECTORY_SEPARATOR)100%
84
	{
85
		$path = str_replace(($directorySeparator == '/' ? '\\' : '/'), $directorySeparator, preg_replace('#^[^:]+://#', '', $stream));
86

                    
87
		return substr($stream, 0, strlen($stream) - strlen($path)) . $path;
88
	}
89

                    
90
	protected function setControllerForMethod($method, array $arguments)100%
91
	{
92
		switch (strtolower($method))
93
		{
94
			case 'dir_opendir':
95
			case 'mkdir':
96
			case 'rename':
97
			case 'rmdir':
98
			case 'stream_metadata':
99
			case 'stream_open':
100
			case 'unlink':
101
			case 'url_stat':
102
			case 'stat':
103
				$this->streamController = static::getControllerFromArguments($arguments)->duplicate();
104
				break;
105
		}
106

                    
107
		return $this;
108
	}
109

                    
110
	protected static function getController($stream)100%
111
	{
112
		return new stream\controller($stream);
113
	}
114

                    
115
	protected static function getControllerFromArguments(array $arguments)89%
116
	{
117
		if (isset($arguments[0]) === false)
118
		{
119
			throw new logic('Argument 0 is undefined for function ' . $method . '()');
120
		}
121

                    
122
		$stream = static::findControllerForStream(static::setDirectorySeparator($arguments[0]));
123

                    
124
		if ($stream === null)
125
		{
126
			$stream = static::get($arguments[0]);
127
		}
128

                    
129
		return $stream;
130
	}
131

                    
132
	protected static function findControllerForStream($path)100%
133
	{
134
		foreach (static::$streams as $stream)
135
		{
136
			if ($stream->getPath() === $path)
137
			{
138
				return $stream;
139
			}
140
		}
141

                    
142
		return null;
143
	}
144
}