PidLock   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 72
ccs 0
cts 54
cp 0
rs 10
wmc 17

7 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 3
A __destruct() 0 5 2
A getLockFileName() 0 3 1
A delete() 0 6 3
A __construct() 0 10 4
A setKey() 0 3 1
A has() 0 11 3
1
<?php
2
3
namespace kalanis\kw_locks\Methods;
4
5
6
use kalanis\kw_locks\Interfaces\IKLTranslations;
7
use kalanis\kw_locks\Interfaces\IPassedKey;
8
use kalanis\kw_locks\LockException;
9
use kalanis\kw_locks\Traits\TLang;
10
11
12
/**
13
 * Class PidLock
14
 * @package kalanis\kw_locks\Methods
15
 * @codeCoverageIgnore accessing *nix calls
16
 */
17
class PidLock implements IPassedKey
18
{
19
    use TLang;
20
21
    protected string $tempPath = '';
22
    protected string $specialKey = '';
23
24
    /**
25
     * @param string $tempPath
26
     * @param IKLTranslations|null $lang
27
     * @throws LockException
28
     */
29
    public function __construct(string $tempPath, ?IKLTranslations $lang = null)
30
    {
31
        $this->setKlLang($lang);
32
        if (\defined('PHP_OS_FAMILY') && in_array(PHP_OS_FAMILY, ['Windows', 'Unknown']) ) {
33
            throw new LockException($this->getKlLang()->iklCannotUseOS());
34
        }
35
        if (\DIRECTORY_SEPARATOR === '\\') {
36
            throw new LockException($this->getKlLang()->iklCannotUseOS());
37
        }
38
        $this->tempPath = $tempPath;
39
    }
40
41
    public function __destruct()
42
    {
43
        try {
44
            $this->delete();
45
        } catch (LockException $ex) {
46
            // do nothing instead of
47
            // register_shutdown_function([$this, 'delete']);
48
        }
49
    }
50
51
    public function setKey(string $key): void
52
    {
53
        $this->specialKey = $key;
54
    }
55
56
    public function has(): bool
57
    {
58
        if (file_exists($this->getLockFileName())) {
59
            $lockingPid = trim(strval(file_get_contents($this->getLockFileName())));
60
            $otherOnes = explode(PHP_EOL, trim(`ps -e | awk '{print $1}'`));
61
            if (in_array($lockingPid, $otherOnes)) {
62
                return true;
63
            }
64
            throw new LockException($this->getKlLang()->iklLockedByOther());
65
        }
66
        return false;
67
    }
68
69
    public function create(bool $force = false): bool
70
    {
71
        if (!$force && $this->has()) {
72
            return false;
73
        }
74
        $result = @file_put_contents($this->getLockFileName(), strval(getmypid()) . PHP_EOL);
75
        return (false !== $result);
76
    }
77
78
    public function delete(bool $force = false): bool
79
    {
80
        if (!$force && !$this->has()) {
81
            return true;
82
        }
83
        return @unlink($this->getLockFileName());
84
    }
85
86
    protected function getLockFileName(): string
87
    {
88
        return $this->tempPath . DIRECTORY_SEPARATOR . $this->specialKey . '.lock';
89
    }
90
}
91