Completed
Push — master ( 7edff1...afe7e9 )
by Thibaud
02:57
created

StreamWriter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A writeFromReader() 0 17 3
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\Writer;
13
14
use Alchemy\Resource\ResourceReader;
15
use Alchemy\Resource\ResourceUri;
16
use Alchemy\Resource\ResourceWriter;
17
18
class StreamWriter implements ResourceWriter
19
{
20
21
    /**
22
     * @param ResourceReader $reader
23
     * @param ResourceUri $target
24
     */
25 12
    public function writeFromReader(ResourceReader $reader, ResourceUri $target)
26
    {
27 12
        $targetResource = @fopen($target->getUri(), 'w+', true);
28
29 12
        if (! is_resource($targetResource)) {
30 4
            throw new \RuntimeException('Unable to open ' . $target->getUri() . ' for writing');
31
        }
32
33 8
        $sourceResource = $reader->getContentsAsStream();
34 8
        $bytesCopied = @stream_copy_to_stream($sourceResource, $targetResource);
35
36 8
        if ($bytesCopied <= 0) {
37 4
            throw new \RuntimeException('Unable to write to ' . $target->getUri());
38
        };
39
40 4
        fclose($targetResource);
41 4
    }
42
}
43