Passed
Push — master ( 590453...ad5a85 )
by Arthur
24:49
created

FileGenerator::generateGitKeep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: tony
5
 * Date: 12.03.19
6
 * Time: 16:04
7
 */
8
9
namespace Foundation\Generator\Generators;
10
11
12
use Illuminate\Filesystem\Filesystem;
13
use Nwidart\Modules\Exceptions\FileAlreadyExistException;
14
15
class FileGenerator
16
{
17
    /**
18
     * The path wil be used.
19
     *
20
     * @var string
21
     */
22
    protected $path;
23
24
    /**
25
     * The contens will be used.
26
     *
27
     * @var string
28
     */
29
    protected $contents;
30
31
    /**
32
     * The laravel filesystem or null.
33
     *
34
     * @var \Illuminate\Filesystem\Filesystem|null
35
     */
36
    protected $filesystem;
37
38
    /**
39
     * The constructor.
40
     *
41
     * @param $path
42
     * @param $contents
43
     * @param null $filesystem
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $filesystem is correct as it would always require null to be passed?
Loading history...
44
     */
45
    public function __construct($path, $contents, $filesystem = null)
46
    {
47
        $this->path = $path;
48
        $this->contents = $contents;
49
        $this->filesystem = $filesystem ?: new Filesystem();
0 ignored issues
show
introduced by
$filesystem is of type null, thus it always evaluated to false.
Loading history...
50
    }
51
52
    /**
53
     * Get contents.
54
     *
55
     * @return mixed
56
     */
57
    public function getContents()
58
    {
59
        return $this->contents;
60
    }
61
62
    /**
63
     * Set contents.
64
     *
65
     * @param mixed $contents
66
     *
67
     * @return $this
68
     */
69
    public function setContents($contents)
70
    {
71
        $this->contents = $contents;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Get filesystem.
78
     *
79
     * @return mixed
80
     */
81
    public function getFilesystem()
82
    {
83
        return $this->filesystem;
84
    }
85
86
    /**
87
     * Set filesystem.
88
     *
89
     * @param Filesystem $filesystem
90
     *
91
     * @return $this
92
     */
93
    public function setFilesystem(Filesystem $filesystem)
94
    {
95
        $this->filesystem = $filesystem;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Get path.
102
     *
103
     * @return mixed
104
     */
105
    public function getPath()
106
    {
107
        return $this->path;
108
    }
109
110
    /**
111
     * Set path.
112
     *
113
     * @param mixed $path
114
     *
115
     * @return $this
116
     */
117
    public function setPath($path)
118
    {
119
        $this->path = $path;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Generate the file.
126
     */
127
    public function generate()
128
    {
129
        if (!$this->filesystem->isDirectory($dir = dirname($this->getPath()))) {
0 ignored issues
show
Bug introduced by
The method isDirectory() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
        if (!$this->filesystem->/** @scrutinizer ignore-call */ isDirectory($dir = dirname($this->getPath()))) {

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...
130
            $this->filesystem->makeDirectory($dir, 0777, true);
131
        }
132
        if (!$this->filesystem->exists($path = $this->getPath())) {
133
            if (!$this->filesystem->exists($gitkeepPath = dirname($path).'/.gitkeep')) {
134
                $this->generateGitKeep($gitkeepPath);
135
            }
136
            return $this->filesystem->put($path, $this->getContents());
137
        }
138
139
        throw new FileAlreadyExistException('File already exists!');
140
    }
141
142
    /**
143
     * Generate git keep to the specified path.
144
     *
145
     * @param string $path
146
     */
147
    public function generateGitKeep($path)
148
    {
149
        $this->filesystem->put($path, '');
150
    }
151
}
152