1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the BenGorFile package. |
5
|
|
|
* |
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace BenGorFile\File\Infrastructure\Persistence\InMemory; |
14
|
|
|
|
15
|
|
|
use BenGorFile\File\Domain\Model\File; |
16
|
|
|
use BenGorFile\File\Domain\Model\FileId; |
17
|
|
|
use BenGorFile\File\Domain\Model\FileName; |
18
|
|
|
use BenGorFile\File\Domain\Model\FileRepository; |
19
|
|
|
use BenGorFile\File\Infrastructure\Domain\Model\FileEventBus; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* In memory file repository class. |
23
|
|
|
* |
24
|
|
|
* @author Beñat Espiña <[email protected]> |
25
|
|
|
* @author Gorka Laucirica <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class InMemoryFileRepository implements FileRepository |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* File collection. |
31
|
|
|
* |
32
|
|
|
* @var File[] |
33
|
|
|
*/ |
34
|
|
|
private $files; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The file event bus, it can be null. |
38
|
|
|
* |
39
|
|
|
* @var FileEventBus|null |
40
|
|
|
*/ |
41
|
|
|
private $eventBus; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* |
46
|
|
|
* @param FileEventBus|null $anEventBus The file event bus, it can be null |
47
|
|
|
*/ |
48
|
|
|
public function __construct(FileEventBus $anEventBus = null) |
49
|
|
|
{ |
50
|
|
|
$this->files = []; |
51
|
|
|
$this->eventBus = $anEventBus; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function fileOfId(FileId $anId) |
58
|
|
|
{ |
59
|
|
|
if (isset($this->files[$anId->id()])) { |
60
|
|
|
return $this->files[$anId->id()]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function fileOfName(FileName $aName) |
68
|
|
|
{ |
69
|
|
|
foreach ($this->files as $file) { |
70
|
|
|
if (true === $file->name()->equals($aName)) { |
71
|
|
|
return $file; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function all() |
80
|
|
|
{ |
81
|
|
|
return $this->files; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function persist(File $aFile) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$this->files[$aFile->id()->id()] = $aFile; |
90
|
|
|
|
91
|
|
|
if ($this->eventBus instanceof FileEventBus) { |
92
|
|
|
$this->handle($aFile->events()); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function remove(File $aFile) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
unset($this->files[$aFile->id()->id()]); |
102
|
|
|
|
103
|
|
|
if ($this->eventBus instanceof FileEventBus) { |
104
|
|
|
$this->handle($aFile->events()); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function size() |
112
|
|
|
{ |
113
|
|
|
return count($this->files); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.