Completed
Push — master ( 7b302a...d24209 )
by Tristan
04:27
created

BaseCommand::execute()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 25
rs 8.439
cc 5
eloc 15
nc 12
nop 2
1
<?php
2
3
namespace Enzyme\Axiom\Console\Commands;
4
5
use ICanBoogie\Inflector;
6
use Enzyme\Axiom\Console\Config;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Enzyme\Axiom\Console\Stubs\Manager as StubManager;
13
14
abstract class BaseCommand extends Command
15
{
16
    /**
17
     * Reference to the stub manager.
18
     *
19
     * @var Enzyme\Axiom\Console\Stubs\Manager
20
     */
21
    protected $stub_manager;
22
23
    /**
24
     * The namespace this generated class falls under.
25
     *
26
     * @var string
27
     */
28
    protected $namespace;
29
30
    /**
31
     * The location this generated class will be saved at.
32
     *
33
     * @var string
34
     */
35
    protected $location;
36
37
    public function __construct(StubManager $stub_manager, Config $config)
38
    {
39
        parent::__construct();
40
41
        $this->stub_manager = $stub_manager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $stub_manager of type object<Enzyme\Axiom\Console\Stubs\Manager> is incompatible with the declared type object<Enzyme\Axiom\Cons...\Console\Stubs\Manager> of property $stub_manager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
        $this->config = $config;
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
    }
44
45
    /**
46
     * The generator type this class is handling, eg: "factory".
47
     *
48
     * @return string
49
     */
50
    abstract protected function getGeneratorType();
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    protected function configure()
56
    {
57
        $generator_type = $this->getGeneratorType();
58
59
        $this
60
            ->setName("make:{$generator_type}")
61
            ->setDescription("Make a new {$generator_type}.")
62
            ->addArgument(
63
                'name',
64
                InputArgument::REQUIRED,
65
                "The name prefix on the generated {$generator_type}, eg: \"user\""
66
            )
67
            ->addOption(
68
                'location',
69
                null,
70
                InputOption::VALUE_REQUIRED,
71
                'The location for the generated file.'
72
            )
73
            ->addOption(
74
               'namespace',
75
               null,
76
               InputOption::VALUE_REQUIRED,
77
               'The namespace to fall under.'
78
            );
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    protected function execute(InputInterface $input, OutputInterface $output)
85
    {
86
        $inflector = Inflector::get(Inflector::DEFAULT_LOCALE);
87
        $type_plural = $inflector->pluralize($this->getGeneratorType());
88
89
        $this->namespace = $input->getOption('namespace') !== null
90
            ? $input->getOption('namespace')
91
            : $this->config->get("{$type_plural}.namespace");
92
93
        $this->location = $input->getOption('location') !== null
94
            ? $input->getOption('location')
95
            : $this->config->get("{$type_plural}.location");
96
97
        if (null === $this->namespace) {
98
            throw new Exception(
99
                "There is no namespace defined for {$type_plural}"
100
            );
101
        }
102
103
        if (null === $this->location) {
104
            throw new Exception(
105
                "There is no location defined for {$type_plural}"
106
            );
107
        }
108
    }
109
110
    /**
111
     * Execute a generation command, aka make a single usuable Axiom class.
112
     *
113
     * @param InputInterface  $input
114
     * @param OutputInterface $output
115
     * @param bool            $dont_affix_type If true, don't add the type to
116
     *                        the generated class name and file.
117
     */
118
    protected function executeGeneration(
119
        InputInterface $input,
120
        OutputInterface $output,
121
        $dont_affix_type = false
122
    ) {
123
        $uc_name = ucfirst($input->getArgument('name'));
124
        $uc_type = ucfirst($this->getGeneratorType());
125
126
        $contents = $this->stub_manager->get($this->getGeneratorType());
127
        $contents = $this->stub_manager->hydrate($contents, [
128
            'namespace' => $this->namespace,
129
            'name'      => $uc_name,
130
        ]);
131
132
        $out_file = $dont_affix_type === true
133
            ? "{$this->location}/{$uc_name}.php"
134
            : "{$this->location}/{$uc_name}{$uc_type}.php";
135
136
        $this->stub_manager->writeOut($contents, $out_file);
137
138
        $this->printResults(
139
            $output,
140
            $uc_name,
141
            $dont_affix_type
142
        );
143
    }
144
145
    /**
146
     * Print the generation results to the console window.
147
     *
148
     * @param OutputInterface $output
149
     * @param string          $name The name prefix of the generated class.
150
     */
151
    protected function printResults(
152
        OutputInterface $output,
153
        $name,
154
        $dont_affix_type = false
155
    ) {
156
        $uc_type = ucfirst($this->getGeneratorType());
157
        $output->writeln("<info>{$uc_type} created for [{$name}]</info>");
158
159
        $class = $dont_affix_type === true
160
            ? $name
161
            : $name.$uc_type;
162
163
        if (true === $output->isVerbose()) {
164
            $output->writeln("<comment>    Namespace -> {$this->namespace}</comment>");
165
            $output->writeln("<comment>        Class -> {$class}</comment>");
166
            $output->writeln("<comment>     Location -> {$this->location}</comment>");
167
        }
168
    }
169
}
170