Passed
Push — master ( 9a5374...9213a8 )
by Kevin
02:12
created

SingleServerExtension::aquireScheduleLock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule\Extension;
4
5
use Symfony\Component\Lock\LockFactory;
6
use Zenstruck\ScheduleBundle\Schedule\Extension;
7
8
/**
9
 * @author Kevin Bond <[email protected]>
10
 */
11
final class SingleServerExtension implements Extension, HasMissingHandlerMessage
12
{
13
    public const DEFAULT_TTL = 3600;
14
15
    private $ttl;
16
    private $lock;
17
18
    /**
19
     * @param int $ttl Maximum expected lock duration in seconds
20
     */
21 9
    public function __construct(int $ttl = self::DEFAULT_TTL)
22
    {
23 9
        $this->ttl = $ttl;
24 9
        $this->lock = new Lock(self::class);
25 9
    }
26
27 1
    public function __toString(): string
28
    {
29 1
        return 'Run on single server';
30
    }
31
32 4
    public function aquireLock(LockFactory $lockFactory, string $mutex, int $timestamp): bool
33
    {
34 4
        $mutex .= \date('Hi', $timestamp);
35
36 4
        return $this->lock->aquire($lockFactory, $mutex, $this->ttl);
37
    }
38
39 1
    public function getMissingHandlerMessage(): string
40
    {
41 1
        return 'To use "onSingleServer" you must configure a lock factory (config path: "zenstruck_schedule.single_server_handler").';
42
    }
43
}
44