Thread   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 89
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 13 3
A setPid() 0 5 1
A setCallback() 0 5 1
A getPid() 0 3 1
A setProcessName() 0 5 1
A getProcessName() 0 3 1
A getCallback() 0 3 1
1
<?php
2
3
namespace Wonderland\Thread;
4
5
use Wonderland\Thread\Exception\ThreadException;
6
7
class Thread
8
{
9
	const EXIT_STATUS_SUCCESS = 0;
10
	const EXIT_STATUS_ERROR = 1;
11
12
	/** @var int $pid */
13
	private $pid;
14
15
	/** @var string $processName */
16
	private $processName;
17
18
	/** @var callable $callback */
19
	private $callback;
20
21
	/**
22
	 * @return int|null
23
	 */
24 2
	public function getPid(): ?int
25
	{
26 2
		return $this->pid;
27
	}
28
29
	/**
30
	 * @param int $pid
31
	 * @return Thread
32
	 */
33 7
	public function setPid(int $pid): self
34
	{
35 7
		$this->pid = $pid;
36
37 7
		return $this;
38
	}
39
40
	/**
41
	 * @return string
42
	 */
43 2
	public function getProcessName(): ?string
44
	{
45 2
		return $this->processName;
46
	}
47
48
	/**
49
	 * @param string $processName
50
	 * @return Thread
51
	 */
52 8
	public function setProcessName(string $processName): self
53
	{
54 8
		$this->processName = $processName;
55
56 8
		return $this;
57
	}
58
59
	/**
60
	 * @return callable
61
	 */
62 2
	public function getCallback(): ?callable
63
	{
64 2
		return $this->callback;
65
	}
66
67
	/**
68
	 * @param callable $callback
69
	 * @return Thread
70
	 */
71 8
	public function setCallback(callable $callback): self
72
	{
73 8
		$this->callback = $callback;
74
75 8
		return $this;
76
	}
77
78
	/**
79
	 * @param string $processName
80
	 * @return int
81
	 * @throws ThreadException
82
	 */
83 3
	public function run(string $processName): int
84
	{
85 3
		if (null === $this->callback) {
86 1
			throw new ThreadException('No callback function defined for the thread');
87
		}
88
89 2
		$status = ($this->callback)($processName);
90
91 2
		if (null === $status) {
92 1
			throw new ThreadException('Error. You must return a process status in your callback function');
93
		}
94
95 1
		return $status;
96
	}
97
98
}
99