Skip to content

Commit

Permalink
replaced Exceptions to dir with name Exception without s on the end
Browse files Browse the repository at this point in the history
reduced complexity Stream class
  • Loading branch information
KonstantinKuklin committed Aug 29, 2014
1 parent 8bd7c23 commit e140bcb
Show file tree
Hide file tree
Showing 16 changed files with 324 additions and 215 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ $stream = new \Stream\Stream($path, $protocol, $port, $driver);

**path** - Path to file on system or ip address in network or hostname which we will work

**protocol** - String value of protocol type, can be Stream::PROTOCOL_TCP,Stream::PROTOCOL_UDP,Stream::PROTOCOL_UNIX
**protocol** - String value of protocol type, can be Connection::PROTOCOL_TCP, Connection::PROTOCOL_UDP, Connection::PROTOCOL_UNIX

**port** - Integer value of port to connect. Not needs if protocol Stream::PROTOCOL_UNIX. Default value = 0.
**port** - Integer value of port to connect. Not needs if protocol Connection::PROTOCOL_UNIX. Default value = 0.

**driver** - Object implements StreamDriverInterface. If your connection need to change transfer data you need to describe it logic with this object. Default value is null(mean data haven't been changed)

Expand All @@ -64,4 +64,4 @@ $stream = new \Stream\Stream($path, $protocol, $port, $driver);
$stream->sendContents($contents);
```

**contents** - can contain any data
**contents** - can contain any string data
78 changes: 78 additions & 0 deletions src/Stream/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* @author KonstantinKuklin <konstantin.kuklin@gmail.com>
*/

namespace Stream;

class Connection
{
const PROTOCOL_TCP = 'tcp';
const PROTOCOL_UDP = 'udp';
const PROTOCOL_UNIX = 'unix';

private $path = null;
private $protocol = null;
private $port = 0;

/**
* @param string $path
* @param string $protocol
* @param int $port
*
* @throws Exception\PathValidateStreamException
* @throws Exception\PortValidateStreamException
* @throws Exception\ProtocolValidateStreamException
*/
public function __construct($path, $protocol, $port = 0)
{
// $this->validatePath($path);
Validator::validateProtocol($protocol);
// it is doesn't matter what is the port if protocol is UNIX
if ($protocol !== self::PROTOCOL_UNIX) {
Validator::validatePort($port);
}
Validator::validatePath($path);

$this->port = $port;
$this->path = $path;
$this->protocol = $protocol;
}

/**
* @return string
*/
public function getProtocol()
{
return $this->protocol;
}

/**
* @return int
*/
public function getPort()
{
return $this->port;
}

/**
* @return null|string
*/
public function getPath()
{
return $this->path;
}

/**
* @return string
*/
public function getUrlConnection()
{
$urlConnection = $this->getProtocol() . '://' . $this->getPath();
if ($this->getPort() > 0) {
$urlConnection .= ':' . $this->getPort();
}

return $urlConnection;
}
}
67 changes: 67 additions & 0 deletions src/Stream/DataHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* @author KonstantinKuklin <konstantin.kuklin@gmail.com>
*/

namespace Stream;

use Stream\Exception\NotStringStreamException;

class DataHandler
{
private $driver = null;

public function __construct(StreamDriverInterface $driver = null)
{
$this->driver = $driver;
}

/**
* @param string $data
*
* @return mixed
*/
public function prepareReceiveData($data)
{
if ($this->hasDriver()) {
return $this->getDriver()->prepareReceiveData($data);
}

return $data;
}

/**
* @param mixed $data
*
* @throws NotStringStreamException
* @return string
*/
public function prepareSendData($data)
{
if ($this->hasDriver()) {
$data = $this->getDriver()->prepareSendData($data);
}

if (!is_string($data)) {
throw new NotStringStreamException("prepareSendData method must return string.");
}

return $data;
}

/**
* @return boolean
*/
private function hasDriver()
{
return ($this->driver !== null);
}

/**
* @return null|StreamDriverInterface
*/
private function getDriver()
{
return $this->driver;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Stream\Exceptions;
namespace Stream\Exception;

/**
* @author KonstantinKuklin <konstantin.kuklin@gmail.com>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Stream\Exceptions;
namespace Stream\Exception;

/**
* @author KonstantinKuklin <konstantin.kuklin@gmail.com>
Expand Down
11 changes: 11 additions & 0 deletions src/Stream/Exception/PathValidateStreamException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Stream\Exception;

/**
* @author KonstantinKuklin <konstantin.kuklin@gmail.com>
*/
class PathValidateStreamException extends ValidateStreamException
{

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Stream\Exceptions;
namespace Stream\Exception;

/**
* @author KonstantinKuklin <konstantin.kuklin@gmail.com>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Stream\Exceptions;
namespace Stream\Exception;

/**
* @author KonstantinKuklin <konstantin.kuklin@gmail.com>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Stream\Exceptions;
namespace Stream\Exception;

/**
* @author KonstantinKuklin <konstantin.kuklin@gmail.com>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Stream\Exceptions;
namespace Stream\Exception;

/**
* @author KonstantinKuklin <konstantin.kuklin@gmail.com>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Stream\Exceptions;
namespace Stream\Exception;

/**
* @author KonstantinKuklin <konstantin.kuklin@gmail.com>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Stream\Exceptions;
namespace Stream\Exception;

/**
* @author KonstantinKuklin <konstantin.kuklin@gmail.com>
Expand Down
Loading

0 comments on commit e140bcb

Please sign in to comment.