chill-bundles/src/Bundle/ChillMainBundle/Redis/RedisConnectionFactory.php

73 lines
1.5 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
/**
* Chill is a software for social workers.
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Redis;
use RuntimeException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RedisConnectionFactory implements EventSubscriberInterface
{
protected $host;
protected $port;
/**
* @var Redis
*/
protected $redis;
protected $timeout;
public function __construct($parameters)
{
$this->host = $parameters['host'];
$this->port = $parameters['port'];
$this->timeout = $parameters['timeout'];
}
public function create()
{
$redis = $this->redis = new ChillRedis();
$result = $redis->connect($this->host, $this->port, $this->timeout);
if (false === $result) {
throw new RuntimeException('Could not connect to redis instance');
}
return $redis;
}
public static function getSubscribedEvents(): array
{
return [
'kernel.finish_request' => [
['onKernelFinishRequest'],
],
];
}
public function onKernelFinishRequest()
{
if (null !== $this->redis) {
$this->redis->close();
}
}
}