SitemapCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
/**
3
 * Mage Scan
4
 *
5
 * PHP version 5
6
 *
7
 * @category  MageScan
8
 * @package   MageScan
9
 * @author    Steve Robbins <[email protected]>
10
 * @copyright 2015 Steve Robbins
11
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
12
 * @link      https://github.com/steverobbins/magescan
13
 */
14
15
namespace MageScan\Command\Scan;
16
17
use MageScan\Check\Sitemap;
18
use MageScan\Request;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * Scan sitemap command
24
 *
25
 * @category  MageScan
26
 * @package   MageScan
27
 * @author    Steve Robbins <[email protected]>
28
 * @copyright 2015 Steve Robbins
29
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
30
 * @link      https://github.com/steverobbins/magescan
31
 */
32
class SitemapCommand extends AbstractCommand
33
{
34
    /**
35
     * Configure command
36
     *
37
     * @return void
38
     */
39
    protected function configure()
40
    {
41
        $this
42
            ->setName('scan:sitemap')
43
            ->setDescription('Check sitemap');
44
        parent::configure();
45
    }
46
47
    /**
48
     * Execute command
49
     *
50
     * @param InputInterface  $input
51
     * @param OutputInterface $output
52
     *
53
     * @return void
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $result = [];
58
        $url = $this->getSitemapUrl();
59
        if ($url === false) {
60
            $result[] = '<error>Sitemap is not declared in robots.txt</error>';
61
            $url = $this->request->getUrl() . 'sitemap.xml';
62
        } else {
63
            $result[] = '<info>Sitemap is declared in robots.txt</info>';
64
        }
65
        $request = new Request(
66
            $url,
67
            $this->input->getOption('insecure')
68
        );
69
        $response = $request->get();
70
        if ($response->getStatusCode() == 200) {
71
            $result[] = '<info>Sitemap is accessible:</info> ' . $url;
72
        } else {
73
            $result[] = '<error>Sitemap is not accessible:</error> ' . $url;
74
        }
75
        $this->out('Sitemap', $result);
76
    }
77
78
    /**
79
     * Parse the robots.txt text file to find the sitemap
80
     *
81
     * @return string|boolean
82
     */
83
    protected function getSitemapUrl()
84
    {
85
        $request = new Request(
86
            $this->request->getUrl(),
0 ignored issues
show
Bug introduced by
It seems like $this->request->getUrl() targeting MageScan\Request::getUrl() can also be of type boolean; however, MageScan\Request::__construct() does only seem to accept false|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
87
            $this->input->getOption('insecure')
88
        );
89
        $response = $request->get('robots.txt');
90
        $sitemap = new Sitemap;
91
        $sitemap->setRequest($this->request);
92
        return $sitemap->getSitemapFromRobotsTxt($response);
93
    }
94
}
95