Completed
Push — master ( 7fa6d5...a90232 )
by Thibaud
02:55
created

StreamWriter::writeFromReader()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 17
ccs 9
cts 10
cp 0.9
rs 9.4285
cc 3
eloc 9
nc 3
nop 2
crap 3.009
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
     * @param ResourceReader $reader
22
     * @param ResourceUri $target
23
     */
24 8
    public function writeFromReader(ResourceReader $reader, ResourceUri $target)
25
    {
26 8
        $targetResource = @fopen($target->getUri(), 'w+', true);
27
28 8
        if (! is_resource($targetResource)) {
29
            throw new \RuntimeException('Unable to open ' . $target->getUri() . ' for writing');
30
        }
31
32 8
        $sourceResource = $reader->getContentsAsStream();
33 8
        $bytesCopied = @stream_copy_to_stream($sourceResource, $targetResource);
34
35 8
        if ($bytesCopied <= 0) {
36 4
            throw new \RuntimeException('Unable to write to ' . $target->getUri());
37
        };
38
39 4
        fclose($targetResource);
40 4
    }
41
}
42