PosixProcessor::checkProcessing()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 9
cp 0
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace TreeHouse\IoBundle\Import\Processor;
4
5
use TreeHouse\IoBundle\Entity\ImportPart;
6
use TreeHouse\IoBundle\Import\Exception\RunningPartException;
7
8
class PosixProcessor implements ProcessorInterface
9
{
10
    /**
11
     * @inheritdoc
12
     */
13
    public function isRunning(ImportPart $part)
14
    {
15
        if (null === $pid = $part->getProcess()) {
16
            return false;
17
        }
18
19
        if (intval($pid) < 1) {
20
            throw new \RuntimeException(
21
                sprintf('Import part does not have a valid pid: %s', json_encode($pid))
22
            );
23
        }
24
25
        // kill signal 0: check whether a process is running.
26
        // see http://www.php.net/manual/en/function.posix-kill.php#82560
27
        return posix_kill($pid, 0);
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33 4
    public function markProcessing(ImportPart $part)
34
    {
35 4
        $part->setProcess(posix_getpid());
36 4
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function checkProcessing(ImportPart $part)
42
    {
43
        if (!$this->isRunning($part)) {
44
            return;
45
        }
46
47
        throw new RunningPartException(
48
            sprintf(
49
                'Part %d of import %d is already running by process %d',
50
                $part->getPosition(),
51
                $part->getImport()->getId(),
52
                $part->getProcess()
53
            )
54
        );
55
    }
56
}
57