Completed
Push — master ( 5ed955...59fcd0 )
by Gareth
25:34 queued 10:42
created

TldrCommand::getCacheAdapterInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
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 25 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\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);
73
74
        $cache = $this->getCacheAdapterInstance();
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 $this->outputHelp($input, $output);
83
        }
84
85
        if ($input->getOption('refresh-cache')) {
86
            $operatingSystem = $input->getOption('os') ?: $this->getOperatingSystem();
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