Completed
Push — master ( cd6ea2...a40d21 )
by Maxim
02:05
created

CommandExecutionLock::lockCommand()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 15
nc 3
nop 2
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 IConsole $console
104
     * @param ICommand $command
105
     */
106
    public function lockCommand(IConsole $console, ICommand $command) {
107
        if ($command->isParallel() && $console->getAllowParallel()) {
108
            return;
109
        }
110
111
        if ($this->isInLockFile($command->getName())) {
112
            throw new CommandIsAlreadyRunningException(s(
113
                'Command "%s" is already being executed. ' .
114
                'Parallel execution for this command has been forbidden. ' .
115
                'This is the corresponding lock file "%s".',
116
                $command->getName(),
117
                $this->getLockFile()
118
            ));
119
        }
120
121
        $this->addToLockFile($command->getName());
122
123
        $self = $this;
124
        register_shutdown_function(function() use ($self, $command) {
125
            $self->removeFromLockFile($command->getName());
126
        }, [$this, $command]);
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    protected function getDefaultLockFile() {
133
        return path(sys_get_temp_dir(), md5(__DIR__) . '_console_lock');
134
    }
135
}
136