Completed
Push — master ( 224ae7...80285a )
by Gareth
13:38
created

RemoteFetcher::fetchPage()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 36
rs 8.8571
cc 3
eloc 21
nc 3
nop 1
1
<?php
2
3
namespace GarethEllis\Tldr\Fetcher;
4
5
use GarethEllis\Tldr\Fetcher\Exception\PageNotFoundException;
6
use GarethEllis\Tldr\Page\TldrPage;
7
use GuzzleHttp\Client;
8
use GarethEllis\Tldr\Cache\CacheWriterInterface;
9
10
/**
11
 * Class RemoteFetcher
12
 *
13
 * Fetches a page from the remote repository and optionally caches the result
14
 *
15
 * @package GarethEllis\Tldr\Fetcher
16
 */
17
class RemoteFetcher implements PageFetcherInterface
18
{
19
20
    protected $http;
21
22
    /**
23
     * @var string The base URL of the JSON index containing a list of commands
24
     */
25
    protected $baseUrl = "https://api.github.com/repos/tldr-pages/tldr/contents/pages/index.json?ref=master";
26
27
    /**
28
     * @var string URL template for fetching an individual page
29
     */
30
    protected $pageInfoUrlTemplate =
31
        "https://api.github.com/repos/tldr-pages/tldr/contents/pages/{os}/{command}.md?ref=master";
32
33
    /**
34
     * @var CacheWriterInterface
35
     */
36
    private $cacheWriter;
37
38
    /**
39
     * RemoteFetcher constructor.
40
     *
41
     * @param \GuzzleHttp\Client $http
42
     */
43
    public function __construct(Client $http, CacheWriterInterface $cacheWriter = null)
44
    {
45
        $this->http = $http;
46
        $this->cacheWriter = $cacheWriter;
47
    }
48
49
    /**
50
     * Fetch a page from remote repository
51
     *
52
     * @param string $page
0 ignored issues
show
Bug introduced by
There is no parameter named $page. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
53
     * @return string
54
     * @throws PageNotFoundException
55
     */
56
    public function fetchPage(String $pageName): TldrPage
57
    {
58
        $response = $this->http->get($this->baseUrl);
59
60
        $contents = json_decode($response->getBody()->getContents(), true);
61
        $pages = base64_decode($contents["content"]);
62
        $json = json_decode($pages, true);
63
        $pages = $json["commands"];
64
65
        $pages = array_filter($pages, function ($foundPage) use ($pageName) {
66
            return $foundPage["name"] === $pageName;
67
        });
68
69
        //working on assumption that only one page has been found!
70
        if (empty($pages)) {
71
            throw new PageNotFoundException;
72
        }
73
74
        $page = $pages["0"];
75
        $os = $page["platform"][0];
76
        $url = str_replace("{os}", $os, $this->pageInfoUrlTemplate);
77
        $url = str_replace("{command}", $page["name"], $url);
78
79
        $response = $this->http->get($url);
80
        $contents = json_decode($response->getBody()->getContents(), true);
81
82
        $pageContent = base64_decode($contents["content"]);
83
84
        $page = new TldrPage($pageName, $os, $pageContent);
85
86
        if ($this->cacheWriter) {
87
            $this->cacheWriter->writeToCache($page);
88
        }
89
90
        return $page;
91
    }
92
}
93