SymfonyFilesystem::read()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\SymfonyFilesystemBridge\Infrastructure\Domain\Model;
14
15
use BenGorFile\File\Domain\Model\FileName;
16
use BenGorFile\File\Domain\Model\Filesystem;
17
use Symfony\Component\Filesystem\Filesystem as Symfony;
18
19
/**
20
 * Symfony implementation of filesystem domain class.
21
 *
22
 * @author Beñat Espiña <[email protected]>
23
 * @author Gorka Laucirica <[email protected]>
24
 */
25
class SymfonyFilesystem implements Filesystem
26
{
27
    /**
28
     * The Symfony filesystem.
29
     *
30
     * @var Symfony
31
     */
32
    private $filesystem;
33
34
    /**
35
     * The path.
36
     *
37
     * @var string
38
     */
39
    private $path;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param string  $aPath       The path
45
     * @param Symfony $aFilesystem The Symfony filesystem
46
     */
47
    public function __construct($aPath, Symfony $aFilesystem)
48
    {
49
        $this->filesystem = $aFilesystem;
50
        $this->path = rtrim($aPath, '/') . '/';
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function delete(FileName $aName)
57
    {
58
        $this->filesystem->remove($this->path . $aName->filename());
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function has(FileName $aName)
65
    {
66
        return $this->filesystem->exists($this->path . $aName->filename());
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function overwrite(FileName $aName, $aContent)
73
    {
74
        $this->filesystem->dumpFile($this->path . $aName->filename(), $aContent);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function read(FileName $aName)
81
    {
82
        return file_get_contents($this->path . $aName->filename());
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function rename(FileName $anOldName, FileName $aNewName)
89
    {
90
        $this->filesystem->rename($this->path . $anOldName->filename(), $this->path . $aNewName->filename());
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function write(FileName $aName, $aContent)
97
    {
98
        if ($this->has($aName)) {
99
            throw new \Exception(
100
                sprintf(
101
                    'Already exists a file with the same name that %s. ' .
102
                    'Maybe, you want to use "overwrite" method instead.',
103
                    $aName->filename()
104
                )
105
            );
106
        }
107
        $this->filesystem->dumpFile($this->path . $aName->filename(), $aContent);
108
    }
109
}
110