Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

Command/GenerateDefaultSiteCommand.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\GeneratorBundle\Command;
4
5
use Kunstmaan\GeneratorBundle\Generator\DefaultSiteGenerator;
6
use Symfony\Component\Console\Input\ArrayInput;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\ConsoleOutput;
9
10
/**
11
 * Generates a default website based on Kunstmaan bundles
12
 */
13
class GenerateDefaultSiteCommand extends KunstmaanGenerateCommand
14
{
15
    /**
16
     * @var BundleInterface
17
     */
18
    private $bundle;
19
20
    /**
21
     * @var string
22
     */
23
    private $prefix;
24
25
    /**
26
     * @var bool
27
     */
28
    private $demosite;
29
30
    /**
31
     * @see Command
32
     */
33
    protected function configure()
34
    {
35
        $this
36
            ->setHelp(<<<'EOT'
37
The <info>kuma:generate:site</info> command generates an website using the Kunstmaan bundles
38
39
<info>php bin/console kuma:generate:default-site --namespace=Namespace/NamedBundle</info>
40
41
Use the <info>--prefix</info> option to add a prefix to the table names of the generated entities
42
43
<info>php bin/console kuma:generate:default-site --namespace=Namespace/NamedBundle --prefix=demo_</info>
44
EOT
45
            )
46
            ->setDescription('Generates a basic website based on Kunstmaan bundles with default templates')
47
            ->addOption('namespace', '', InputOption::VALUE_OPTIONAL, 'The namespace to generate the default website in')
48
            ->addOption('prefix', '', InputOption::VALUE_OPTIONAL, 'The prefix to be used in the table names of the generated entities')
49
            ->addOption('demosite', '', InputOption::VALUE_NONE, 'Whether to generate a website with demo contents or a basic website')
50
            ->addOption('browsersync', '', InputOption::VALUE_OPTIONAL, 'The URI that will be used for browsersync to connect')
51
            ->addOption('articleoverviewpageparent', '', InputOption::VALUE_OPTIONAL, 'Shortnames of the pages that can have the article overview page as a child (comma separated)')
52
            ->setName('kuma:generate:default-site');
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function getWelcomeText()
59
    {
60
        return 'Welcome to the Kunstmaan default site generator';
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    protected function doExecute()
67
    {
68
        $this->assistant->writeSection('Site generation');
69
        $this->assistant->writeLine(array("This command helps you to generate a default site setup.\n"));
70
71
        /**
72
         * Ask for which bundle we need to create the layout
73
         */
74
        $bundleNamespace = $this->assistant->getOptionOrDefault('namespace', null);
75
        $this->bundle = $this->askForBundleName('layout', $bundleNamespace);
76
77
        /*
78
         * Ask the database table prefix
79
         */
80
        $this->prefix = $this->askForPrefix(null, $this->bundle->getNamespace());
81
82
        /*
83
         * If we need to generate a full site, or only the basic structure
84
         */
85
        $this->demosite = $this->assistant->getOption('demosite');
86
87
        $browserSyncUrl = $this->assistant->getOptionOrDefault('browsersync', null);
88
89
        // First we generate the layout if it is not yet generated
90
        $command = $this->getApplication()->find('kuma:generate:layout');
91
        $arguments = array(
92
            'command' => 'kuma:generate:layout',
93
            '--namespace' => str_replace('\\', '/', $this->bundle->getNamespace()),
94
            '--demosite' => $this->demosite,
95
            '--browsersync' => $browserSyncUrl,
96
            '--subcommand' => true,
97
        );
98
        $input = new ArrayInput($arguments);
99
        $command->run($input, $this->assistant->getOutput());
100
101
        $rootDir = $this->getApplication()->getKernel()->getProjectDir().'/';
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getKernel() does only exist in the following sub-classes of Symfony\Component\Console\Application: Symfony\Bundle\FrameworkBundle\Console\Application. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
102
        $this->createGenerator()->generate($this->bundle, $this->prefix, $rootDir, $this->demosite);
103
104
        // Generate the default pageparts
105
        $command = $this->getApplication()->find('kuma:generate:default-pageparts');
106
        $arguments = array(
107
            'command' => 'kuma:generate:default-pageparts',
108
            '--namespace' => str_replace('\\', '/', $this->bundle->getNamespace()),
109
            '--prefix' => $this->prefix,
110
            '--contexts' => 'main',
111
            '--quiet' => true,
112
        );
113
        $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_QUIET);
114
        $input = new ArrayInput($arguments);
115
        $command->run($input, $output);
116
        $this->assistant->writeLine('Generating default pageparts : <info>OK</info>');
117
118
        if ($this->demosite) {
119
            // Generate a blog
120
            $command = $this->getApplication()->find('kuma:generate:article');
121
            $pages = $this->assistant->getOptionOrDefault('articleoverviewpageparent', null);
122
            $arguments = array(
123
                'command' => 'kuma:generate:article',
124
                '--namespace' => str_replace('\\', '/', $this->bundle->getNamespace()),
125
                '--prefix' => $this->prefix,
126
                '--entity' => 'Blog',
127
                '--with-author' => true,
128
                '--with-category' => true,
129
                '--with-tag' => true,
130
                '--dummydata' => true,
131
                '--articleoverviewpageparent' => $pages,
132
            );
133
            $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL);
134
            $input = new ArrayInput($arguments);
135
            $command->run($input, $output);
136
137
            $this->assistant->writeLine('Generating blog : <info>OK</info>');
138
        }
139
140
        $this->assistant->writeSection('Site successfully created', 'bg=green;fg=black');
141
142
        return 0;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    protected function doInteract()
149
    {
150
    }
151
152
    /**
153
     * Get the generator.
154
     *
155
     * @return DefaultSiteGenerator
156
     */
157 View Code Duplication
    protected function createGenerator()
0 ignored issues
show
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...
158
    {
159
        $filesystem = $this->getContainer()->get('filesystem');
160
        $registry = $this->getContainer()->get('doctrine');
161
162
        return new DefaultSiteGenerator($filesystem, $registry, '/defaultsite', $this->assistant, $this->getContainer());
163
    }
164
}
165