Completed
Push — master ( 993468...5fd9a9 )
by Markus
12s queued 10s
created

AbstractClassGenerator::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Jellyfish\Transfer\Generator;
4
5
use Jellyfish\Transfer\Definition\ClassDefinitionInterface;
6
7
abstract class AbstractClassGenerator extends AbstractGenerator implements ClassGeneratorInterface
8
{
9
    /**
10
     * @param \Jellyfish\Transfer\Definition\ClassDefinitionInterface $classDefinition
11
     *
12
     * @return \Jellyfish\Transfer\Generator\ClassGeneratorInterface
13
     *
14
     * @throws \Twig\Error\LoaderError
15
     * @throws \Twig\Error\RuntimeError
16
     * @throws \Twig\Error\SyntaxError
17
     */
18
    public function generate(ClassDefinitionInterface $classDefinition): ClassGeneratorInterface
19
    {
20
        $file = $this->getFile($classDefinition);
21
        $pathToFile = $this->getPathToFile($classDefinition);
22
        $fileContent = $this->twig->render($this->getTemplateName(), [
23
            'classDefinition' => $classDefinition
24
        ]);
25
26
        $this->createDirectories($pathToFile);
27
28
        $this->filesystem->writeToFile($pathToFile . $file, $fileContent);
29
30
        return $this;
31
    }
32
33
    /**
34
     * @param \Jellyfish\Transfer\Definition\ClassDefinitionInterface $classDefinition
35
     *
36
     * @return string
37
     */
38
    abstract protected function getFile(ClassDefinitionInterface $classDefinition): string;
39
40
    /**
41
     * @param \Jellyfish\Transfer\Definition\ClassDefinitionInterface $classDefinition
42
     *
43
     * @return string
44
     */
45
    protected function getPathToFile(ClassDefinitionInterface $classDefinition): string
46
    {
47
        $namespace = $classDefinition->getNamespace();
48
49
        if ($namespace === null) {
50
            return $this->targetDirectory;
51
        }
52
53
        $namespaceParts = \explode('\\', $namespace);
54
55
        return $this->targetDirectory . \implode(DIRECTORY_SEPARATOR, $namespaceParts) . DIRECTORY_SEPARATOR;
56
    }
57
}
58