Completed
Push — master ( e8851b...557727 )
by Beñat
03:22
created

InMemoryFileRepository   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 15.24 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 3
dl 16
loc 105
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fileOfId() 0 6 2
A query() 0 4 1
A singleResultQuery() 0 4 1
A count() 0 4 1
A fileOfName() 0 8 3
A all() 0 4 1
A persist() 8 8 2
A remove() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 query($aSpecification)
68
    {
69
        throw new \LogicException('This method is not implemented yet, maybe you can propose a PR :)');
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function singleResultQuery($aSpecification)
76
    {
77
        throw new \LogicException('This method is not implemented yet, maybe you can propose a PR :)');
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function count($aSpecification)
84
    {
85
        return count($this->files);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function fileOfName(FileName $aName)
92
    {
93
        foreach ($this->files as $file) {
94
            if (true === $file->name()->equals($aName)) {
95
                return $file;
96
            }
97
        }
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function all()
104
    {
105
        return $this->files;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 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...
112
    {
113
        $this->files[$aFile->id()->id()] = $aFile;
114
115
        if ($this->eventBus instanceof FileEventBus) {
116
            $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...
117
        }
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 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...
124
    {
125
        unset($this->files[$aFile->id()->id()]);
126
127
        if ($this->eventBus instanceof FileEventBus) {
128
            $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...
129
        }
130
    }
131
}
132