Completed
Push — 2.x-dev-kit ( 971030 )
by
unknown
07:52
created

SitemapGeneratorCommand::execute()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 60
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 60
rs 7.4661
cc 7
eloc 32
nc 18
nop 2

How to fix   Long Method   

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
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\SeoBundle\Command;
13
14
use Exporter\Handler;
15
use Exporter\Writer\SitemapWriter;
16
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Filesystem\Filesystem;
22
use Symfony\Component\Finder\Finder;
23
24
/**
25
 * Create a sitemap.
26
 *
27
 * @author Thomas Rabaix <[email protected]>
28
 */
29
class SitemapGeneratorCommand extends ContainerAwareCommand
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function configure()
35
    {
36
        $this->setName('sonata:seo:sitemap');
37
38
        $this->addArgument('folder', InputArgument::REQUIRED, 'The folder to store the sitemap.xml file');
39
        $this->addArgument('host', InputArgument::REQUIRED, 'Set the host');
40
        $this->addOption('scheme', null, InputOption::VALUE_OPTIONAL, 'Set the scheme', 'http');
41
        $this->addOption('baseurl', null, InputOption::VALUE_OPTIONAL, 'Set the base url', '');
42
        $this->addOption('sitemap_path', null, InputOption::VALUE_OPTIONAL, 'Set the sitemap relative path (if in a specific folder)', '');
43
44
        $this->setDescription('Create a sitemap');
45
        $this->setHelp(<<<'EOT'
46
The <info>sonata:seo:sitemap</info> command create new sitemap files (index + sitemap).
47
48
EOT
49
        );
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
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