Skip to content

Commit

Permalink
Code clarity enhancements;
Browse files Browse the repository at this point in the history
Unused and ugly "autoconnect" flag removed from setConnectionParameters;
Tests followed the modifications
  • Loading branch information
adamturcsan committed Nov 29, 2016
1 parent d866ccc commit 42963b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
34 changes: 17 additions & 17 deletions src/ReconnectingPDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ReconnectingPDO
/**
* @var PDO
*/
protected $db;
protected $connection;

/**
* @var string DSN
Expand Down Expand Up @@ -110,42 +110,44 @@ public function __call($method, $arguments)
*/
protected function call($method, $arguments)
{
if (!($this->db instanceof PDO)) {
if (!($this->connection instanceof PDO)) {
throw new ReconnectingPDOException('No PDO connection is set');
}
try {
$returnValue = call_user_func_array([$this->db, $method], $arguments);
$returnValue = call_user_func_array([$this->connection, $method],
$arguments);
} catch (\PDOException $ex) {
if (!stristr($ex->getMessage(), "server has gone away") || $ex->getCode() != 'HY000') {
throw $ex;
}
if ($this->reconnectCounter < $this->maxReconnection) {
$this->reconnectDb();
$returnValue = $this->call($method, $arguments); // Retry
$this->resetCounter();
} else {
if ($this->reconnectCounter >= $this->maxReconnection) {
throw new ExceededMaxReconnectionException('ReconnectingPDO has exceeded max reconnection limit',
$ex->getCode(), $ex);
}
$this->reconnectDb();
$returnValue = $this->call($method, $arguments); // Retry
$this->resetCounter();
}
if ($returnValue instanceof \PDOStatement) {
return new ReconnectingPDOStatement($returnValue, $this, $method === 'query');
return new ReconnectingPDOStatement($returnValue, $this,
$method === 'query');
}
return $returnValue;
}

protected function reconnectDb()
{
unset($this->db);
unset($this->connection);
$this->connectDb();
$this->reconnectCounter++;
}

protected function connectDb()
{
$this->db = new PDO($this->dsn, $this->username, $this->passwd,
$this->connection = new PDO($this->dsn, $this->username, $this->passwd,
$this->options);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
}

/**
Expand Down Expand Up @@ -173,24 +175,21 @@ public function setMaxReconnection($max)
* @param array $parameters
* @param bool $autoconnect
*/
public function setConnectionParameters($parameters, $autoconnect = false)
public function setConnectionParameters($parameters)
{
foreach ($parameters as $key => $param) {
if (property_exists($this, $key)) {
$this->$key = $param;
}
}
if ($autoconnect) {
$this->connectDb();
}
}

public function setPDO(PDO $pdoObject)
{
if (!$this->connectionParametersAreSet()) {
throw new ConnectionParametersMissingException();
}
$this->db = $pdoObject;
$this->connection = $pdoObject;
}

protected function connectionParametersAreSet()
Expand All @@ -200,4 +199,5 @@ protected function connectionParametersAreSet()
}
return false;
}

}
13 changes: 2 additions & 11 deletions test/ReconnectingPDOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testConstruct()
$this->assertAttributeEquals('test', 'passwd', $rpdo);
$this->assertAttributeEquals([], 'options', $rpdo);
$this->assertAttributeEquals(3, 'maxReconnection', $rpdo);
$this->assertAttributeInstanceOf(\PDO::class, 'db', $rpdo);
$this->assertAttributeInstanceOf(\PDO::class, 'connection', $rpdo);
}

public function testSetters()
Expand Down Expand Up @@ -73,15 +73,6 @@ public function testSetters()
}
$this->assertNull($exception);

unset($rpdo);
$rpdo = new ReconnectingPDO();
$rpdo->setConnectionParameters([
'dsn' => $dsn,
'passwd' => $passwd,
'username' => $username
], true);

$this->assertAttributeInstanceOf(\PDO::class, 'db', $rpdo);
}

public function testReconnection()
Expand All @@ -100,7 +91,7 @@ public function testReconnection()
$rpdo = new ReconnectingPDO($dsn, $username, $passwd);
$rpdo->setPDO($mockPDO);

$this->assertAttributeEquals($mockPDO, 'db', $rpdo);
$this->assertAttributeEquals($mockPDO, 'connection', $rpdo);

$exception = null;
try {
Expand Down

0 comments on commit 42963b4

Please sign in to comment.