Completed
Pull Request — master (#322)
by Mathieu
03:43
created

FileGenerator::getContents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Nwidart\Modules\Generators;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Nwidart\Modules\Exceptions\FileAlreadyExistException;
7
8
class FileGenerator extends Generator
9
{
10
    /**
11
     * The path wil be used.
12
     *
13
     * @var string
14
     */
15
    protected $path;
16
17
    /**
18
     * The contens will be used.
19
     *
20
     * @var string
21
     */
22
    protected $contents;
23
24
    /**
25
     * The laravel filesystem or null.
26
     *
27
     * @var \Illuminate\Filesystem\Filesystem|null
28
     */
29
    protected $filesystem;
30
31
    /**
32
     * The constructor.
33
     *
34
     * @param $path
35
     * @param $contents
36
     * @param null $filesystem
37
     */
38 53
    public function __construct($path, $contents, $filesystem = null)
39
    {
40 53
        $this->path = $path;
41 53
        $this->contents = $contents;
42 53
        $this->filesystem = $filesystem ?: new Filesystem();
43 53
    }
44
45
    /**
46
     * Get contents.
47
     *
48
     * @return mixed
49
     */
50 53
    public function getContents()
51
    {
52 53
        return $this->contents;
53
    }
54
55
    /**
56
     * Set contents.
57
     *
58
     * @param mixed $contents
59
     *
60
     * @return $this
61
     */
62
    public function setContents($contents)
63
    {
64
        $this->contents = $contents;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Get filesystem.
71
     *
72
     * @return mixed
73
     */
74
    public function getFilesystem()
75
    {
76
        return $this->filesystem;
77
    }
78
79
    /**
80
     * Set filesystem.
81
     *
82
     * @param Filesystem $filesystem
83
     *
84
     * @return $this
85
     */
86
    public function setFilesystem(Filesystem $filesystem)
87
    {
88
        $this->filesystem = $filesystem;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Get path.
95
     *
96
     * @return mixed
97
     */
98 53
    public function getPath()
99
    {
100 53
        return $this->path;
101
    }
102
103
    /**
104
     * Set path.
105
     *
106
     * @param mixed $path
107
     *
108
     * @return $this
109
     */
110
    public function setPath($path)
111
    {
112
        $this->path = $path;
113
114
        return $this;
115
    }
116
117
    /**
118
     * Generate the file.
119
     */
120 53
    public function generate()
121
    {
122 53
        if (!$this->filesystem->exists($path = $this->getPath())) {
123 53
            return $this->filesystem->put($path, $this->getContents());
124
        }
125
126
        throw new FileAlreadyExistException('File already exists!');
127
    }
128
}
129