Completed
Push — master ( 24969c...7fa6d5 )
by Thibaud
02:39
created

StreamReader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 43
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __destruct() 0 6 2
A getContentsAsStream() 0 12 2
1
<?php
2
3
/*
4
 * This file is part of alchemy/resource-component.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Alchemy\Resource\Reader;
13
14
use Alchemy\Resource\ResourceReader;
15
use Alchemy\Resource\ResourceUri;
16
17
class StreamReader implements ResourceReader
18
{
19
    /**
20
     * @var ResourceUri
21
     */
22
    private $resource;
23
24
    /**
25
     * @var resource[]
26
     */
27
    private $streams = [];
28
29
    /**
30
     * @param ResourceUri $resource
31
     */
32 16
    public function __construct(ResourceUri $resource)
33
    {
34 16
        $this->resource = $resource;
35 16
    }
36
37 16
    public function __destruct()
38
    {
39 16
        foreach ($this->streams as $stream) {
40 8
            @fclose($stream);
0 ignored issues
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...
41 12
        }
42 16
    }
43
44
    /**
45
     * @return resource
46
     */
47 12
    public function getContentsAsStream()
48
    {
49 12
        $stream = @fopen($this->resource, 'r');
50
51 12
        if ($stream === false) {
52 4
            throw new \RuntimeException('Unable to open stream resource for ' . $this->resource);
53
        }
54
55 8
        $this->streams[] = $stream;
56
57 8
        return $stream;
58
    }
59
}
60