InstallCommand::checkRequirements()   F
last analyzed

Complexity

Conditions 14
Paths 324

Size

Total Lines 98
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 57
nc 324
nop 0
dl 0
loc 98
rs 3.8833
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Wallabag\CoreBundle\Command;
4
5
use FOS\UserBundle\Event\UserEvent;
6
use FOS\UserBundle\FOSUserEvents;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Input\ArrayInput;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\BufferedOutput;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Question\Question;
14
use Symfony\Component\Console\Style\SymfonyStyle;
15
use Wallabag\CoreBundle\Entity\IgnoreOriginInstanceRule;
16
use Wallabag\CoreBundle\Entity\InternalSetting;
17
18
class InstallCommand extends ContainerAwareCommand
19
{
20
    /**
21
     * @var InputInterface
22
     */
23
    protected $defaultInput;
24
25
    /**
26
     * @var SymfonyStyle
27
     */
28
    protected $io;
29
30
    /**
31
     * @var array
32
     */
33
    protected $functionExists = [
34
        'curl_exec',
35
        'curl_multi_init',
36
    ];
37
38
    protected function configure()
39
    {
40
        $this
41
            ->setName('wallabag:install')
42
            ->setDescription('wallabag installer.')
43
            ->addOption(
44
               'reset',
45
               null,
46
               InputOption::VALUE_NONE,
47
               'Reset current database'
48
            )
49
        ;
50
    }
51
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $this->defaultInput = $input;
55
56
        $this->io = new SymfonyStyle($input, $output);
57
58
        $this->io->title('wallabag installer');
59
60
        $this
61
            ->checkRequirements()
62
            ->setupDatabase()
63
            ->setupAdmin()
64
            ->setupConfig()
65
        ;
66
67
        $this->io->success('wallabag has been successfully installed.');
68
        $this->io->success('You can now configure your web server, see https://doc.wallabag.org');
69
    }
70
71
    protected function checkRequirements()
72
    {
73
        $this->io->section('Step 1 of 4: Checking system requirements.');
74
75
        $doctrineManager = $this->getContainer()->get('doctrine')->getManager();
76
77
        $rows = [];
78
79
        // testing if database driver exists
80
        $fulfilled = true;
81
        $label = '<comment>PDO Driver (%s)</comment>';
82
        $status = '<info>OK!</info>';
83
        $help = '';
84
85
        if (!\extension_loaded($this->getContainer()->getParameter('database_driver'))) {
86
            $fulfilled = false;
87
            $status = '<error>ERROR!</error>';
88
            $help = 'Database driver "' . $this->getContainer()->getParameter('database_driver') . '" is not installed.';
89
        }
90
91
        $rows[] = [sprintf($label, $this->getContainer()->getParameter('database_driver')), $status, $help];
92
93
        // testing if connection to the database can be etablished
94
        $label = '<comment>Database connection</comment>';
95
        $status = '<info>OK!</info>';
96
        $help = '';
97
98
        $conn = $this->getContainer()->get('doctrine')->getManager()->getConnection();
99
100
        try {
101
            $conn->connect();
102
        } catch (\Exception $e) {
103
            if (false === strpos($e->getMessage(), 'Unknown database')
104
                && false === strpos($e->getMessage(), 'database "' . $this->getContainer()->getParameter('database_name') . '" does not exist')) {
105
                $fulfilled = false;
106
                $status = '<error>ERROR!</error>';
107
                $help = 'Can\'t connect to the database: ' . $e->getMessage();
108
            }
109
        }
110
111
        $rows[] = [$label, $status, $help];
112
113
        // check MySQL & PostgreSQL version
114
        $label = '<comment>Database version</comment>';
115
        $status = '<info>OK!</info>';
116
        $help = '';
117
118
        // now check if MySQL isn't too old to handle utf8mb4
119
        if ($conn->isConnected() && 'mysql' === $conn->getDatabasePlatform()->getName()) {
120
            $version = $conn->query('select version()')->fetchColumn();
121
            $minimalVersion = '5.5.4';
122
123
            if (false === version_compare($version, $minimalVersion, '>')) {
124
                $fulfilled = false;
125
                $status = '<error>ERROR!</error>';
126
                $help = 'Your MySQL version (' . $version . ') is too old, consider upgrading (' . $minimalVersion . '+).';
127
            }
128
        }
129
130
        // testing if PostgreSQL > 9.1
131
        if ($conn->isConnected() && 'postgresql' === $conn->getDatabasePlatform()->getName()) {
132
            // return version should be like "PostgreSQL 9.5.4 on x86_64-apple-darwin15.6.0, compiled by Apple LLVM version 8.0.0 (clang-800.0.38), 64-bit"
133
            $version = $doctrineManager->getConnection()->query('SELECT version();')->fetchColumn();
134
135
            preg_match('/PostgreSQL ([0-9\.]+)/i', $version, $matches);
136
137
            if (isset($matches[1]) & version_compare($matches[1], '9.2.0', '<')) {
138
                $fulfilled = false;
139
                $status = '<error>ERROR!</error>';
140
                $help = 'PostgreSQL should be greater than 9.1 (actual version: ' . $matches[1] . ')';
141
            }
142
        }
143
144
        $rows[] = [$label, $status, $help];
145
146
        foreach ($this->functionExists as $functionRequired) {
147
            $label = '<comment>' . $functionRequired . '</comment>';
148
            $status = '<info>OK!</info>';
149
            $help = '';
150
151
            if (!\function_exists($functionRequired)) {
152
                $fulfilled = false;
153
                $status = '<error>ERROR!</error>';
154
                $help = 'You need the ' . $functionRequired . ' function activated';
155
            }
156
157
            $rows[] = [$label, $status, $help];
158
        }
159
160
        $this->io->table(['Checked', 'Status', 'Recommendation'], $rows);
161
162
        if (!$fulfilled) {
163
            throw new \RuntimeException('Some system requirements are not fulfilled. Please check output messages and fix them.');
164
        }
165
166
        $this->io->success('Success! Your system can run wallabag properly.');
167
168
        return $this;
169
    }
170
171
    protected function setupDatabase()
172
    {
173
        $this->io->section('Step 2 of 4: Setting up database.');
174
175
        // user want to reset everything? Don't care about what is already here
176
        if (true === $this->defaultInput->getOption('reset')) {
177
            $this->io->text('Dropping database, creating database and schema, clearing the cache');
178
179
            $this
180
                ->runCommand('doctrine:database:drop', ['--force' => true])
181
                ->runCommand('doctrine:database:create')
182
                ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
183
                ->runCommand('cache:clear')
184
            ;
185
186
            $this->io->newLine();
187
188
            return $this;
189
        }
190
191
        if (!$this->isDatabasePresent()) {
192
            $this->io->text('Creating database and schema, clearing the cache');
193
194
            $this
195
                ->runCommand('doctrine:database:create')
196
                ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
197
                ->runCommand('cache:clear')
198
            ;
199
200
            $this->io->newLine();
201
202
            return $this;
203
        }
204
205
        if ($this->io->confirm('It appears that your database already exists. Would you like to reset it?', false)) {
206
            $this->io->text('Dropping database, creating database and schema...');
207
208
            $this
209
                ->runCommand('doctrine:database:drop', ['--force' => true])
210
                ->runCommand('doctrine:database:create')
211
                ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
212
            ;
213
        } elseif ($this->isSchemaPresent()) {
214
            if ($this->io->confirm('Seems like your database contains schema. Do you want to reset it?', false)) {
215
                $this->io->text('Dropping schema and creating schema...');
216
217
                $this
218
                    ->runCommand('doctrine:schema:drop', ['--force' => true])
219
                    ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
220
                ;
221
            }
222
        } else {
223
            $this->io->text('Creating schema...');
224
225
            $this
226
                ->runCommand('doctrine:migrations:migrate', ['--no-interaction' => true])
227
            ;
228
        }
229
230
        $this->io->text('Clearing the cache...');
231
        $this->runCommand('cache:clear');
232
233
        $this->io->newLine();
234
        $this->io->text('<info>Database successfully setup.</info>');
235
236
        return $this;
237
    }
238
239
    protected function setupAdmin()
240
    {
241
        $this->io->section('Step 3 of 4: Administration setup.');
242
243
        if (!$this->io->confirm('Would you like to create a new admin user (recommended)?', true)) {
244
            return $this;
245
        }
246
247
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
248
249
        $userManager = $this->getContainer()->get('fos_user.user_manager');
250
        $user = $userManager->createUser();
251
252
        $user->setUsername($this->io->ask('Username', 'wallabag'));
253
254
        $question = new Question('Password', 'wallabag');
255
        $question->setHidden(true);
256
        $user->setPlainPassword($this->io->askQuestion($question));
257
258
        $user->setEmail($this->io->ask('Email', '[email protected]'));
259
260
        $user->setEnabled(true);
261
        $user->addRole('ROLE_SUPER_ADMIN');
262
263
        $em->persist($user);
264
265
        // dispatch a created event so the associated config will be created
266
        $event = new UserEvent($user);
267
        $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
268
269
        $this->io->text('<info>Administration successfully setup.</info>');
270
271
        return $this;
272
    }
273
274
    protected function setupConfig()
275
    {
276
        $this->io->section('Step 4 of 4: Config setup.');
277
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
278
279
        // cleanup before insert new stuff
280
        $em->createQuery('DELETE FROM WallabagCoreBundle:InternalSetting')->execute();
281
        $em->createQuery('DELETE FROM WallabagCoreBundle:IgnoreOriginInstanceRule')->execute();
282
283
        foreach ($this->getContainer()->getParameter('wallabag_core.default_internal_settings') as $setting) {
284
            $newSetting = new InternalSetting();
285
            $newSetting->setName($setting['name']);
286
            $newSetting->setValue($setting['value']);
287
            $newSetting->setSection($setting['section']);
288
            $em->persist($newSetting);
289
        }
290
291
        foreach ($this->getContainer()->getParameter('wallabag_core.default_ignore_origin_instance_rules') as $ignore_origin_instance_rule) {
292
            $newIgnoreOriginInstanceRule = new IgnoreOriginInstanceRule();
293
            $newIgnoreOriginInstanceRule->setRule($ignore_origin_instance_rule['rule']);
294
            $em->persist($newIgnoreOriginInstanceRule);
295
        }
296
297
        $em->flush();
298
299
        $this->io->text('<info>Config successfully setup.</info>');
300
301
        return $this;
302
    }
303
304
    /**
305
     * Run a command.
306
     *
307
     * @param string $command
308
     * @param array  $parameters Parameters to this command (usually 'force' => true)
309
     */
310
    protected function runCommand($command, $parameters = [])
311
    {
312
        $parameters = array_merge(
313
            ['command' => $command],
314
            $parameters,
315
            [
316
                '--no-debug' => true,
317
                '--env' => $this->defaultInput->getOption('env') ?: 'dev',
318
            ]
319
        );
320
321
        if ($this->defaultInput->getOption('no-interaction')) {
322
            $parameters = array_merge($parameters, ['--no-interaction' => true]);
323
        }
324
325
        $this->getApplication()->setAutoExit(false);
326
327
        $output = new BufferedOutput();
328
        $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output);
329
330
        // PDO does not always close the connection after Doctrine commands.
331
        // See https://github.com/symfony/symfony/issues/11750.
332
        $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
333
334
        if (0 !== $exitCode) {
335
            $this->getApplication()->setAutoExit(true);
336
337
            throw new \RuntimeException('The command "' . $command . "\" generates some errors: \n\n" . $output->fetch());
338
        }
339
340
        return $this;
341
    }
342
343
    /**
344
     * Check if the database already exists.
345
     *
346
     * @return bool
347
     */
348
    private function isDatabasePresent()
349
    {
350
        $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
351
        $databaseName = $connection->getDatabase();
352
353
        try {
354
            $schemaManager = $connection->getSchemaManager();
355
        } catch (\Exception $exception) {
356
            // mysql & sqlite
357
            if (false !== strpos($exception->getMessage(), sprintf("Unknown database '%s'", $databaseName))) {
358
                return false;
359
            }
360
361
            // pgsql
362
            if (false !== strpos($exception->getMessage(), sprintf('database "%s" does not exist', $databaseName))) {
363
                return false;
364
            }
365
366
            throw $exception;
367
        }
368
369
        // custom verification for sqlite, since `getListDatabasesSQL` doesn't work for sqlite
370
        if ('sqlite' === $schemaManager->getDatabasePlatform()->getName()) {
371
            $params = $this->getContainer()->get('doctrine.dbal.default_connection')->getParams();
372
373
            if (isset($params['path']) && file_exists($params['path'])) {
374
                return true;
375
            }
376
377
            return false;
378
        }
379
380
        try {
381
            return \in_array($databaseName, $schemaManager->listDatabases(), true);
382
        } catch (\Doctrine\DBAL\Exception\DriverException $e) {
383
            // it means we weren't able to get database list, assume the database doesn't exist
384
385
            return false;
386
        }
387
    }
388
389
    /**
390
     * Check if the schema is already created.
391
     * If we found at least oen table, it means the schema exists.
392
     *
393
     * @return bool
394
     */
395
    private function isSchemaPresent()
396
    {
397
        $schemaManager = $this->getContainer()->get('doctrine')->getManager()->getConnection()->getSchemaManager();
398
399
        return \count($schemaManager->listTableNames()) > 0 ? true : false;
400
    }
401
}
402