Completed
Push — master ( 6077fa...f4e20a )
by Pascal
02:40
created

AbstractScaffoldingCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 14
cts 14
cp 1
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
crap 1
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 4
    protected function configure()
24
    {
25 4
        $this
26 4
            ->setName('create:' . $this->type)
27 4
            ->setDescription('Create a new ' . ucfirst($this->type))
28 4
            ->addArgument(
29 4
                'namespace',
30 4
                InputArgument::REQUIRED,
31
                'Namespace in Dot-Notation e.g. Acme.Api'
32 4
            )
33 4
            ->addArgument(
34 4
                'name',
35 4
                InputArgument::REQUIRED,
36 4
                'Name of the ' . ucfirst($this->type)
37 4
            )
38
        ;
39 4
    }
40
41
    /**
42
     * @param \Symfony\Component\Console\Input\InputInterface   $input
43
     * @param \Symfony\Component\Console\Output\OutputInterface $output
44
     * @param string                                            $path
45
     */
46 4
    protected function createFileTemplate(InputInterface $input, OutputInterface $output, $path)
47
    {
48
        // args
49 4
        $namespace = str_replace('.', '\\', $input->getArgument('namespace'));
50 4
        $name = $input->getArgument('name');
51 4
        $filename = $path . str_plural($name) . '.php';
52
        
53
        // only write file if path exists and file does not exist yet
54 4
        if (file_exists($path) && !file_exists($filename)) {
55
            // write file
56 4
            $file = fopen($filename, "w+");
57 4
            fwrite($file, $this->template($namespace, $name));
58 4
            fclose($file);
59
60
            // output
61 4
            $output->writeln("<info>Created {$this->type}</info>");
62 4
            return;
63
        }
64
65
        // error
66 2
        $output->writeln("<error>Could not create file $name</error>");
67 2
    }
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