Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6218b6a
commit 87a5766
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace RichId\MaintenanceBundle\Infrastructure\Driver; | ||
|
||
use Lexik\Bundle\MaintenanceBundle\Drivers\AbstractDriver; | ||
use Lexik\Bundle\MaintenanceBundle\Drivers\DriverTtlInterface; | ||
use Symfony\Component\Cache\Traits\RedisTrait; | ||
|
||
class RedisDriver extends AbstractDriver implements DriverTtlInterface | ||
{ | ||
use RedisTrait; | ||
|
||
const VALUE_TO_STORE = "maintenance"; | ||
|
||
/** @param array<string, mixed> $options */ | ||
public function __construct(array $options = array()) | ||
{ | ||
parent::__construct($options); | ||
|
||
if (!isset($options['key_name'])) { | ||
throw new \InvalidArgumentException('$options[\'key_name\'] must be defined if Driver Redis configuration is used'); | ||
} | ||
|
||
if (!isset($options['redis_dsn'])) { | ||
throw new \InvalidArgumentException('$options[\'redis_dsn\'] must be defined if Driver Redis configuration is used'); | ||
} | ||
|
||
if (isset($options['ttl']) && !is_int($options['ttl'])) { | ||
throw new \InvalidArgumentException('$options[\'ttl\'] should be an integer if Driver Redis configuration is used'); | ||
} | ||
|
||
$this->redis = self::createConnection($options['redis_dsn']); | ||
} | ||
|
||
protected function createLock() | ||
{ | ||
if (!isset($this->options['ttl']) || $this->options['ttl'] <= 0) { | ||
return $this->redis->set($this->options['key_name'], self::VALUE_TO_STORE); | ||
} | ||
|
||
return $this->redis->setex($this->options['key_name'], $this->options['ttl'], self::VALUE_TO_STORE); | ||
} | ||
|
||
protected function createUnlock() | ||
{ | ||
return $this->redis->del($this->options['key_name']) > 0; | ||
} | ||
|
||
public function isExists() | ||
{ | ||
return $this->redis->exists($this->options['key_name']) > 0; | ||
} | ||
|
||
public function getMessageLock($resultTest) | ||
{ | ||
$key = $resultTest ? 'lexik_maintenance.success_lock_redis' : 'lexik_maintenance.not_success_lock'; | ||
|
||
return $this->translator->trans($key, array(), 'maintenance'); | ||
} | ||
|
||
public function getMessageUnlock($resultTest) | ||
{ | ||
$key = $resultTest ? 'lexik_maintenance.success_unlock' : 'lexik_maintenance.not_success_unlock'; | ||
|
||
return $this->translator->trans($key, array(), 'maintenance'); | ||
} | ||
|
||
public function setTtl($value): void | ||
{ | ||
$this->options['ttl'] = $value; | ||
} | ||
|
||
public function getTtl() | ||
{ | ||
return $this->options['ttl']; | ||
} | ||
|
||
public function hasTtl() | ||
{ | ||
return isset($this->options['ttl']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters