Conditions | 11 |
Paths | 23 |
Total Lines | 97 |
Code Lines | 48 |
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 |
||
50 | protected function execute(InputInterface $input, OutputInterface $output) |
||
51 | { |
||
52 | if (null === $this->container) { |
||
53 | throw new RuntimeException('No container.'); |
||
54 | } |
||
55 | |||
56 | if (!$this->lock()) { |
||
57 | $output->writeln('The command is already running in another process.'); |
||
58 | |||
59 | return 0; |
||
60 | } |
||
61 | |||
62 | $io = new SymfonyStyle($input, $output); |
||
63 | |||
64 | $dumpSql = true === $input->getOption('dump-sql'); |
||
65 | $force = true === $input->getOption('force'); |
||
66 | |||
67 | /** |
||
68 | * @var TransactionManager |
||
69 | */ |
||
70 | $manager = $this->container->get('dh_doctrine_audit.manager'); |
||
71 | |||
72 | /** |
||
73 | * @var Reader |
||
74 | */ |
||
75 | $reader = $this->container->get('dh_doctrine_audit.reader'); |
||
76 | |||
77 | /** |
||
78 | * @var \DH\DoctrineAuditBundle\Updater\UpdateManager |
||
79 | */ |
||
80 | $updater = new UpdateManager($manager, $reader); |
||
81 | |||
82 | $sqls = $updater->getUpdateAuditSchemaSql(); |
||
83 | |||
84 | if (empty($sqls)) { |
||
85 | $io->success('Nothing to update.'); |
||
86 | |||
87 | $this->release(); |
||
88 | |||
89 | return 0; |
||
90 | } |
||
91 | |||
92 | if ($dumpSql) { |
||
93 | $io->text('The following SQL statements will be executed:'); |
||
94 | $io->newLine(); |
||
95 | |||
96 | foreach ($sqls as $sql) { |
||
97 | $io->text(sprintf(' %s;', $sql)); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | if ($force) { |
||
102 | if ($dumpSql) { |
||
103 | $io->newLine(); |
||
104 | } |
||
105 | $io->text('Updating database schema...'); |
||
106 | $io->newLine(); |
||
107 | |||
108 | $progressBar = new ProgressBar($output, \count($sqls)); |
||
109 | $progressBar->start(); |
||
110 | |||
111 | $updater->updateAuditSchema($sqls, static function (array $progress) use ($progressBar): void { |
||
112 | $progressBar->advance(); |
||
113 | }); |
||
114 | |||
115 | $progressBar->finish(); |
||
116 | |||
117 | $io->newLine(2); |
||
118 | |||
119 | $pluralization = (1 === \count($sqls)) ? 'query was' : 'queries were'; |
||
120 | |||
121 | $io->text(sprintf(' <info>%s</info> %s executed', \count($sqls), $pluralization)); |
||
122 | $io->success('Database schema updated successfully!'); |
||
123 | } |
||
124 | |||
125 | if ($dumpSql || $force) { |
||
126 | $this->release(); |
||
127 | |||
128 | return 0; |
||
129 | } |
||
130 | |||
131 | $io->caution('This operation should not be executed in a production environment!'); |
||
132 | |||
133 | $io->text( |
||
134 | [ |
||
135 | sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', \count($sqls)), |
||
136 | '', |
||
137 | 'Please run the operation by passing one - or both - of the following options:', |
||
138 | '', |
||
139 | sprintf(' <info>%s --force</info> to execute the command', $this->getName()), |
||
140 | sprintf(' <info>%s --dump-sql</info> to dump the SQL statements to the screen', $this->getName()), |
||
141 | ] |
||
142 | ); |
||
143 | |||
144 | $this->release(); |
||
145 | |||
146 | return 1; |
||
147 | } |
||
149 |