1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Inotify; |
6
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
8
|
|
|
use JsonSerializable; |
9
|
|
|
|
10
|
|
|
class InotifyEvent implements Arrayable, JsonSerializable |
11
|
|
|
{ |
12
|
|
|
private int $id; |
13
|
|
|
private InotifyEventCodeEnum $inotifyEventCodeEnum; |
14
|
|
|
private int $uniqueId; |
15
|
|
|
private string $fileName; |
16
|
|
|
private WatchedResource $watchedResource; |
17
|
|
|
private int $timestamp; |
18
|
|
|
|
19
|
2 |
|
public function __construct( |
20
|
|
|
int $descriptor, |
21
|
|
|
InotifyEventCodeEnum $inotifyEventCodeEnum, |
22
|
|
|
int $uniqueId, |
23
|
|
|
string $fileName, |
24
|
|
|
WatchedResource $watchedResource, |
25
|
|
|
int $timestamp |
26
|
|
|
) { |
27
|
2 |
|
$this->id = $descriptor; |
28
|
2 |
|
$this->inotifyEventCodeEnum = $inotifyEventCodeEnum; |
29
|
2 |
|
$this->uniqueId = $uniqueId; |
30
|
2 |
|
$this->fileName = $fileName; |
31
|
2 |
|
$this->watchedResource = $watchedResource; |
32
|
2 |
|
$this->timestamp = $timestamp; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function __toString(): string |
36
|
|
|
{ |
37
|
|
|
return (string)print_r($this->toArray(), true); |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
public function toArray(): array |
41
|
|
|
{ |
42
|
2 |
|
return [ |
43
|
2 |
|
'id' => $this->getId(), |
44
|
2 |
|
'eventCode' => $this->getInotifyEventCode(), |
45
|
2 |
|
'eventDescription' => $this->getInotifyEventCodeDescription(), |
46
|
2 |
|
'uniqueId' => $this->getUniqueId(), |
47
|
2 |
|
'fileName' => $this->getFileName(), |
48
|
2 |
|
'pathName' => $this->getWatchedResource()->getPathname(), |
49
|
2 |
|
'customName' => $this->getWatchedResource()->getCustomName(), |
50
|
2 |
|
'pathWithFile' => $this->getPathWithFile(), |
51
|
2 |
|
'timestamp' => $this->getTimestamp() |
52
|
2 |
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
public function getId(): int |
56
|
|
|
{ |
57
|
2 |
|
return $this->id; |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
public function getInotifyEventCode(): int |
61
|
|
|
{ |
62
|
2 |
|
return $this->inotifyEventCodeEnum->getValue(); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
public function getInotifyEventCodeDescription(): string |
66
|
|
|
{ |
67
|
2 |
|
return InotifyEventCodeEnum::getCodeDescription($this->getInotifyEventCode()); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
public function getUniqueId(): int |
71
|
|
|
{ |
72
|
2 |
|
return $this->uniqueId; |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
public function getFileName(): string |
76
|
|
|
{ |
77
|
2 |
|
return $this->fileName; |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
public function getWatchedResource(): WatchedResource |
81
|
|
|
{ |
82
|
2 |
|
return $this->watchedResource; |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
public function getPathWithFile(): string |
86
|
|
|
{ |
87
|
2 |
|
$path = $this->watchedResource->getPathname(); |
88
|
2 |
|
if ('' === $this->getFileName()) { |
89
|
1 |
|
return $path; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
if ($this->getFileName()[0] === DIRECTORY_SEPARATOR) { |
93
|
|
|
return $path . DIRECTORY_SEPARATOR . $this->getFileName(); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return $path . $this->getFileName(); |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
public function getTimestamp(): int |
100
|
|
|
{ |
101
|
2 |
|
return $this->timestamp; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function jsonSerialize(): array |
105
|
|
|
{ |
106
|
|
|
return $this->toArray(); |
107
|
|
|
} |
108
|
|
|
} |