CacheFetcher   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 35
c 0
b 0
f 0
wmc 3
lcom 1
cbo 3
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fetchPage() 0 14 2
1
<?php
2
declare(strict_types=1);
3
4
namespace GarethEllis\Tldr\Fetcher;
5
6
use GarethEllis\Tldr\Cache\CacheAdapterInterface;
7
use GarethEllis\Tldr\Fetcher\Exception\PageNotFoundException;
8
use GarethEllis\Tldr\Page\TldrPage;
9
10
class CacheFetcher implements PageFetcherInterface
11
{
12
    use OperatingSystemTrait;
13
14
    /**
15
     * @var PageFetcherInterface
16
     */
17
    private $fetcher;
18
19
    /**
20
     * @var CacheAdapterInterface
21
     */
22
    private $cache;
23
24
    public function __construct(PageFetcherInterface $fetcher, CacheAdapterInterface $cache)
25
    {
26
        $this->fetcher = $fetcher;
27
        $this->cache = $cache;
28
    }
29
30
    public function fetchPage(string $pageName, array $options = []): TldrPage
31
    {
32
33
        try {
34
35
            return $this->cache->readFromCache($this->getOperatingSystem($options), $pageName);
36
        } catch (PageNotFoundException $e) {
37
38
            $page = $this->fetcher->fetchPage($pageName);
39
            $this->cache->writeToCache($page);
40
41
            return $page;
42
        }
43
    }
44
}
45