Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric committed Oct 31, 2018
1 parent e035b04 commit a299f95
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
47 changes: 21 additions & 26 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@
* Import classes
*/
use Psr\Http\Message\StreamInterface;
use Sunrise\Stream\Exception\InvalidArgumentException;
use Sunrise\Stream\Exception\UnreadableStreamException;
use Sunrise\Stream\Exception\UnseekableStreamException;
use Sunrise\Stream\Exception\UntellableStreamException;
use Sunrise\Stream\Exception\UnwritableStreamException;

/**
* Stream
Expand All @@ -45,7 +40,7 @@ public function __construct($resource)
{
if (! \is_resource($resource))
{
throw new InvalidArgumentException('Invalid stream resource');
throw new Exception\InvalidArgumentException('Invalid stream resource');
}

$this->resource = $resource;
Expand Down Expand Up @@ -126,14 +121,14 @@ public function tell() : int
{
if (! $this->isResourceable())
{
throw new UntellableStreamException('Stream is not resourceable');
throw new Exception\UntellableStreamException('Stream is not resourceable');
}

$result = \ftell($this->resource);

if (false === $result)
{
throw new UntellableStreamException('Unable to get the stream pointer position');
throw new Exception\UntellableStreamException('Unable to get the stream pointer position');
}

return $result;
Expand Down Expand Up @@ -169,19 +164,19 @@ public function rewind() : void
{
if (! $this->isResourceable())
{
throw new UnseekableStreamException('Stream is not resourceable');
throw new Exception\UnseekableStreamException('Stream is not resourceable');
}

if (! $this->isSeekable())
{
throw new UnseekableStreamException('Stream is not seekable');
throw new Exception\UnseekableStreamException('Stream is not seekable');
}

$result = \fseek($this->resource, 0, \SEEK_SET);

if (! (0 === $result))
{
throw new UnseekableStreamException('Unable to move the stream pointer to beginning');
throw new Exception\UnseekableStreamException('Unable to move the stream pointer to beginning');
}
}

Expand All @@ -201,19 +196,19 @@ public function seek($offset, $whence = \SEEK_SET) : void
{
if (! $this->isResourceable())
{
throw new UnseekableStreamException('Stream is not resourceable');
throw new Exception\UnseekableStreamException('Stream is not resourceable');
}

if (! $this->isSeekable())
{
throw new UnseekableStreamException('Stream is not seekable');
throw new Exception\UnseekableStreamException('Stream is not seekable');
}

$result = \fseek($this->resource, $offset, $whence);

if (! (0 === $result))
{
throw new UnseekableStreamException('Unable to move the stream pointer to the given position');
throw new Exception\UnseekableStreamException('Unable to move the stream pointer to the given position');
}
}

Expand Down Expand Up @@ -251,19 +246,19 @@ public function write($string) : int
{
if (! $this->isResourceable())
{
throw new UnwritableStreamException('Stream is not resourceable');
throw new Exception\UnwritableStreamException('Stream is not resourceable');
}

if (! $this->isWritable())
{
throw new UnwritableStreamException('Stream is not writable');
throw new Exception\UnwritableStreamException('Stream is not writable');
}

$result = \fwrite($this->resource, $string);

if (false === $result)
{
throw new UnwritableStreamException('Unable to write to the stream');
throw new Exception\UnwritableStreamException('Unable to write to the stream');
}

return $result;
Expand All @@ -284,19 +279,19 @@ public function truncate(int $length = 0) : void
{
if (! $this->isResourceable())
{
throw new UnwritableStreamException('Stream is not resourceable');
throw new Exception\UnwritableStreamException('Stream is not resourceable');
}

if (! $this->isWritable())
{
throw new UnwritableStreamException('Stream is not writable');
throw new Exception\UnwritableStreamException('Stream is not writable');
}

$result = \ftruncate($this->resource, $length);

if (false === $result)
{
throw new UnwritableStreamException('Unable to truncate the stream');
throw new Exception\UnwritableStreamException('Unable to truncate the stream');
}
}

Expand Down Expand Up @@ -332,19 +327,19 @@ public function read($length) : string
{
if (! $this->isResourceable())
{
throw new UnreadableStreamException('Stream is not resourceable');
throw new Exception\UnreadableStreamException('Stream is not resourceable');
}

if (! $this->isReadable())
{
throw new UnreadableStreamException('Stream is not readable');
throw new Exception\UnreadableStreamException('Stream is not readable');
}

$result = \fread($this->resource, $length);

if (false === $result)
{
throw new UnreadableStreamException('Unable to read from the stream');
throw new Exception\UnreadableStreamException('Unable to read from the stream');
}

return $result;
Expand All @@ -363,19 +358,19 @@ public function getContents() : string
{
if (! $this->isResourceable())
{
throw new UnreadableStreamException('Stream is not resourceable');
throw new Exception\UnreadableStreamException('Stream is not resourceable');
}

if (! $this->isReadable())
{
throw new UnreadableStreamException('Stream is not readable');
throw new Exception\UnreadableStreamException('Stream is not readable');
}

$result = \stream_get_contents($this->resource);

if (false === $result)
{
throw new UnreadableStreamException('Unable to read remainder of the stream');
throw new Exception\UnreadableStreamException('Unable to read remainder of the stream');
}

return $result;
Expand Down
9 changes: 4 additions & 5 deletions src/StreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ public function createStream(string $content = '') : StreamInterface
*/
public function createStreamFromFile(string $filename, string $mode = 'r') : StreamInterface
{
\set_error_handler(function() {}, E_WARNING);

$resource = \fopen($filename, $mode);

\restore_error_handler();
// If the open fails, an error of level E_WARNING is generated.
// You may use @ to suppress this warning.
// http://php.net/manual/en/function.fopen.php
$resource = @ \fopen($filename, $mode);

if (false === $resource)
{
Expand Down

0 comments on commit a299f95

Please sign in to comment.