StashAdapter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 62
c 0
b 0
f 0
wmc 8
lcom 1
cbo 5
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A readFromCache() 0 17 3
A writeToCache() 0 6 1
A deleteFromCache() 0 7 1
A flushCache() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace GarethEllis\Tldr\Cache;
5
6
use GarethEllis\Tldr\Cache\Exception\InvalidCacheDriverException;
7
use GarethEllis\Tldr\Fetcher\Exception\PageNotFoundException;
8
use GarethEllis\Tldr\Page\TldrPage;
9
use Stash\Driver\Ephemeral;
10
use Stash\Pool;
11
12
class StashAdapter implements CacheAdapterInterface
13
{
14
    /**
15
     * @var Pool
16
     */
17
    private $pool;
18
19
    /**
20
     * CacheReader constructor.
21
     */
22
    public function __construct(Pool $pool)
23
    {
24
        if ($pool->getDriver() instanceof Ephemeral) {
25
            throw new InvalidCacheDriverException("Ephemeral cannot be used as a cache driver in this app");
26
        }
27
        $this->pool = $pool;
28
    }
29
30
    public function readFromCache(String $platform, String $pageName): TldrPage
31
    {
32
        $item = $this->pool->getItem($platform . "/" . $pageName);
33
        $page = $item->get();
34
35
        if ($item->isMiss()) {
36
37
            $item = $this->pool->getItem("common/" . $pageName);
38
            $page = $item->get();
39
40
            if ($item->isMiss()) {
41
                throw new PageNotFoundException();
42
            }
43
        }
44
45
        return $page;
46
    }
47
48
    public function writeToCache(TldrPage $page)
49
    {
50
        $item = $this->pool->getItem($page->getPlatform() . "/" . $page->getName());
51
        $item->lock();
52
        $item->set($page);
53
    }
54
55
    public function deleteFromCache(String $platform, String $pageName)
56
    {
57
        $item = $this->pool->getItem($platform . "/" . $pageName);
58
        $item->clear();
59
        $item = $this->pool->getItem("common/" . $pageName);
60
        $item->clear();
61
    }
62
63
64
    /**
65
     * Flush the stash cache
66
     *
67
     * @return void
68
     */
69
    public function flushCache()
70
    {
71
        $this->pool->flush();
72
    }
73
}
74