Conditions | 7 |
Paths | 18 |
Total Lines | 60 |
Code Lines | 32 |
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 |
||
55 | public function execute(InputInterface $input, OutputInterface $output) |
||
56 | { |
||
57 | $this->getContainer()->get('router')->getContext()->setHost($input->getArgument('host')); |
||
58 | $this->getContainer()->get('router')->getContext()->setScheme($input->getOption('scheme')); |
||
59 | $this->getContainer()->get('router')->getContext()->setBaseUrl($input->getOption('baseurl')); |
||
60 | |||
61 | $tempFolder = sys_get_temp_dir().'/sonata_sitemap_'.md5(__DIR__); |
||
62 | |||
63 | $fs = new Filesystem(); |
||
64 | |||
65 | // step 1 |
||
66 | $output->writeln(sprintf('Creating temporary folder: %s', $tempFolder)); |
||
67 | |||
68 | if ($fs->exists($tempFolder)) { |
||
69 | $output->writeln('<error>The temporary folder already exists</error>'); |
||
70 | $output->writeln('<error>If the task is not running please delete this folder</error>'); |
||
71 | |||
72 | return 1; |
||
73 | } |
||
74 | |||
75 | $fs->mkdir($tempFolder); |
||
76 | |||
77 | // step 2 |
||
78 | $manager = $this->getContainer()->get('sonata.seo.sitemap.manager'); |
||
79 | |||
80 | // step 3 |
||
81 | $output->writeln(sprintf('Generating sitemap - this can take a while')); |
||
82 | foreach ($manager as $group => $sitemap) { |
||
83 | $write = new SitemapWriter($tempFolder, $group, $sitemap->types, false); |
||
84 | |||
85 | try { |
||
86 | Handler::create($sitemap->sources, $write)->export(); |
||
87 | } catch (\Exception $e) { |
||
88 | $fs->remove($tempFolder); |
||
89 | |||
90 | throw $e; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | // generate global sitemap index |
||
95 | $appendPath = $input->hasOption('sitemap_path') ? $input->getOption('sitemap_path') : $input->getOption('baseurl'); |
||
96 | SitemapWriter::generateSitemapIndex($tempFolder, sprintf('%s://%s%s', $input->getOption('scheme'), $input->getArgument('host'), $appendPath), 'sitemap*.xml', 'sitemap.xml'); |
||
97 | |||
98 | // step 4 |
||
99 | $output->writeln(sprintf('Moving temporary file to %s ...', $input->getArgument('folder'))); |
||
100 | |||
101 | $oldFiles = Finder::create()->files()->name('sitemap*.xml')->in($input->getArgument('folder')); |
||
102 | foreach ($oldFiles as $file) { |
||
103 | $fs->remove($file->getRealPath()); |
||
104 | } |
||
105 | |||
106 | $newFiles = Finder::create()->files()->name('sitemap*.xml')->in($tempFolder); |
||
107 | foreach ($newFiles as $file) { |
||
108 | $fs->rename($file->getRealPath(), sprintf('%s/%s', $input->getArgument('folder'), $file->getFilename())); |
||
109 | } |
||
110 | |||
111 | $fs->remove($tempFolder); |
||
112 | |||
113 | $output->writeln('<info>done!</info>'); |
||
114 | } |
||
115 | } |
||
116 |