| Conditions | 12 |
| Paths | 14 |
| Total Lines | 93 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 35 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 36 | { |
||
| 37 | if (!$this->lock()) { |
||
| 38 | $output->writeln('The command is already running in another process.'); |
||
| 39 | |||
| 40 | return 0; |
||
| 41 | } |
||
| 42 | |||
| 43 | $io = new SymfonyStyle($input, $output); |
||
| 44 | |||
| 45 | $dumpSql = true === $input->getOption('dump-sql'); |
||
| 46 | $force = true === $input->getOption('force'); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var AuditManager |
||
| 50 | */ |
||
| 51 | $manager = $this->container->get('dh_doctrine_audit.manager'); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var AuditReader |
||
| 55 | */ |
||
| 56 | $reader = $this->container->get('dh_doctrine_audit.reader'); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var UpdateHelper |
||
| 60 | */ |
||
| 61 | $updater = new UpdateHelper($manager, $reader); |
||
| 62 | |||
| 63 | $auditEntityManager = $manager->getConfiguration()->getEntityManager(); |
||
| 64 | |||
| 65 | $sqls = $updater->getUpdateAuditSchemaSql(); |
||
| 66 | |||
| 67 | if (empty($sqls)) { |
||
| 68 | $io->success('Nothing to update.'); |
||
| 69 | |||
| 70 | $this->release(); |
||
| 71 | |||
| 72 | return 0; |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($dumpSql) { |
||
| 76 | $io->text('The following SQL statements will be executed:'); |
||
| 77 | $io->newLine(); |
||
| 78 | |||
| 79 | foreach ($sqls as $sql) { |
||
| 80 | $io->text(sprintf(' %s;', $sql)); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($force) { |
||
| 85 | if ($dumpSql) { |
||
| 86 | $io->newLine(); |
||
| 87 | } |
||
| 88 | $io->text('Updating database schema...'); |
||
| 89 | $io->newLine(); |
||
| 90 | |||
| 91 | foreach ($sqls as $sql) { |
||
| 92 | try { |
||
| 93 | $statement = $auditEntityManager->getConnection()->prepare($sql); |
||
| 94 | $statement->execute(); |
||
| 95 | } catch (\Exception $e) { |
||
| 96 | // something bad happened here :/ |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | $pluralization = (1 === \count($sqls)) ? 'query was' : 'queries were'; |
||
| 101 | |||
| 102 | $io->text(sprintf(' <info>%s</info> %s executed', \count($sqls), $pluralization)); |
||
| 103 | $io->success('Database schema updated successfully!'); |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($dumpSql || $force) { |
||
| 107 | $this->release(); |
||
| 108 | |||
| 109 | return 0; |
||
| 110 | } |
||
| 111 | |||
| 112 | $io->caution('This operation should not be executed in a production environment!'); |
||
| 113 | |||
| 114 | $io->text( |
||
| 115 | [ |
||
| 116 | sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', \count($sqls)), |
||
| 117 | '', |
||
| 118 | 'Please run the operation by passing one - or both - of the following options:', |
||
| 119 | '', |
||
| 120 | sprintf(' <info>%s --force</info> to execute the command', $this->getName()), |
||
| 121 | sprintf(' <info>%s --dump-sql</info> to dump the SQL statements to the screen', $this->getName()), |
||
| 122 | ] |
||
| 123 | ); |
||
| 124 | |||
| 125 | $this->release(); |
||
| 126 | |||
| 127 | return 1; |
||
| 128 | } |
||
| 140 |