Passed
Push — master ( b6ba86...2e4ed0 )
by Thomas
02:28
created

ResourceWrapper::createURI()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 1
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Allow to load a stream resource using generated URI/context.
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
6
 * @copyright Copyright (c) 2009-2017 FluentDOM Contributors
7
 */
8
9
namespace FluentDOM\Utility {
10
11
  /**
12
   * Allow to load a stream resource using generated URI/context.
13
   */
14
  class ResourceWrapper {
15
16
    /**
17
     * @var array
18
     */
19
    private static $__streams = [];
20
21
    /**
22
     * @var null|resource
23
     */
24
    private $__stream;
25
    /**
26
     * @var string
27
     */
28
    private $__id = '';
29
30
    /**
31
     * @var resource
32
     */
33
    public $context;
34
35
    /**
36
     * Return an URI to open the stream, the actual stream will be stored in the
37
     * class and removed by the destructor.
38
     *
39
     * @param resource $stream
40
     * @param string $protocol
41
     * @return string
42
     */
43 5
    public static function createURI($stream, string $protocol = 'fluentdom-resource'): string {
44 5
      self::register($protocol);
45
      do {
46 5
        $id = uniqid('fd', TRUE);
47 5
      } while(isset(self::$__streams[$id]));
48 5
      self::$__streams[$id] = $stream;
49 5
      return $protocol.'://'.$id;
50
    }
51
52
    /**
53
     * Return an URI and a context to open the stream, the actual stream will be stored in the
54
     * context.
55
     *
56
     * @param resource $stream
57
     * @param string $protocol
58
     * @return array
59
     */
60 2
    public static function createContext($stream, string $protocol = 'fluentdom-resource'): array {
61 2
      self::register($protocol);
62
      return [
63 2
        $protocol.'://context', stream_context_create([$protocol => ['stream' => $stream]])
64
      ];
65
    }
66
67
    /**
68
     * Register the stream wrapper for the given protocol, if it is not registered yet.
69
     *
70
     * @param string $protocol
71
     */
72 7
    private static function register($protocol) {
73 7
      if (!in_array($protocol, stream_get_wrappers(), TRUE)) {
74 1
        stream_wrapper_register($protocol, __CLASS__);
75
      }
76 7
    }
77
78
    /**
79
     * @param string $path
80
     * @param int $flags
81
     * @return array
82
     */
83 1
    public function url_stat(string $path , int $flags) {
2 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $flags is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84 1
      return [];
85
    }
86
87
    /**
88
     * @param string $path
89
     * @param string $mode
90
     * @param int $options
91
     * @param string $opened_path
92
     * @return bool
93
     */
94 6
    public function stream_open($path, $mode, $options, &$opened_path): bool {
3 ignored issues
show
Unused Code introduced by
The parameter $mode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $opened_path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95 6
      list($protocol, $id) = explode('://', $path);
96 6
      $context = stream_context_get_options($this->context);
97
      if (
98 6
        isset($context[$protocol], $context[$protocol]['stream']) &&
99 1
        is_resource($context[$protocol]['stream'])
100
      ) {
101 1
        $this->__stream = $context[$protocol]['stream'];
102 1
        return TRUE;
103
      }
104 5
      if (isset(self::$__streams[$id])) {
105 4
        $this->__stream = self::$__streams[$id];
106 4
        $this->__id = $id;
107 4
        return TRUE;
108
      }
109 1
      return FALSE;
110
    }
111
112
    /**
113
     * @param int $count
114
     * @return bool|string
115
     */
116 4
    public function stream_read(int $count) {
117 4
      return fread($this->__stream, $count);
118
    }
119
120
    /**
121
     * @param string $data
122
     * @return bool|int
123
     */
124 1
    public function stream_write(string $data) {
125 1
      return fwrite($this->__stream, $data);
126
    }
127
128
    /**
129
     * @return bool
130
     */
131 4
    public function stream_eof(): bool {
132 4
      return feof($this->__stream);
133
    }
134
135
    /**
136
     * @param int $offset
137
     * @param int $whence
138
     * @return int
139
     */
140 1
    public function stream_seek(int $offset, int $whence): int {
141 1
      return fseek($this->__stream, $offset, $whence);
142
    }
143
144 7
    public function __destruct() {
145 7
      if (isset($this->__id, self::$__streams[$this->__id])) {
146 4
        unset(self::$__streams[$this->__id]);
147
      }
148 7
    }
149
  }
150
}
151