CheckCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 12

Importance

Changes 11
Bugs 0 Features 3
Metric Value
wmc 13
c 11
b 0
f 3
lcom 0
cbo 12
dl 0
loc 85
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 22 1
C execute() 0 58 12
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