Completed
Pull Request — master (#14)
by
unknown
08:12
created

FilesManager::getFilesystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SimpleEntityGeneratorBundle\Lib;
4
5
use SimpleEntityGeneratorBundle\Lib\Exceptions\FilesManagerException;
6
use SimpleEntityGeneratorBundle\Lib\Interfaces\DumpableInterface;
7
use ReflectionClass;
8
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\HttpKernel\KernelInterface;
11
12
/**
13
 * Files Manager
14
 * - load structure from yaml
15
 * - create directory if do not exist
16
 * - update exisiting sources
17
 *
18
 * @author Sławomir Kania <[email protected]>
19
 */
20
class FilesManager
21
{
22
23
    /**
24
     * @var KernelInterface
25
     */
26
    private $kernel = null;
27
28
    /**
29
     * CONSTR
30
     *
31
     * @param KernelInterface $kernel
32
     */
33
    public function __construct(KernelInterface $kernel)
34
    {
35
        $this->kernel = $kernel;
36
    }
37
38
    /**
39
     * Load class structure from yaml file to array array('{"data":"data"}', '{"data":"data"}')
40
     * File has to be placed in to the directory src/{bundleName}/Resources/config/{fileName}
41
     *
42
     * @param string $bundleName
43
     * @param string $fileName
44
     * @return string
45
     * @throws FilesManagerException
46
     */
47
    public function loadFileContent($bundleName, $fileName)
48
    {
49
        $fileDirectory = sprintf('%s/../src/%s/Resources/config/%s', $this->getKernel()->getRootDir(), $bundleName, $fileName);
50
        $result = file_get_contents($fileDirectory);
51
        if (is_bool($result) && false === $result) {
52
            throw new FilesManagerException(sprintf("Can not load file from: %s", $fileDirectory));
53
        }
54
55
        return $result;
56
    }
57
58
    /**
59
     * Create or update item file
60
     *
61
     * @param DumpableInterface $item
62
     * @return DumpableInterface
63
     * @throws FilesManagerException
64
     */
65
    public function dump(DumpableInterface $item)
66
    {
67
        try {
68
            if ($this->isItemFileExists($item)) {
69
                $this->updateExistingFile($item); // working on reference
70
            } else {
71
                $this->createDirectoryIfNeedAndFileAndDumpContent($item);
72
            }
73
74
            return $item;
75
        } catch (IOExceptionInterface $e) {
76
            throw new FilesManagerException("An error occurred while creating your directory at ".$e->getPath());
77
        }
78
    }
79
80
    /**
81
     * @param DumpableInterface $item
82
     * @return boolean
83
     */
84
    public function isItemFileExists(DumpableInterface $item)
85
    {
86
        $fs = $this->getFilesystem();
87
        $fullFileDirectory = $this->getItemDirectoryWithClassNameAndExtension($item);
88
89
        return $fs->exists($fullFileDirectory);
90
    }
91
92
    /**
93
     * @param DumpableInterface $item
94
     * @throws FilesManagerException
95
     */
96
    public function checkItemFileExists(DumpableInterface $item)
97
    {
98
        if (false === $this->isItemFileExists($item)) {
99
            throw new FilesManagerException(sprintf("Item source file does not exist: %s", $item->getNamespace()));
100
        }
101
    }
102
103
    /**
104
     * @param DumpableInterface $item
105
     * @return ReflectionClass
106
     * @throws FilesManagerException
107
     */
108
    public function getReflectionClassForItem(DumpableInterface $item)
109
    {
110
        if(!class_exists($item->getNamespace()) && !interface_exists($item->getNamespace())){
111
            throw new FilesManagerException(sprintf("Item source file does not exist: %s", $item->getNamespace()));
112
        }
113
        return new ReflectionClass($item->getNamespace());
114
    }
115
116
    /**
117
     * @param DumpableInterface $item
118
     * @return string
119
     * @throws FilesManagerException
120
     */
121
    public function getContentFromItemFile(DumpableInterface $item)
122
    {
123
        $this->checkItemFileExists($item);
124
        $fullFileDirectory = $this->getItemDirectoryWithClassNameAndExtension($item);
125
        return file_get_contents($fullFileDirectory);
126
    }
127
128
    /**
129
     * @param string $path
130
     * @return string
131
     * @throws FilesManagerException
132
     */
133
    public function loadContentFromFile($path)
134
    {
135
        $fs = $this->getFilesystem();
136
        if (false === $fs->exists($path)) {
137
            throw new FilesManagerException(sprintf("Template file %s does not exist", $path));
138
        }
139
140
        return file_get_contents($path);
141
    }
142
143
    /**
144
     * @param DumpableInterface $item
145
     * @return DumpableInterface
146
     * @throws FilesManagerException
147
     */
148
    protected function updateExistingFile(DumpableInterface $item)
149
    {
150
        $fs = $this->getFilesystem();
151
        $content = $this->getContentFromItemFile($item);
152
        $reflectionClass = $this->getReflectionClassForItem($item);
153
        $updatedContent = $this->getStructureResolver()->getUpdatedItemSourceContent($content, $item, $reflectionClass);
154
        if (false === empty($updatedContent)) {
155
            $fs->dumpFile($this->getItemDirectoryWithClassNameAndExtension($item), $updatedContent);
156
        }
157
158
        return $item;
159
    }
160
161
    /**
162
     * @param DumpableInterface $item
163
     * @throws FilesManagerException
164
     */
165
    protected function createDirectoryIfNeedAndFileAndDumpContent(DumpableInterface $item)
166
    {
167
        $fs = $this->getFilesystem();
168
        $fullFileDirectory = $this->getItemDirectoryWithClassNameAndExtension($item);
169
        $directory = $this->getItemDirectory($item);
170
        if (false === $fs->exists($directory)) {
171
            $fs->mkdir($directory);
172
        }
173
174
        $fs->touch($fullFileDirectory);
175
        if (false === $fs->exists($fullFileDirectory)) {
176
            throw new FilesManagerException("Structure file can not be created");
177
        }
178
179
        $content = $this->getRenderer()->render($item);
0 ignored issues
show
Documentation introduced by
$item is of type object<SimpleEntityGener...aces\DumpableInterface>, but the function expects a object<SimpleEntityGener...es\RenderableInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
180
        if (false === empty($content)) {
181
            $fs->dumpFile($fullFileDirectory, $content);
182
        }
183
    }
184
185
    /**
186
     * @return KernelInterface
187
     */
188
    protected function getKernel()
189
    {
190
        return $this->kernel;
191
    }
192
193
    /**
194
     * @return Filesystem
195
     */
196
    protected function getFilesystem()
197
    {
198
        return new Filesystem();
199
    }
200
201
    /**
202
     * @param DumpableInterface $item
203
     * @return string
204
     */
205
    protected function getItemDirectory(DumpableInterface $item)
206
    {
207
        return sprintf('%s/../src%s', $this->getKernel()->getRootDir(), $item->getDirectory());
208
    }
209
210
    /**
211
     * @param DumpableInterface $item
212
     * @return string
213
     */
214
    protected function getItemDirectoryWithClassNameAndExtension(DumpableInterface $item)
215
    {
216
        return sprintf("%s/%s.php", $this->getItemDirectory($item), $item->getName());
217
    }
218
219
    /**
220
     * @return StructureResolver
221
     */
222
    protected function getStructureResolver()
223
    {
224
        return $this->getKernel()->getContainer()->get("seg.structure_resolver");
225
    }
226
227
    /**
228
     * @return Renderer
229
     */
230
    protected function getRenderer()
231
    {
232
        return $this->getKernel()->getContainer()->get("seg.renderer");
233
    }
234
}
235