CheckCommand::execute()   C
last analyzed

Complexity

Conditions 12
Paths 66

Size

Total Lines 58
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 0 Features 2
Metric Value
c 10
b 0
f 2
dl 0
loc 58
rs 6.5331
cc 12
eloc 33
nc 66
nop 2

How to fix   Long Method    Complexity   

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
namespace SiteChecker\Commands;
4
5
use Psr\Log\LogLevel;
6
use SiteChecker\Asset;
7
use SiteChecker\Config;
8
use SiteChecker\ConsoleObserver;
9
use SiteChecker\SiteChecker;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Logger\ConsoleLogger;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputOption;
17
18
define('CONFIG_PATH', '/../../../config/app.json');
19
20
/**
21
 * Class CheckCommand
22
 * @package SiteChecker\Commands
23
 */
24
class CheckCommand extends Command
25
{
26
27
    protected function configure()
28
    {
29
        $this->setName("site-checker:check")
30
            ->setDescription("Display the fibonacci numbers between 2 given numbers")
31
            ->setDefinition([
32
                new InputArgument('site', InputArgument::REQUIRED),
33
                new InputOption('check-external', 'e', InputOption::VALUE_NONE,
34
                    'Check external links'),
35
                new InputOption('log-success', 's', InputOption::VALUE_NONE,
36
                    'Log successful page loads'),
37
                new InputOption('full-html', 'f', InputOption::VALUE_NONE,
38
                    'Show full html tag of element in log'),
39
            ])
40
            ->setHelp(<<<EOT
41
Checks a site for broken links and missing files (CSS, js, images)
42
43
Usage:
44
45
<info> sitechecker http://site.url</info>
46
EOT
47
            );
48
    }
49
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
53
        $headerStyle = new OutputFormatterStyle('white', 'green',
54
            array('bold'));
55
        $output->getFormatter()->setStyle('header', $headerStyle);
56
57
        $site = $input->getArgument('site');
58
        $output->writeln('<header>Checking ' . $site . '... </header>');
59
        $site = new Asset($site);
60
61
        $verbosityLevelMap = [];
62
        if ($input->getOption('log-success')) {
63
            $verbosityLevelMap = array(
64
                LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL,
65
            );
66
        }
67
68
        $logger = new ConsoleLogger($output, $verbosityLevelMap);
69
        $observer = new ConsoleObserver($logger);
70
        $siteChecker = SiteChecker::create($observer);
71
72
        if ($input->getOptions()) {
73
            $config = new Config();
74
75
            // Load configuration from file if any
76
            $conf = json_decode(file_get_contents(__DIR__ . CONFIG_PATH));
77
            $siteConf = isset($conf->{$site->host}) ? $conf->{$site->host} : null;
78
79
            if (!is_null($siteConf) || !is_null($conf)) {
80
                foreach ((array)$config as $key => $value) {
81
                    // First use general values
82
                    if (!empty($conf->{$key})) {
83
                        $config->{$key} = $conf->{$key};
84
                    }
85
                    // Then rewrite them with site-specific
86
                    if (!empty($siteConf->{$key})) {
87
                        $config->{$key} = $siteConf->{$key};
88
                    }
89
                }
90
            }
91
92
            if ($input->getOption('log-success')) {
93
                $config->showOnlyProblems = false;
94
            }
95
96
            if ($input->getOption('check-external')) {
97
                $config->checkExternal = true;
98
            }
99
100
            if ($input->getOption('full-html')) {
101
                $config->showFullTags = true;
102
            }
103
            $siteChecker->setConfig($config);
104
            $observer->setConfig($config);
105
        }
106
        $siteChecker->check($site);
107
    }
108
}
109