Completed
Push — master ( 5c5205...f459ef )
by Gareth
15:29
created

TldrCommand::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 0
loc 33
rs 8.8571
c 3
b 0
f 1
cc 1
eloc 26
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use GarethEllis\Tldr\Fetcher\RemoteFetcher;
19
use GuzzleHttp\Client as Http;
20
use GarethEllis\Tldr\Fetcher\Exception\PageNotFoundException;
21
22
class TldrCommand extends Command
23
{
24
    use OperatingSystemTrait;
25
26
    protected function configure()
27
    {
28
        $this
29
            ->setName('tldr')
30
            ->setDescription('Perform a look-up against the TLDR man pages project')
31
            ->addArgument(
32
                'page',
33
                InputArgument::OPTIONAL,
34
                'Please specify a man page to look-up.'
35
            )
36
            ->addOption(
37
                'refresh-cache',
38
                'r',
39
                InputOption::VALUE_NONE,
40
                "Fetch command from remote repository and refresh cache",
41
                null
42
            )
43
            ->addOption(
44
                'os',
45
                null,
46
                InputOption::VALUE_OPTIONAL,
47
                "Operating system to search for: linux, osx or sunos",
48
                null
49
            )
50
            ->addOption(
51
                'flush-cache',
52
                'f',
53
                InputOption::VALUE_NONE,
54
                "Delete all cached pages",
55
                null
56
            )
57
        ;
58
    }
59
60
    protected function execute(InputInterface $input, OutputInterface $output)
61
    {
62
        $http = new Http();
63
64
        $options = [];
65
        if ($input->getOption('os')) {
66
            $options["operatingSystem"] = $input->getOption('os');
67
        }
68
        $fetcher = new RemoteFetcher($http, $options);
69
70
        $driver = new FileSystem([
71
            "path" => sys_get_temp_dir()
72
        ]);
73
        $pool = new Pool($driver);
74
        $cache = new StashAdapter($pool);
75
        $fetcher = new CacheFetcher($fetcher, $cache, $options);
76
77
        if ($input->getOption('flush-cache')) {
78
            $cache->flushCache();
79
        }
80
81
        if (!$input->getArgument("page")) {
82
            return $output->write("TODO"); //TODO output help page here
83
        }
84
85
        if ($input->getOption('refresh-cache')) {
86
            $operatingSystem = $input->getOption('os') ?: $this->getOperatingSystem();
87
            $cache->deleteFromCache($operatingSystem, $input->getArgument("page"));
88
        }
89
90
        try {
91
92
            $page = $fetcher->fetchPage($input->getArgument("page"));
93
            $pageOutput = new PageOutput($output);
94
            $pageOutput->write($page);
95
96
        } catch (PageNotFoundException $e) {
97
98
            return $output->writeln("<comment>Page not found</comment>");
99
        } catch (RemoteFetcherException $e) {
100
101
            return $output->writeln("<error>Unable to connect to repository :-(</error>");
102
        }
103
    }
104
}
105