StreamWriter::refreshResource()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\IO\Stream;
26
27
use Brickoo\Component\IO\Stream\Exception\InvalidResourceHandleException;
28
use Brickoo\Component\IO\Stream\Exception\UnableToWriteBytesException;
29
use Brickoo\Component\Common\Assert;
30
31
/**
32
 * StreamWriter
33
 *
34
 * Implements a stream writer who
35
 * does use a max. number of retries to write
36
 * the content to the resource.
37
 * @author Celestino Diaz <[email protected]>
38
 */
39
class StreamWriter {
40
41
    /** @var resource */
42
    private $streamResource;
43
44
    /** @var integer */
45
    private $numberOfRetries;
46
47
    /**
48
     * @param resource $streamResource
49
     * @param integer $numberOfRetries
50
     * @throws \InvalidArgumentException
51
     */
52 3
    public function __construct($streamResource, $numberOfRetries = 3) {
53 3
        Assert::isResource($streamResource);
54 3
        Assert::isInteger($numberOfRetries);
55 3
        $this->streamResource = $streamResource;
56 3
        $this->numberOfRetries = $numberOfRetries;
57 3
    }
58
59
    /**
60
     * Refresh the local stream resource.
61
     * @param resource $streamResource
62
     * @return \Brickoo\Component\IO\Stream\StreamWriter
63
     */
64 1
    public function refreshResource($streamResource) {
65 1
        Assert::isResource($streamResource);
66 1
        $this->streamResource = $streamResource;
67 1
        return $this;
68
    }
69
70
    /**
71
     * Writes the content to the stream resource.
72
     * @param string $content
73
     * @throws \Brickoo\Component\IO\Stream\Exception\InvalidResourceHandleException
74
     * @throws \Brickoo\Component\IO\Stream\Exception\UnableToWriteBytesException
75
     * @return \Brickoo\Component\IO\Stream\StreamWriter
76
     */
77 4
    public function write($content) {
78 4
        if (!is_resource($this->streamResource)) {
79 1
            throw new InvalidResourceHandleException();
80
        }
81
82 3
        if (($bytesLeft = $this->writeWithRetryLoop($this->streamResource, $content, $this->numberOfRetries)) > 0) {
83 1
            throw new UnableToWriteBytesException($bytesLeft);
84
        }
85
86 2
        return $this;
87
    }
88
89
    /**
90
     * Writes the content with a retry loop to the stream resource.
91
     * @param resource $streamResource
92
     * @param string $content
93
     * @param integer $retries
94
     * @return integer the unwritten bytes
95
     */
96 2
    private function writeWithRetryLoop($streamResource, $content, $retries) {
97 2
        $bytesLength = strlen($content);
98 2
        $bytesLeft = $bytesLength;
99
100 2
        while ($bytesLeft > 0 && $retries > 0) {
101 2
            $offset = $bytesLength - $bytesLeft;
102 2
            if (!($bytesWritten = fwrite($streamResource, substr($content, $offset), $bytesLeft))) {
103 1
                --$retries;
104 1
                continue;
105
            }
106
107 1
            $bytesLeft -= $bytesWritten;
108 1
        }
109 2
        return (int)$bytesLeft;
110
    }
111
112
}
113