Skip to content

Commit 9a7c650

Browse files
committed
PSR-12 changes, doc-blocks and small refactorings
1 parent 86c6104 commit 9a7c650

10 files changed

+271
-92
lines changed

src/Mutex.php

+63-29
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,70 @@
22

33
namespace Illuminated\Console;
44

5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Cache;
7+
use Illuminate\Support\Facades\Redis as RedisFacade;
58
use NinjaMutex\Lock\FlockLock;
9+
use NinjaMutex\Lock\MemcachedLock;
610
use NinjaMutex\Lock\MySqlLock;
7-
use NinjaMutex\Mutex as Ninja;
8-
use Illuminate\Console\Command;
911
use NinjaMutex\Lock\PhpRedisLock;
10-
use NinjaMutex\Lock\MemcachedLock;
1112
use NinjaMutex\Lock\PredisRedisLock;
12-
use Illuminate\Support\Facades\Cache;
13-
use Illuminate\Support\Facades\Redis as RedisFacade;
13+
use NinjaMutex\Mutex as NinjaMutex;
1414

1515
/**
16-
* @mixin Ninja
16+
* @mixin \NinjaMutex\Mutex
1717
*/
1818
class Mutex
1919
{
20+
/**
21+
* The console command.
22+
*
23+
* @var \Illuminate\Console\Command
24+
*/
2025
private $command;
21-
private $strategy;
22-
private $ninja;
2326

27+
/**
28+
* The ninja mutex.
29+
*
30+
* @var \NinjaMutex\Mutex
31+
*/
32+
private $ninjaMutex;
33+
34+
/**
35+
* The ninja mutex lock.
36+
*
37+
* @var \NinjaMutex\Lock\LockAbstract
38+
*/
39+
private $ninjaMutexLock;
40+
41+
/**
42+
* Create a new instance of the mutex.
43+
*
44+
* @param \Illuminate\Console\Command|\Illuminated\Console\WithoutOverlapping $command
45+
* @return void
46+
*/
2447
public function __construct(Command $command)
2548
{
2649
$this->command = $command;
27-
$this->strategy = $this->getStrategy();
28-
$this->ninja = new Ninja($command->getMutexName(), $this->strategy);
50+
51+
$mutexName = $command->getMutexName();
52+
$this->ninjaMutexLock = $this->getNinjaMutexLock();
53+
$this->ninjaMutex = new NinjaMutex($mutexName, $this->ninjaMutexLock);
2954
}
3055

31-
public function getStrategy()
56+
/**
57+
* Get the ninja mutex lock.
58+
*
59+
* @return \NinjaMutex\Lock\LockAbstract
60+
*/
61+
public function getNinjaMutexLock()
3262
{
33-
if (!empty($this->strategy)) {
34-
return $this->strategy;
63+
if (!empty($this->ninjaMutexLock)) {
64+
return $this->ninjaMutexLock;
3565
}
3666

37-
switch ($this->command->getMutexStrategy()) {
67+
$strategy = $this->command->getMutexStrategy();
68+
switch ($strategy) {
3869
case 'mysql':
3970
return new MySqlLock(
4071
config('database.connections.mysql.username'),
@@ -55,27 +86,30 @@ public function getStrategy()
5586
}
5687
}
5788

89+
/**
90+
* Get the redis lock.
91+
*
92+
* @param string $client
93+
* @return \NinjaMutex\Lock\LockAbstract
94+
*/
5895
private function getRedisLock($client)
5996
{
60-
if ($client === 'phpredis') {
61-
return new PhpRedisLock($this->getPhpRedisClient());
62-
}
63-
64-
return new PredisRedisLock($this->getPredisClient());
65-
}
97+
$redis = RedisFacade::connection()->client();
6698

67-
public function getPhpRedisClient()
68-
{
69-
return RedisFacade::connection()->client();
70-
}
71-
72-
public function getPredisClient()
73-
{
74-
return RedisFacade::connection()->client();
99+
return $client === 'phpredis'
100+
? new PhpRedisLock($redis)
101+
: new PredisRedisLock($redis);
75102
}
76103

104+
/**
105+
* Forward method calls to ninja mutex.
106+
*
107+
* @param string $method
108+
* @param mixed $parameters
109+
* @return mixed
110+
*/
77111
public function __call($method, $parameters)
78112
{
79-
return call_user_func_array([$this->ninja, $method], $parameters);
113+
return call_user_func_array([$this->ninjaMutex, $method], $parameters);
80114
}
81115
}

src/WithoutOverlapping.php

+78-5
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,128 @@
77

88
trait WithoutOverlapping
99
{
10+
/**
11+
* Overwrite the console command initialization.
12+
*
13+
* @param \Symfony\Component\Console\Input\InputInterface $input
14+
* @param \Symfony\Component\Console\Output\OutputInterface $output
15+
* @return void
16+
*/
1017
protected function initialize(InputInterface $input, OutputInterface $output)
1118
{
1219
$this->initializeMutex();
1320

1421
parent::initialize($input, $output);
1522
}
1623

24+
/**
25+
* Initialize the mutex.
26+
*
27+
* @return void
28+
*/
1729
protected function initializeMutex()
1830
{
1931
$mutex = new Mutex($this);
20-
if (!$mutex->acquireLock($this->getMutexTimeout())) {
32+
33+
$timeout = $this->getMutexTimeout();
34+
if (!$mutex->acquireLock($timeout)) {
2135
throw new MutexRuntimeException('Command is running now!');
2236
}
2337

2438
register_shutdown_function([$this, 'releaseMutexLock'], $mutex);
2539
}
2640

41+
/**
42+
* Get the mutex strategy.
43+
*
44+
* Currently supported: "file", "mysql", "redis" and "memcached".
45+
*
46+
* @return string
47+
*/
2748
public function getMutexStrategy()
2849
{
29-
return property_exists($this, 'mutexStrategy') ? $this->mutexStrategy : 'file';
50+
return property_exists($this, 'mutexStrategy')
51+
? $this->mutexStrategy
52+
: 'file';
3053
}
3154

55+
/**
56+
* Set the mutex strategy.
57+
*
58+
* Currently supported: "file", "mysql", "redis" and "memcached".
59+
*
60+
* @param string $strategy
61+
* @return void
62+
*/
3263
public function setMutexStrategy($strategy)
3364
{
3465
$this->mutexStrategy = $strategy;
3566
}
3667

68+
/**
69+
* Get the mutex timeout in milliseconds.
70+
*
71+
* Possible values:
72+
* `0` - check without waiting;
73+
* `{milliseconds}` - check, and wait for a maximum of milliseconds specified;
74+
* `null` - wait, till running command finish its execution;
75+
*
76+
* @return int|null
77+
*/
3778
public function getMutexTimeout()
3879
{
39-
return property_exists($this, 'mutexTimeout') ? $this->mutexTimeout : 0;
80+
return property_exists($this, 'mutexTimeout')
81+
? $this->mutexTimeout
82+
: 0;
4083
}
4184

85+
/**
86+
* Set the mutex timeout in milliseconds.
87+
*
88+
* Possible values:
89+
* `0` - check without waiting;
90+
* `{milliseconds}` - check, and wait for a maximum of milliseconds specified;
91+
* `null` - wait, till running command finish its execution;
92+
*
93+
* @param int|null $timeout
94+
* @return void
95+
*/
4296
public function setMutexTimeout($timeout)
4397
{
4498
$this->mutexTimeout = $timeout;
4599
}
46100

101+
/**
102+
* Get the mutex name.
103+
*
104+
* @return string
105+
*/
47106
public function getMutexName()
48107
{
49108
$name = $this->getName();
50-
$arguments = json_encode($this->argument());
51-
return "icmutex-{$name}-" . md5($arguments);
109+
$argumentsHash = md5(json_encode($this->argument()));
110+
111+
return "icmutex-{$name}-{$argumentsHash}";
52112
}
53113

114+
/**
115+
* Get the mutex file storage path.
116+
*
117+
* @return string
118+
*/
54119
public function getMutexFileStorage()
55120
{
56121
return storage_path('app');
57122
}
58123

124+
/**
125+
* Release the mutex lock.
126+
*
127+
* Called automatically, because it's registered as a shutdown function.
128+
*
129+
* @param \Illuminated\Console\Mutex $mutex
130+
* @return void
131+
*/
59132
public function releaseMutexLock(Mutex $mutex)
60133
{
61134
$mutex->releaseLock();

0 commit comments

Comments
 (0)