Symfony's documentation for service factories explains how to allow the service container to instantiate services using factories, but does not explain what the recommended best practice is when creation fails, i.e. when the service is not available for whatever reason.
For example; imagine a Redis memory caching service. Inside the factory class, you have a method that will instantiate and return a Redis client object;
public function createRedisClient() {
$redis = new \Redis();
$connectionResult = $redis->connect($host, $port);
return $redis;
}
If the Redis server is temporarily not available, and I would like to be able to gracefully fall back to another solution for storing whatever data we have, should the factory return null
, throw an exception of some specific kind, or simply not care about availability?