Skip to content

Commit e97ee76

Browse files
authored
Support for DSN select database and take care of errors while connecting (#43)
* Support for DSN select database and take care of errors while connecting. This commit is related to php-cache/issues#62 * Applied fixes from StyleCI (#44)
1 parent c1aa43a commit e97ee76

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

Changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
44

55
## UNRELEASED
66

7+
### Added
8+
9+
* `ConnectException` that is thrown when you fail to connect to Redis
10+
11+
### Fixed
12+
13+
* If a DSN is provided to redis we make sure to select a database for you.
14+
715
## 0.3.4
816

917
No changelog before this version

src/Exception/ConfigurationException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Cache\AdapterBundle\Exception;
1313

1414
/**
15+
* An exception thrown when you have invalid configuration.
16+
*
1517
* @author Tobias Nyholm <[email protected]>
1618
*/
1719
class ConfigurationException extends \Exception

src/Exception/ConnectException.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\AdapterBundle\Exception;
13+
14+
/**
15+
* If you can connect to the cache storage you will get this exception thrown at you.
16+
*
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class ConnectException extends \RuntimeException
20+
{
21+
}

src/Factory/RedisFactory.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Cache\AdapterBundle\Factory;
1313

1414
use Cache\Adapter\Redis\RedisCachePool;
15+
use Cache\AdapterBundle\Exception\ConnectException;
1516
use Symfony\Component\OptionsResolver\OptionsResolver;
1617

1718
/**
@@ -33,13 +34,25 @@ public function getAdapter(array $config)
3334

3435
$dsn = $this->getDsn();
3536
if (empty($dsn)) {
36-
$client->connect($config['host'], $config['port']);
37+
if (false === $client->connect($config['host'], $config['port'])) {
38+
throw new ConnectException(sprintf('Could not connect to Redis database on "%s:%s".', $config['host'], $config['port']));
39+
}
3740
} else {
3841
if (!empty($dsn->getPassword())) {
39-
$client->auth($dsn->getPassword());
42+
if (false === $client->auth($dsn->getPassword())) {
43+
throw new ConnectException('Could not connect authenticate connection to Redis database.');
44+
}
45+
}
46+
47+
if (false === $client->connect($dsn->getFirstHost(), $dsn->getFirstPort())) {
48+
throw new ConnectException(sprintf('Could not connect to Redis database on "%s:%s".', $dsn->getFirstHost(), $dsn->getFirstPort()));
4049
}
4150

42-
$client->connect($dsn->getFirstHost(), $dsn->getFirstPort());
51+
if ($dsn->getDatabase() !== null) {
52+
if (false === $client->select($dsn->getDatabase())) {
53+
throw new ConnectException(sprintf('Could not select Redis database with index "%s".', $dsn->getDatabase()));
54+
}
55+
}
4356
}
4457

4558
return new RedisCachePool($client);

0 commit comments

Comments
 (0)