Completed
Push — master ( 36152a...1c7656 )
by Beñat
02:48
created

InMemoryFileRepository::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
88
    {
89
        $this->files[$aFile->id()->id()] = $aFile;
90
91
        if ($this->eventBus instanceof FileEventBus) {
92
            $this->handle($aFile->events());
0 ignored issues
show
Bug introduced by
The method handle() does not seem to exist on object<BenGorFile\File\I...InMemoryFileRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
        }
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 View Code Duplication
    public function remove(File $aFile)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
100
    {
101
        unset($this->files[$aFile->id()->id()]);
102
103
        if ($this->eventBus instanceof FileEventBus) {
104
            $this->handle($aFile->events());
0 ignored issues
show
Bug introduced by
The method handle() does not seem to exist on object<BenGorFile\File\I...InMemoryFileRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
        }
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function size()
112
    {
113
        return count($this->files);
114
    }
115
}
116