Completed
Push — master ( d48ef1...47bb75 )
by
unknown
166:45 queued 146:49
created

GenerateFormPagePartsCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 6.6 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 5
dl 7
loc 106
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
A getWelcomeText() 0 4 1
A doExecute() 0 28 2
A doInteract() 0 19 2
A createGenerator() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kunstmaan\GeneratorBundle\Command;
4
5
use Kunstmaan\GeneratorBundle\Generator\DefaultPagePartGenerator;
6
use Symfony\Component\Console\Input\InputOption;
7
8
/**
9
 * Generates the default form pageparts
10
 */
11
class GenerateFormPagePartsCommand extends KunstmaanGenerateCommand
12
{
13
    /**
14
     * @var BundleInterface
15
     */
16
    private $bundle;
17
18
    /**
19
     * @var string
20
     */
21
    private $prefix;
22
23
    /**
24
     * @see Command
25
     */
26
    protected function configure()
27
    {
28
        $this->setDescription('Generates default form pageparts')
29
            ->setHelp(
30
                <<<EOT
31
The <info>kuma:generate:form-pageparts</info> command generates the default form pageparts and adds the pageparts configuration.
32
33
<info>php bin/console kuma:generate:form-pageparts</info>
34
EOT
35
            )
36
            ->addOption('namespace', '', InputOption::VALUE_OPTIONAL, 'The namespace to generate the default form pageparts in')
37
            ->addOption('prefix', '', InputOption::VALUE_OPTIONAL, 'The prefix to be used in the table name of the generated entity')
38
            ->setName('kuma:generate:form-pageparts');
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function getWelcomeText()
45
    {
46
        return 'Welcome to the Kunstmaan default form pageparts generator';
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function doExecute()
53
    {
54
        $this->assistant->writeSection('Default Form PageParts generation');
55
56
        $pagepartNames = [
57
            'AbstractFormPagePart',
58
            'CheckboxPagePart',
59
            'ChoicePagePart',
60
            'EmailPagePart',
61
            'FileUploadPagePart',
62
            'MultiLineTextPagePart',
63
            'SingleLineTextPagePart',
64
            'SubmitButtonPagePart',
65
        ];
66
67
        foreach ($pagepartNames as $pagepartName) {
68
            $this->createGenerator()->generate($this->bundle, $pagepartName, $this->prefix, [], false);
69
        }
70
71
        $this->assistant->writeSection('PageParts successfully created', 'bg=green;fg=black');
72
        $this->assistant->writeLine(
73
            [
74
                'Make sure you update your database first before you test the pagepart:',
75
                '    Directly update your database:          <comment>bin/console doctrine:schema:update --force</comment>',
76
                '    Create a Doctrine migration and run it: <comment>bin/console doctrine:migrations:diff && bin/console doctrine:migrations:migrate</comment>',
77
            ]
78
        );
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    protected function doInteract()
85
    {
86
        if (!$this->isBundleAvailable('KunstmaanPagePartBundle')) {
87
            $this->assistant->writeError('KunstmaanPagePartBundle not found', true);
88
        }
89
90
        $this->assistant->writeLine(["This command helps you to generate the form pageparts.\n"]);
91
92
        /**
93
         * Ask for which bundle we need to create the pagepart
94
         */
95
        $bundleNamespace = $this->assistant->getOptionOrDefault('namespace', null);
96
        $this->bundle = $this->askForBundleName('pagepart', $bundleNamespace);
0 ignored issues
show
Documentation Bug introduced by Sander
It seems like $this->askForBundleName(...art', $bundleNamespace) of type object<Symfony\Component...Bundle\BundleInterface> is incompatible with the declared type object<Kunstmaan\Generat...ommand\BundleInterface> of property $bundle.

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...
97
98
        /**
99
         * Ask the database table prefix
100
         */
101
        $this->prefix = $this->askForPrefix(null, $this->bundle->getNamespace());
102
    }
103
104
    /**
105
     * Get the generator.
106
     *
107
     * @return DefaultPagePartGenerator
108
     */
109 View Code Duplication
    protected function createGenerator()
0 ignored issues
show
Duplication introduced by Sander
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $filesystem = $this->getContainer()->get('filesystem');
112
        $registry = $this->getContainer()->get('doctrine');
113
114
        return new DefaultPagePartGenerator($filesystem, $registry, '/pagepart', $this->assistant, $this->getContainer());
115
    }
116
}
117