Completed
Push — master ( 7e29e1...6077fa )
by Pascal
02:32
created

AbstractScaffoldingCommand::createFileTemplate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 0
cts 14
cp 0
rs 9.2
cc 3
eloc 11
nc 2
nop 3
crap 12
1
<?php
2
namespace Atog\Api\Console;
3
4
use Symfony\Component\Console\Command\Command;
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * Class ModelCommand
11
 * @package Atog\Console
12
 */
13
abstract class AbstractScaffoldingCommand extends Command
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $type;
19
20
    /**
21
     * Command Config
22
     */
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('create:' . $this->type)
27
            ->setDescription('Create a new ' . ucfirst($this->type))
28
            ->addArgument(
29
                'namespace',
30
                InputArgument::REQUIRED,
31
                'Namespace in Dot-Notation e.g. Acme.Api'
32
            )
33
            ->addArgument(
34
                'name',
35
                InputArgument::REQUIRED,
36
                'Name of the ' . ucfirst($this->type)
37
            )
38
        ;
39
    }
40
41
    /**
42
     * @param \Symfony\Component\Console\Input\InputInterface   $input
43
     * @param \Symfony\Component\Console\Output\OutputInterface $output
44
     * @param string                                            $path
45
     */
46
    protected function createFileTemplate(InputInterface $input, OutputInterface $output, $path)
47
    {
48
        // args
49
        $namespace = str_replace('.', '\\', $input->getArgument('namespace'));
50
        $name = $input->getArgument('name');
51
        $filename = $path . str_plural($name) . '.php';
52
53
        // only write file if path exists and file does not exist yet
54
        if (file_exists($path) && !file_exists($filename)) {
55
            // write file
56
            $file = fopen($filename, "w+");
57
            fwrite($file, $this->template($namespace, $name));
58
            fclose($file);
59
60
            // output
61
            $output->writeln("<info>Created {$this->type}</info>");
62
            return;
63
        }
64
65
        // error
66
        $output->writeln("<error>Could not create file $filename at $path</error>");
67
    }
68
69
    /**
70
     * File Template
71
     * @param string $namespace
72
     * @param string $name
73
     * @param string $topNSPart
74
     * @return string
75
     */
76
    abstract protected function template($namespace, $name, $topNSPart = "");
77
}
78