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

60 lines
1.3 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);
namespace Chill\MainBundle\Redis;
use RuntimeException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class RedisConnectionFactory implements EventSubscriberInterface
{
private string $host;
private int $port;
private ChillRedis $redis;
private int $timeout;
public function __construct($parameters)
{
$this->host = $parameters['host'];
$this->port = (int) $parameters['port'];
$this->timeout = (int) $parameters['timeout'];
$this->redis = new ChillRedis();
}
public function create()
{
$result = $this->redis->connect($this->host, $this->port, $this->timeout);
if (false === $result) {
throw new RuntimeException('Could not connect to redis instance');
}
return $this->redis;
}
public static function getSubscribedEvents(): array
{
return [
'kernel.finish_request' => [
['onKernelFinishRequest'],
],
];
}
public function onKernelFinishRequest()
{
$this->redis->close();
}
}