Completed
Push — master ( 5c5205...f459ef )
by Gareth
15:29
created

StashAdapter::deleteFromCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
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 12 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\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
     * TODO this currently doesn't seem to work, it throws warnings on rmdir
68
     *
69
     * @return void
70
     */
71
    public function flushCache()
72
    {
73
        $this->pool->flush();
74
    }
75
76
77
}
78