TldrCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 14
Bugs 2 Features 2
Metric Value
wmc 11
c 14
b 2
f 2
lcom 1
cbo 12
dl 0
loc 105
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 33 1
C execute() 0 39 8
A outputHelp() 0 6 1
A getCacheAdapterInstance() 0 9 1
1
<?php
2
declare(strict_types=1);
3
4
namespace GarethEllis\Tldr\Console\Command;
5
6
use GarethEllis\Tldr\Cache\StashAdapter;
7
use GarethEllis\Tldr\Console\Output\PageOutput;
8
use GarethEllis\Tldr\Fetcher\CacheFetcher;
9
use GarethEllis\Tldr\Fetcher\Exception\RemoteFetcherException;
10
use GarethEllis\Tldr\Fetcher\OperatingSystemTrait;
11
use Stash\Driver\FileSystem;
12
use Stash\Pool;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Command\HelpCommand;
15
use Symfony\Component\Console\Helper\DescriptorHelper;
16
use Symfony\Component\Console\Input\ArrayInput;
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 GarethEllis\Tldr\Fetcher\RemoteFetcher;
22
use GuzzleHttp\Client as Http;
23
use GarethEllis\Tldr\Fetcher\Exception\PageNotFoundException;
24
25
class TldrCommand extends Command
26
{
27
    use OperatingSystemTrait;
28
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('tldr')
33
            ->setDescription('Perform a look-up against the TLDR man pages project')
34
            ->addArgument(
35
                'page',
36
                InputArgument::OPTIONAL,
37
                'The TLDR man page to look-up'
38
            )
39
            ->addOption(
40
                'refresh-cache',
41
                'r',
42
                InputOption::VALUE_NONE,
43
                "Fetch command from remote repository and refresh cache",
44
                null
45
            )
46
            ->addOption(
47
                'os',
48
                'o',
49
                InputOption::VALUE_OPTIONAL,
50
                "Operating system to search for: linux, osx or sunos",
51
                null
52
            )
53
            ->addOption(
54
                'flush-cache',
55
                'f',
56
                InputOption::VALUE_NONE,
57
                "Delete all cached pages",
58
                null
59
            )
60
        ;
61
    }
62
63
64
    protected function execute(InputInterface $input, OutputInterface $output)
65
    {
66
        $http = new Http();
67
68
        $options = [];
69
        if ($input->getOption('os')) {
70
            $options["operatingSystem"] = $input->getOption('os');
71
        }
72
        $fetcher = new RemoteFetcher($http, $options);
0 ignored issues
show
Unused Code introduced by
The call to RemoteFetcher::__construct() has too many arguments starting with $options.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
73
74
        $cache = $this->getCacheAdapterInstance();
75
        $fetcher = new CacheFetcher($fetcher, $cache, $options);
0 ignored issues
show
Unused Code introduced by
The call to CacheFetcher::__construct() has too many arguments starting with $options.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
76
77
        if ($input->getOption('flush-cache')) {
78
            $cache->flushCache();
79
        }
80
81
        if (!$input->getArgument("page")) {
82
            return $this->outputHelp($input, $output);
83
        }
84
85
        if ($input->getOption('refresh-cache')) {
86
            $operatingSystem = $input->getOption('os') ?: $this->getOperatingSystem();
0 ignored issues
show
Bug introduced by
The call to getOperatingSystem() misses a required argument $options.

This check looks for function calls that miss required arguments.

Loading history...
87
            $cache->deleteFromCache($operatingSystem, $input->getArgument("page"));
88
        }
89
        try {
90
91
            $page = $fetcher->fetchPage($input->getArgument("page"));
92
            $pageOutput = new PageOutput($output);
93
            $pageOutput->write($page);
94
95
        } catch (PageNotFoundException $e) {
96
97
            return $output->writeln("<comment>Page not found</comment>");
98
        } catch (RemoteFetcherException $e) {
99
100
            return $output->writeln("<error>Unable to connect to repository :-(</error>");
101
        }
102
    }
103
104
    /**
105
     * @param InputInterface $input
106
     * @param OutputInterface $output
107
     * @return int
108
     * @throws \Symfony\Component\Console\Exception\ExceptionInterface
109
     */
110
    protected function outputHelp(InputInterface $input, OutputInterface $output)
111
    {
112
        $help = new HelpCommand();
113
        $help->setCommand($this);
114
        return $help->run($input, $output);
115
    }
116
117
    /**
118
     * @return StashAdapter
119
     */
120
    protected function getCacheAdapterInstance()
121
    {
122
        $driver = new FileSystem([
123
            "path" => sys_get_temp_dir() . "tldr-cache"
124
        ]);
125
        $pool = new Pool($driver);
126
        $cache = new StashAdapter($pool);
127
        return $cache;
128
    }
129
}
130