Completed
Pull Request — master (#8)
by
unknown
08:15
created

FilesManager::loadContentFromFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace HelloWordPl\SimpleEntityGeneratorBundle\Lib;
4
5
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Exceptions\FilesManagerException;
6
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Interfaces\DumpableInterface;
7
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Renderer;
8
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\StructureResolver;
9
use ReflectionClass;
10
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
11
use Symfony\Component\Filesystem\Filesystem;
12
use Symfony\Component\HttpKernel\KernelInterface;
13
14
/**
15
 * Files Manager
16
 * - load structure from yaml
17
 * - create directory if do not exist
18
 * - update exisiting sources
19
 *
20
 * @author Sławomir Kania <[email protected]>
21
 */
22
class FilesManager
23
{
24
25
    /**
26
     * @var KernelInterface
27
     */
28
    private $kernel = null;
29
30
    /**
31
     * CONSTR
32
     *
33
     * @param KernelInterface $kernel
34
     */
35
    public function __construct(KernelInterface $kernel)
36
    {
37
        $this->kernel = $kernel;
38
    }
39
40
    /**
41
     * Load class structure from yaml file to array array('{"data":"data"}', '{"data":"data"}')
42
     * File has to be placed in to the directory src/{bundleName}/Resources/config/{fileName}
43
     *
44
     * @param string $bundleName
45
     * @param string $fileName
46
     * @return string
47
     */
48
    public function loadFileContent($bundleName, $fileName)
49
    {
50
        $fileDirectory = sprintf('%s/../src/%s/Resources/config/%s', $this->getKernel()->getRootDir(), $bundleName, $fileName);
51
        $result = file_get_contents($fileDirectory);
52
        if (is_bool($result) && false === $result) {
53
            throw new FilesManagerException(sprintf("Can not load file from: %s", $fileDirectory));
54
        }
55
56
        return $result;
57
    }
58
59
    /**
60
     * Create or update item file
61
     *
62
     * @param DumpableInterface $item
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
        $this->checkItemFileExists($item);
111
        return new ReflectionClass($item->getNamespace());
112
    }
113
114
    /**
115
     * @param DumpableInterface $item
116
     * @return string
117
     * @throws FilesManagerException
118
     */
119
    public function getContentFromItemFile(DumpableInterface $item)
120
    {
121
        $this->checkItemFileExists($item);
122
        $fullFileDirectory = $this->getItemDirectoryWithClassNameAndExtension($item);
123
        return file_get_contents($fullFileDirectory);
124
    }
125
126
    /**
127
     * @param string $path
128
     * @return string
129
     * @throws FilesManagerException
130
     */
131
    public function loadContentFromFile($path)
132
    {
133
        $fs = $this->getFilesystem();
134
        if (false === $fs->exists($path)) {
135
            throw new FilesManagerException(sprintf("Template file %s does not exist", $path));
136
        }
137
138
        return file_get_contents($path);
139
    }
140
141
    /**
142
     * @param DumpableInterface $item
143
     * @throws FilesManagerException
144
     */
145
    protected function updateExistingFile(DumpableInterface $item)
146
    {
147
        $fs = $this->getFilesystem();
148
        $content = $this->getContentFromItemFile($item);
149
        $reflectionClass = $this->getReflectionClassForItem($item);
150
        $updatedContent = $this->getStructureResolver()->getUpdatedItemSourceContent($content, $item, $reflectionClass);
151
        if (false === empty($updatedContent)) {
152
            $fs->dumpFile($this->getItemDirectoryWithClassNameAndExtension($item), $updatedContent);
153
        }
154
155
        return $item;
156
    }
157
158
    /**
159
     * @param DumpableInterface $item
160
     * @throws FilesManagerException
161
     */
162
    protected function createDirectoryIfNeedAndFileAndDumpContent(DumpableInterface $item)
163
    {
164
        $fs = $this->getFilesystem();
165
        $fullFileDirectory = $this->getItemDirectoryWithClassNameAndExtension($item);
166
        $directory = $this->getItemDirectory($item);
167
        if (false === $fs->exists($directory)) {
168
            $fs->mkdir($directory);
169
        }
170
171
        $fs->touch($fullFileDirectory);
172
        if (false === $fs->exists($fullFileDirectory)) {
173
            throw new FilesManagerException("Structure file can not be created");
174
        }
175
176
        $content = $this->getRenderer()->render($item);
0 ignored issues
show
Documentation introduced by
$item is of type object<HelloWordPl\Simpl...aces\DumpableInterface>, but the function expects a object<HelloWordPl\Simpl...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...
177
        if (false === empty($content)) {
178
            $fs->dumpFile($fullFileDirectory, $content);
179
        }
180
    }
181
182
    /**
183
     * @return KernelInterface
184
     */
185
    protected function getKernel()
186
    {
187
        return $this->kernel;
188
    }
189
190
    /**
191
     * @return Filesystem
192
     */
193
    protected function getFilesystem()
194
    {
195
        return new Filesystem();
196
    }
197
198
    /**
199
     * @param DumpableInterface $item
200
     * @return string
201
     */
202
    protected function getItemDirectory(DumpableInterface $item)
203
    {
204
        return sprintf('%s/../src%s', $this->getKernel()->getRootDir(), $item->getDirectory());
205
    }
206
207
    /**
208
     * @param DumpableInterface $item
209
     * @return string
210
     */
211
    protected function getItemDirectoryWithClassNameAndExtension(DumpableInterface $item)
212
    {
213
        return sprintf("%s/%s.php", $this->getItemDirectory($item), $item->getName());
214
    }
215
216
    /**
217
     * @return StructureResolver
218
     */
219
    protected function getStructureResolver()
220
    {
221
        return $this->getKernel()->getContainer()->get("seg.structure_resolver");
222
    }
223
224
    /**
225
     * @return Renderer
226
     */
227
    protected function getRenderer()
228
    {
229
        return $this->getKernel()->getContainer()->get("seg.renderer");
230
    }
231
}
232