InMemoryFilesystem::read()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 3
nop 1
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\Domain\Model;
14
15
use BenGorFile\File\Domain\Model\FileName;
16
use BenGorFile\File\Domain\Model\Filesystem;
17
18
/**
19
 * In memory implementation of filesystem domain class.
20
 *
21
 * @author Beñat Espiña <[email protected]>
22
 */
23
class InMemoryFilesystem implements Filesystem
24
{
25
    /**
26
     * The file collection.
27
     *
28
     * @var array
29
     */
30
    private $files;
31
32
    /**
33
     * Constructor.
34
     */
35
    public function __construct()
36
    {
37
        $this->files = [];
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function delete(FileName $aName)
44
    {
45
        foreach ($this->files as $key => $file) {
46
            if ($file['filename'] === $aName->filename()) {
47
                unset($this->files[$key]);
48
                break;
49
            }
50
        }
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function has(FileName $aName)
57
    {
58
        foreach ($this->files as $file) {
59
            if ($file['filename'] === $aName->filename()) {
60
                return true;
61
            }
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 View Code Duplication
    public function overwrite(FileName $aName, $aContent)
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...
71
    {
72
        foreach ($this->files as $key => $file) {
73
            if ($file['filename'] === $aName->filename()) {
74
                $this->files[$key]['content'] = $aContent;
75
            }
76
        }
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function read(FileName $aName)
83
    {
84
        foreach ($this->files as $key => $file) {
85
            if ($file['filename'] === $aName->filename()) {
86
                return $this->files[$key];
87
            }
88
        }
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function rename(FileName $anOldName, FileName $aNewName)
95
    {
96
        foreach ($this->files as $key => $file) {
97
            if ($file['filename'] === $anOldName->filename()) {
98
                $this->files[$key]['filename'] = $aNewName->filename();
99
                break;
100
            }
101
        }
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 View Code Duplication
    public function write(FileName $aName, $aContent)
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...
108
    {
109
        foreach ($this->files as $key => $file) {
110
            if ($file['filename'] === $aName->filename()) {
111
                return;
112
            }
113
        }
114
        $this->files[] = ['filename' => $aName->filename(), 'content' => $aContent];
115
    }
116
}
117