1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace PersonalGalaxy\Files\Entity; |
5
|
|
|
|
6
|
|
|
use PersonalGalaxy\Files\{ |
7
|
|
|
Entity\Folder\Identity, |
8
|
|
|
Entity\Folder\Name, |
9
|
|
|
Event\FolderWasAdded, |
10
|
|
|
Event\FolderWasTrashed, |
11
|
|
|
Event\FolderWasRestored, |
12
|
|
|
Event\FolderWasRemoved, |
13
|
|
|
Event\FolderWasRenamed, |
14
|
|
|
Event\FolderWasMovedToADifferentParent, |
15
|
|
|
Exception\LogicException, |
16
|
|
|
}; |
17
|
|
|
use Innmind\EventBus\{ |
18
|
|
|
ContainsRecordedEventsInterface, |
19
|
|
|
EventRecorder, |
20
|
|
|
}; |
21
|
|
|
|
22
|
|
|
final class Folder implements ContainsRecordedEventsInterface |
23
|
|
|
{ |
24
|
|
|
use EventRecorder; |
25
|
|
|
|
26
|
|
|
private $identity; |
27
|
|
|
private $name; |
28
|
|
|
private $parent; |
29
|
|
|
private $trashed = false; |
30
|
|
|
|
31
|
12 |
|
private function __construct( |
32
|
|
|
Identity $identity, |
33
|
|
|
Name $name, |
34
|
|
|
Identity $parent |
35
|
|
|
) { |
36
|
12 |
|
$this->identity = $identity; |
37
|
12 |
|
$this->name = $name; |
38
|
12 |
|
$this->parent = $parent; |
39
|
12 |
|
} |
40
|
|
|
|
41
|
12 |
|
public static function add( |
42
|
|
|
Identity $identity, |
43
|
|
|
Name $name, |
44
|
|
|
Identity $parent |
45
|
|
|
): self { |
46
|
12 |
|
$self = new self($identity, $name, $parent); |
47
|
12 |
|
$self->record(new FolderWasAdded($identity, $name, $parent)); |
48
|
|
|
|
49
|
12 |
|
return $self; |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
public function identity(): Identity |
53
|
|
|
{ |
54
|
4 |
|
return $this->identity; |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
public function name(): Name |
58
|
|
|
{ |
59
|
4 |
|
return $this->name; |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
public function parent(): Identity |
63
|
|
|
{ |
64
|
4 |
|
return $this->parent; |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
public function trashed(): bool |
68
|
|
|
{ |
69
|
3 |
|
return $this->trashed; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
public function rename(Name $name): self |
73
|
|
|
{ |
74
|
2 |
|
if ($name->equals($this->name)) { |
75
|
1 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
$this->name = $name; |
79
|
2 |
|
$this->record(new FolderWasRenamed($this->identity, $name)); |
80
|
|
|
|
81
|
2 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
public function moveTo(Identity $parent): self |
85
|
|
|
{ |
86
|
2 |
|
if ($parent->equals($this->parent)) { |
87
|
1 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
$this->parent = $parent; |
91
|
2 |
|
$this->record(new FolderWasMovedToADifferentParent($this->identity, $parent)); |
92
|
|
|
|
93
|
2 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
6 |
|
public function trash(): self |
97
|
|
|
{ |
98
|
6 |
|
if ($this->trashed) { |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
6 |
|
$this->trashed = true; |
103
|
6 |
|
$this->record(new FolderWasTrashed($this->identity)); |
104
|
|
|
|
105
|
6 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
public function restore(): self |
109
|
|
|
{ |
110
|
2 |
|
if (!$this->trashed) { |
111
|
1 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
$this->trashed = false; |
115
|
2 |
|
$this->record(new FolderWasRestored($this->identity)); |
116
|
|
|
|
117
|
2 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Last method that can be called, here only to record the event |
122
|
|
|
*/ |
123
|
3 |
|
public function remove(): void |
124
|
|
|
{ |
125
|
3 |
|
if (!$this->trashed) { |
126
|
1 |
|
throw new LogicException; |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
$this->record(new FolderWasRemoved($this->identity)); |
130
|
3 |
|
} |
131
|
|
|
} |
132
|
|
|
|