Completed
Push — master ( e6372b...4cf7af )
by Gareth
20:29 queued 05:42
created

TldrCommand::execute()   B

Complexity

Conditions 3
Paths 7

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 1
Metric Value
dl 0
loc 24
c 8
b 0
f 1
rs 8.9713
cc 3
eloc 15
nc 7
nop 2
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 20 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 Stash\Driver\FileSystem;
11
use Stash\Pool;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use GarethEllis\Tldr\Fetcher\RemoteFetcher;
17
use GuzzleHttp\Client as Http;
18
use GarethEllis\Tldr\Fetcher\Exception\PageNotFoundException;
19
20
class TldrCommand extends Command
21
{
22
    protected function configure()
23
    {
24
        $this
25
            ->setName('tldr')
26
            ->setDescription('Perform a look-up against the TLDR man pages project')
27
            ->addArgument(
28
                'page',
29
                InputArgument::REQUIRED,
30
                'Please specify a man page to look-up.'
31
            )
32
        ;
33
    }
34
35
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        $http = new Http();
38
        $fetcher = new RemoteFetcher($http);
39
40
        $driver = new FileSystem();
41
        $pool = new Pool($driver);
42
        $cache = new StashAdapter($pool);
43
        $fetcher = new CacheFetcher($fetcher, $cache);
44
45
        try {
46
47
            $page = $fetcher->fetchPage($input->getArgument("page"));
48
            $pageOutput = new PageOutput($output);
49
            $pageOutput->write($page);
50
51
        } catch (PageNotFoundException $e) {
52
53
            return $output->writeln("<comment>Page not found</comment>");
54
        } catch (RemoteFetcherException $e) {
55
56
            return $output->writeln("<error>Unable to connect to repository :-(</error>");
57
        }
58
    }
59
}
60