Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
347 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 15.07.15 | ||
* Time: 00:31 | ||
*/ | ||
|
||
namespace Rpodwika\Designpatterns\Behavioral\State; | ||
|
||
|
||
class ConnectionClosed implements ConnectionState | ||
{ | ||
public function open() | ||
{ | ||
return 'Opening connection'; | ||
} | ||
|
||
public function close() | ||
{ | ||
return 'Connection already closed'; | ||
} | ||
|
||
public function reconnect() | ||
{ | ||
return 'Reconnecting'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 15.07.15 | ||
* Time: 00:26 | ||
*/ | ||
|
||
namespace Rpodwika\Designpatterns\Behavioral\State; | ||
|
||
|
||
class ConnectionEstablished implements ConnectionState | ||
{ | ||
public function open() | ||
{ | ||
return 'Cannot open already opened connection'; | ||
} | ||
|
||
public function close() | ||
{ | ||
return 'Freeing resources'; | ||
} | ||
|
||
public function reconnect() | ||
{ | ||
return "Waiting for reconnection"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 15.07.15 | ||
* Time: 00:25 | ||
*/ | ||
|
||
namespace Rpodwika\Designpatterns\Behavioral\State; | ||
|
||
|
||
interface ConnectionState | ||
{ | ||
public function open(); | ||
public function close(); | ||
public function reconnect(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 15.07.15 | ||
* Time: 00:33 | ||
*/ | ||
|
||
namespace Rpodwika\Designpatterns\Behavioral\State; | ||
|
||
|
||
class DbConnection | ||
{ | ||
/** | ||
* @var ConnectionState $state | ||
*/ | ||
private $state; | ||
private $port; | ||
private $uri; | ||
|
||
public function __construct($port, $uri) | ||
{ | ||
$this->port = $port; | ||
$this->uri = $uri; | ||
} | ||
|
||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getState() | ||
{ | ||
return $this->state; | ||
} | ||
|
||
/** | ||
* @param mixed $state | ||
* @return $this | ||
*/ | ||
public function setState(ConnectionState $state) | ||
{ | ||
$this->state = $state; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getUri() | ||
{ | ||
return $this->uri; | ||
} | ||
|
||
/** | ||
* @param mixed $uri | ||
* @return $this | ||
*/ | ||
public function setUri($uri) | ||
{ | ||
$this->uri = $uri; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getPort() | ||
{ | ||
return $this->port; | ||
} | ||
|
||
/** | ||
* @param mixed $port | ||
* @return $this | ||
*/ | ||
public function setPort($port) | ||
{ | ||
$this->port = $port; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function connect() | ||
{ | ||
$this->state = new ConnectionEstablished(); | ||
$this->state->open(); | ||
|
||
return sprintf('Opening connection %s:%d', $this->getUri(), $this->getPort()); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function close() | ||
{ | ||
$this->state = new ConnectionClosed(); | ||
$this->state->close(); | ||
|
||
return 'Closing connection'; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function reconnect() | ||
{ | ||
$this->close(); | ||
$this->connect(); | ||
|
||
return 'Reconnecting'; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#State | ||
|
||
State allows oject to later its behavior when its internal state changes. | ||
The key idea in this pattern is to create State object that represents current state of an object. | ||
|
||
#When to use | ||
|
||
* When object behavior depends on state and it must change behavior at runtime depending on that state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 15.07.15 | ||
* Time: 00:41 | ||
*/ | ||
|
||
namespace Rpodwika\Designpatterns\Tests\Behavioral\State; | ||
|
||
use Rpodwika\Designpatterns\Behavioral\State\ConnectionClosed; | ||
|
||
class ConnectionClosedTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @covers \Rpodwika\Designpatterns\Behavioral\State\ConnectionClosed::open | ||
*/ | ||
public function testOpen() | ||
{ | ||
$connection = new ConnectionClosed(); | ||
$this->assertEquals('Opening connection', $connection->open()); | ||
} | ||
|
||
/** | ||
* @covers \Rpodwika\Designpatterns\Behavioral\State\ConnectionClosed::close | ||
*/ | ||
public function testClose() | ||
{ | ||
$connection = new ConnectionClosed(); | ||
$this->assertEquals('Connection already closed', $connection->close()); | ||
} | ||
|
||
/** | ||
* @covers \Rpodwika\Designpatterns\Behavioral\State\ConnectionClosed::reconnect | ||
*/ | ||
public function testReconnect() | ||
{ | ||
$connection = new ConnectionClosed(); | ||
$this->assertEquals('Reconnecting', $connection->reconnect()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 15.07.15 | ||
* Time: 00:41 | ||
*/ | ||
|
||
namespace Rpodwika\Designpatterns\Tests\Behavioral\State; | ||
|
||
use Rpodwika\Designpatterns\Behavioral\State\ConnectionEstablished; | ||
|
||
class ConnectionEstablishedTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @covers \Rpodwika\Designpatterns\Behavioral\State\ConnectionEstablished::open | ||
*/ | ||
public function testOpen() | ||
{ | ||
$connection = new ConnectionEstablished(); | ||
$this->assertEquals('Cannot open already opened connection', $connection->open()); | ||
} | ||
|
||
/** | ||
* @covers \Rpodwika\Designpatterns\Behavioral\State\ConnectionEstablished::close | ||
*/ | ||
public function testClose() | ||
{ | ||
$connection = new ConnectionEstablished(); | ||
$this->assertEquals('Freeing resources', $connection->close()); | ||
} | ||
|
||
/** | ||
* @covers \Rpodwika\Designpatterns\Behavioral\State\ConnectionEstablished::reconnect | ||
*/ | ||
public function testReconnect() | ||
{ | ||
$connection = new ConnectionEstablished(); | ||
$this->assertEquals('Waiting for reconnection', $connection->reconnect()); | ||
} | ||
} |
Oops, something went wrong.