Completed
Branch full-rewrite (4754d3)
by Thibaud
03:13
created

StreamReader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
wmc 6
lcom 1
cbo 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __destruct() 0 6 2
A getContents() 0 4 1
A getContentsAsStream() 0 12 2
1
<?php
2
3
namespace Alchemy\Zippy\Resource\Reader;
4
5
use Alchemy\Zippy\Resource\Resource;
6
use Alchemy\Zippy\Resource\ResourceReader;
7
use Alchemy\Zippy\Resource\ResourceUri;
8
9
class StreamReader implements ResourceReader
10
{
11
    /**
12
     * @var \Alchemy\Zippy\Resource\Resource
13
     */
14
    private $resource;
15
16
    /**
17
     * @var resource[]
18
     */
19
    private $streams = [];
20
21
    /**
22
     * @param ResourceUri $resource
23
     */
24
    public function __construct(ResourceUri $resource)
25
    {
26
        $this->resource = $resource;
0 ignored issues
show
Documentation Bug introduced by
It seems like $resource of type object<Alchemy\Zippy\Resource\ResourceUri> is incompatible with the declared type object<Alchemy\Zippy\Resource\Resource> of property $resource.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
    }
28
29
    public function __destruct()
30
    {
31
        foreach ($this->streams as $stream) {
32
            @fclose($stream);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
33
        }
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    public function getContents()
40
    {
41
        return stream_get_contents($this->getContentsAsStream());
42
    }
43
44
    /**
45
     * @return resource
46
     */
47
    public function getContentsAsStream()
48
    {
49
        $stream = @fopen(rawurldecode($this->resource), 'r');
50
51
        if ($stream === false) {
52
            throw new \RuntimeException('Unable to open stream resource for ' . rawurldecode($this->resource));
53
        }
54
55
        $this->streams[] = $stream;
56
57
        return $stream;
58
    }
59
}
60