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

StreamWriter::writeFromReader()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 2
crap 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