1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace PersonalGalaxy\Files\Entity; |
5
|
|
|
|
6
|
|
|
use PersonalGalaxy\Files\{ |
7
|
|
|
Entity\File\Identity, |
8
|
|
|
Entity\File\Name, |
9
|
|
|
Entity\Folder\Identity as Folder, |
|
|
|
|
10
|
|
|
Event\FileWasAdded, |
11
|
|
|
Event\FileWasTrashed, |
12
|
|
|
Event\FileWasRestored, |
13
|
|
|
Event\FileWasRemoved, |
14
|
|
|
Event\FileWasRenamed, |
15
|
|
|
Event\FileWasMovedToADifferentFolder, |
16
|
|
|
Exception\LogicException, |
17
|
|
|
}; |
18
|
|
|
use Innmind\EventBus\{ |
19
|
|
|
ContainsRecordedEventsInterface, |
20
|
|
|
EventRecorder, |
21
|
|
|
}; |
22
|
|
|
use Innmind\Filesystem\MediaType; |
23
|
|
|
|
24
|
|
|
final class File implements ContainsRecordedEventsInterface |
25
|
|
|
{ |
26
|
|
|
use EventRecorder; |
27
|
|
|
|
28
|
|
|
private $identity; |
29
|
|
|
private $name; |
30
|
|
|
private $folder; |
31
|
|
|
private $mediaType; |
32
|
|
|
private $trashed = false; |
33
|
|
|
|
34
|
14 |
|
private function __construct( |
35
|
|
|
Identity $identity, |
36
|
|
|
Name $name, |
37
|
|
|
Folder $folder, |
38
|
|
|
MediaType $mediaType |
39
|
|
|
) { |
40
|
14 |
|
$this->identity = $identity; |
41
|
14 |
|
$this->name = $name; |
42
|
14 |
|
$this->folder = $folder; |
43
|
14 |
|
$this->mediaType = $mediaType; |
44
|
14 |
|
} |
45
|
|
|
|
46
|
14 |
|
public static function add( |
47
|
|
|
Identity $identity, |
48
|
|
|
Name $name, |
49
|
|
|
Folder $folder, |
50
|
|
|
MediaType $mediaType |
51
|
|
|
): self { |
52
|
14 |
|
$self = new self($identity, $name, $folder, $mediaType); |
53
|
14 |
|
$self->record(new FileWasAdded($identity, $name, $folder, $mediaType)); |
54
|
|
|
|
55
|
14 |
|
return $self; |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
public function identity(): Identity |
59
|
|
|
{ |
60
|
5 |
|
return $this->identity; |
61
|
|
|
} |
62
|
|
|
|
63
|
7 |
|
public function name(): Name |
64
|
|
|
{ |
65
|
7 |
|
return $this->name; |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
public function folder(): Folder |
69
|
|
|
{ |
70
|
4 |
|
return $this->folder; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function mediaType(): MediaType |
74
|
|
|
{ |
75
|
2 |
|
return $this->mediaType; |
76
|
|
|
} |
77
|
|
|
|
78
|
4 |
|
public function trashed(): bool |
79
|
|
|
{ |
80
|
4 |
|
return $this->trashed; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function rename(Name $name): self |
84
|
|
|
{ |
85
|
2 |
|
if ($name->equals($this->name)) { |
86
|
1 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
$this->name = $name; |
90
|
2 |
|
$this->record(new FileWasRenamed($this->identity, $name)); |
91
|
|
|
|
92
|
2 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
public function moveTo(Folder $folder): self |
96
|
|
|
{ |
97
|
2 |
|
if ($folder->equals($this->folder)) { |
98
|
1 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
$this->folder = $folder; |
102
|
2 |
|
$this->record(new FileWasMovedToADifferentFolder($this->identity, $folder)); |
103
|
|
|
|
104
|
2 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
8 |
|
public function trash(): self |
108
|
|
|
{ |
109
|
8 |
|
if ($this->trashed) { |
110
|
1 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
8 |
|
$this->trashed = true; |
114
|
8 |
|
$this->record(new FileWasTrashed($this->identity)); |
115
|
|
|
|
116
|
8 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
public function restore(): self |
120
|
|
|
{ |
121
|
2 |
|
if (!$this->trashed) { |
122
|
1 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
$this->trashed = false; |
126
|
2 |
|
$this->record(new FileWasRestored($this->identity)); |
127
|
|
|
|
128
|
2 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Last method that can be called, here only to record the event |
133
|
|
|
*/ |
134
|
4 |
|
public function remove(): void |
135
|
|
|
{ |
136
|
4 |
|
if (!$this->trashed) { |
137
|
1 |
|
throw new LogicException; |
138
|
|
|
} |
139
|
|
|
|
140
|
4 |
|
$this->record(new FileWasRemoved($this->identity)); |
141
|
4 |
|
} |
142
|
|
|
} |
143
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: