Completed
Push — master ( 51d11c...72fa5d )
by Rougin
01:42
created

CreateCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace Rougin\Refinery\Console;
4
5
use Rougin\Describe\Column;
6
use Rougin\Describe\Describe;
7
use Rougin\Refinery\Builder;
8
use Rougin\Refinery\Manager;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Create Command
17
 *
18
 * @package Refinery
19
 * @author  Rougin Royce Gutib <[email protected]>
20
 */
21
class CreateCommand extends Command
22
{
23
    /**
24
     * @var \Rougin\Refinery\Builder
25
     */
26
    protected $builder;
27
28
    /**
29
     * @var \Rougin\Describe\Describe
30
     */
31
    protected $describe;
32
33
    /**
34
     * @var \Rougin\Refinery\Manager
35
     */
36
    protected $manager;
37
38
    /**
39
     * Initializes the command instance.
40
     *
41
     * @param \Rougin\Refinery\Builder  $builder
42
     * @param \Rougin\Describe\Describe $describe
43
     * @param \Rougin\Refinery\Manager  $manager
44
     */
45
    public function __construct(Builder $builder, Describe $describe, Manager $manager)
46
    {
47
        $this->builder = $builder;
48
49
        $this->describe = $describe;
50
51
        parent::__construct();
52
53
        $this->manager = $manager;
54
    }
55
56
    /**
57
     * Configures the current command.
58
     *
59
     * @return void
60
     */
61
    protected function configure()
62
    {
63
        $this->setName('create')->setDescription('Creates a new database migration file');
64
        $this->addArgument('name', InputArgument::REQUIRED, 'Name of the migration file');
65
66
        $this->addOption('auto-increment', null, InputOption::VALUE_OPTIONAL, 'Sets the "auto_increment" value', false);
67
        $this->addOption('default', null, InputOption::VALUE_OPTIONAL, 'Sets the "default" value of the column', '');
68
        $this->addOption('from-database', null, InputOption::VALUE_NONE, 'Creates a migration from the database');
69
        $this->addOption('length', null, InputOption::VALUE_OPTIONAL, 'Sets the "constraint" value of the column', 50);
70
        $this->addOption('null', null, InputOption::VALUE_OPTIONAL, 'Sets the column with a nullable value', false);
71
        $this->addOption('primary', null, InputOption::VALUE_OPTIONAL, 'Sets the column as the primary key', false);
72
        $this->addOption('type', null, InputOption::VALUE_OPTIONAL, 'Sets the data type of the column', 'varchar');
73
        $this->addOption('unsigned', null, InputOption::VALUE_OPTIONAL, 'Sets the column with unsigned value', false);
74
    }
75
76
    /**
77
     * Executes the command.
78
     *
79
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
80
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
81
     * @return void
82
     */
83
    protected function execute(InputInterface $input, OutputInterface $output)
84
    {
85
        $name = $input->getArgument('name');
86
87
        $column = $this->column($input);
88
89
        $this->builder->column($column);
90
91
        if ($input->getOption('from-database')) {
92
            $this->builder->describe($this->describe);
93
        }
94
95
        $file = $this->builder->make($name);
96
97
        $name = $this->manager->filename($name);
98
99
        $this->manager->create($name, $file);
100
101
        $message = '"' . $name . '" has been created.';
102
103
        $output->writeln('<info>' . $message . '</info>');
104
    }
105
106
    /**
107
     * Initializes a column instance.
108
     *
109
     * @param  \Symfony\Component\Console\Input\InputInterface $input
110
     * @return \Rougin\Describe\Column
111
     */
112
    protected function column(InputInterface $input)
113
    {
114
        $column = new Column;
115
116
        $column->setDefaultValue($input->getOption('default'));
117
118
        $column->setDataType(strtoupper($input->getOption('type')));
119
120
        $column->setLength($input->getOption('length'));
121
122
        $column->setAutoIncrement($input->getOption('auto-increment'));
123
124
        $column->setNull($input->getOption('null'));
125
126
        $column->setUnsigned($input->getOption('unsigned'));
127
128
        $column->setPrimary($input->getOption('primary'));
129
130
        return $column;
131
    }
132
}
133