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

StreamWriter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 3
c 2
b 1
f 0
lcom 0
cbo 2
dl 0
loc 24
ccs 9
cts 10
cp 0.9
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
     * @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