Completed
Push — master ( 4ede59...8eb3b1 )
by Cedric
10:16
created

FlysystemLoader::getFileOrFail()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3.0261
1
<?php
2
3
namespace CedricZiel\TwigLoaderFlysystem;
4
5
use League\Flysystem\Filesystem;
6
use Twig_Error_Loader;
7
use Twig_LoaderInterface;
8
9
/**
10
 * Provides a template loader for twig that allows to use flysystem
11
 * instances to load templates.
12
 *
13
 * @package CedricZiel\TwigLoaderFlysystem
14
 */
15
class FlysystemLoader implements Twig_LoaderInterface
16
{
17
    /**
18
     * @var Filesystem
19
     */
20
    private $filesystem;
21
22
    /**
23
     * FlysystemLoader constructor.
24
     *
25
     * @param Filesystem $filesystem
26
     */
27 12
    public function __construct(Filesystem $filesystem)
28
    {
29 12
        $this->filesystem = $filesystem;
30 12
    }
31
32
    /**
33
     * Gets the source code of a template, given its name.
34
     *
35
     * @param string $name The name of the template to load
36
     *
37
     * @return string The template source code
38
     *
39
     * @throws Twig_Error_Loader When $name is not found
40
     */
41 6
    public function getSource($name)
42
    {
43 6
        $this->getFileOrFail($name);
44
45 3
        return $this->filesystem->read($name);
46
    }
47
48
    /**
49
     * Checks if the underlying flysystem contains a file of the given name.
50
     *
51
     * @param string $name
52
     *
53
     * @return \League\Flysystem\File|\League\Flysystem\Handler
54
     * @throws Twig_Error_Loader
55
     */
56 12
    protected function getFileOrFail($name)
57
    {
58 12
        if (!$this->filesystem->has($name)) {
59 3
            throw new Twig_Error_Loader('Template could not be found on the given filesystem');
60
        }
61
62 9
        $fileObject = $this->filesystem->get($name);
63 9
        if ($fileObject->isDir()) {
64
            throw new Twig_Error_Loader('Cannot use directory as template');
65
        }
66
67 9
        return $fileObject;
68
    }
69
70
    /**
71
     * Gets the cache key to use for the cache for a given template name.
72
     *
73
     * @param string $name The name of the template to load
74
     *
75
     * @return string The cache key
76
     *
77
     * @throws Twig_Error_Loader When $name is not found
78
     */
79 3
    public function getCacheKey($name)
80
    {
81 3
        $this->getFileOrFail($name);
82
83 3
        return $name;
84
    }
85
86
    /**
87
     * Returns true if the template is still fresh.
88
     *
89
     * @param string $name The template name
90
     * @param int    $time Timestamp of the last modification time of the
91
     *                     cached template
92
     *
93
     * @return bool true if the template is fresh, false otherwise
94
     *
95
     * @throws Twig_Error_Loader When $name is not found
96
     */
97 3
    public function isFresh($name, $time)
98
    {
99 3
        $object = $this->getFileOrFail($name);
100
101 3
        return (int)$time >= (int)$object->getTimestamp();
102
    }
103
}
104