Completed
Push — master ( aa2f53...cd6ea2 )
by Maxim
03:44
created

CommandExecutionLock::getLockFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Weew\Console;
4
5
use DateTime;
6
use Exception;
7
use Weew\Console\Exceptions\CommandIsAlreadyRunningException;
8
use Weew\ConsoleArguments\ICommand;
9
use Weew\JsonEncoder\JsonEncoder;
10
11
class CommandExecutionLock implements ICommandExecutionLock {
12
    /**
13
     * @var string
14
     */
15
    protected $lockFile;
16
17
    /**
18
     * CommandExecutionLock constructor.
19
     *
20
     * @param string $lockFile
21
     */
22
    public function __construct($lockFile = null) {
23
        if ($lockFile === null) {
24
            $lockFile = $this->getDefaultLockFile();
25
        }
26
27
        $this->setLockFile($lockFile);
28
    }
29
30
    /**
31
     * @return string
32
     */
33
    public function getLockFile() {
34
        return $this->lockFile;
35
    }
36
37
    /**
38
     * @param string $lockFile
39
     */
40
    public function setLockFile($lockFile) {
41
        $this->lockFile = $lockFile;
42
    }
43
44
    /**
45
     * @return array
46
     */
47
    public function readLockFile() {
48
        $lockFile = $this->getLockFile();
49
        $data = [];
50
51
        if (file_exists($lockFile)) {
52
            try {
53
                $encoder = new JsonEncoder();
54
                $data = $encoder->decode(file_read($lockFile));
55
56
                if ($data === null) {
57
                    $data = [];
58
                }
59
            } catch (Exception $ex) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
60
        }
61
62
        return $data;
63
    }
64
65
    /**
66
     * @param array $data
67
     */
68
    public function writeLockFile(array $data) {
69
        $lockFile = $this->getLockFile();
70
        $encoder = new JsonEncoder();
71
72
        file_write($lockFile, $encoder->encode($data));
73
    }
74
75
    /**
76
     * @param string $value
77
     *
78
     * @return bool
79
     */
80
    public function isInLockFile($value) {
81
        return array_has($this->readLockFile(), $value);
82
    }
83
84
    /**
85
     * @param string $value
86
     */
87
    public function addToLockFile($value) {
88
        $data = $this->readLockFile();
89
        $data[$value] = (new DateTime())->format(DateTime::ATOM);
90
        $this->writeLockFile($data);
91
    }
92
93
    /**
94
     * @param string $value
95
     */
96
    public function removeFromLockFile($value) {
97
        $data = $this->readLockFile();
98
        array_remove($data, $value);
99
        $this->writeLockFile($data);
100
    }
101
102
    /**
103
     * @param ICommand $command
104
     */
105
    public function lockCommand(ICommand $command) {
106
        if ($command->isParallel()) {
107
            return;
108
        }
109
110
        if ($this->isInLockFile($command->getName())) {
111
            throw new CommandIsAlreadyRunningException(s(
112
                'Command "%s" is already being executed. ' .
113
                'Parallel execution for this command has been forbidden. ' .
114
                'This is the corresponding lock file "%s".',
115
                $command->getName(),
116
                $this->getLockFile()
117
            ));
118
        }
119
120
        $this->addToLockFile($command->getName());
121
122
        $self = $this;
123
        register_shutdown_function(function() use ($self, $command) {
124
            $self->removeFromLockFile($command->getName());
125
        }, [$this, $command]);
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    protected function getDefaultLockFile() {
132
        return path(sys_get_temp_dir(), md5(__DIR__) . '_console_lock');
133
    }
134
}
135