Completed
Pull Request — master (#6)
by Daniel
02:39
created

Process::getCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Jellyfish\ProcessSymfony;
4
5
use Jellyfish\Process\Exception\RuntimeException;
6
use Jellyfish\Process\ProcessInterface;
7
use Symfony\Component\Process\Process as SymfonyProcess;
8
9
class Process implements ProcessInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $id;
15
16
    /**
17
     * @var array
18
     */
19
    protected $command;
20
21
    /**
22
     * @var string
23
     */
24
    protected $pathToLockFile;
25
26
    /**
27
     * @var \Symfony\Component\Process\Process
28
     */
29
    protected $process;
30
31
    /**
32
     * @param array $command
33
     * @param string $tempDir
34
     */
35
    public function __construct(array $command, string $tempDir)
36
    {
37
        $this->id = \sha1(implode(' ', $command));
38
        $this->command = $command;
39
        $this->pathToLockFile = rtrim($tempDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $this->id;
40
41
        $preparedCommand = array_merge($command, [';', 'rm', $this->pathToLockFile]);
42
43
        $this->process = new SymfonyProcess($preparedCommand);
44
    }
45
46
    /**
47
     * @return void
48
     */
49
    public function start(): void
50
    {
51
        if ($this->isLocked()) {
52
            throw new RuntimeException('Process is locked.');
53
        }
54
55
        $this->lock();
56
        $this->process->start();
57
    }
58
59
    /**
60
     * @return void
61
     */
62
    protected function lock(): void
63
    {
64
        touch($this->pathToLockFile);
65
        file_put_contents($this->pathToLockFile, implode(' ', $this->command));
66
    }
67
68
    /**
69
     * @return bool
70
     */
71
    public function isLocked(): bool
72
    {
73
        return file_exists($this->pathToLockFile);
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getCommand(): array
80
    {
81
        return $this->command;
82
    }
83
}
84