FlysystemLoader::getFileOrFail()   A
last analyzed

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 0
Metric Value
c 0
b 0
f 0
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
     * @var string
24
     */
25
    private $templatePath;
26
27
    /**
28
     * FlysystemLoader constructor.
29
     *
30
     * @param Filesystem $filesystem
31
     * @param string $templatePath
32
     */
33 15
    public function __construct(Filesystem $filesystem, $templatePath = '')
34
    {
35 15
        $this->filesystem = $filesystem;
36 15
        $this->templatePath = $templatePath;
37 15
    }
38
39
    /**
40
     * Gets the source code of a template, given its name.
41
     *
42
     * @param string $name The name of the template to load
43
     *
44
     * @return string The template source code
45
     *
46
     * @throws Twig_Error_Loader When $name is not found
47
     */
48 9
    public function getSource($name)
49
    {
50 9
        $this->getFileOrFail($name);
51
52 6
        return $this->filesystem->read($this->resolveTemplateName($name));
53
    }
54
55
    /**
56
     * Checks if the underlying flysystem contains a file of the given name.
57
     *
58
     * @param string $name
59
     *
60
     * @return \League\Flysystem\File|\League\Flysystem\Handler
61
     * @throws Twig_Error_Loader
62
     */
63 15
    protected function getFileOrFail($name)
64
    {
65 15
        if (!$this->filesystem->has($this->resolveTemplateName($name))) {
66 3
            throw new Twig_Error_Loader('Template could not be found on the given filesystem');
67
        }
68
69 12
        $fileObject = $this->filesystem->get($this->resolveTemplateName($name));
70 12
        if ($fileObject->isDir()) {
71
            throw new Twig_Error_Loader('Cannot use directory as template');
72
        }
73
74 12
        return $fileObject;
75
    }
76
77
    /**
78
     * Gets the cache key to use for the cache for a given template name.
79
     *
80
     * @param string $name The name of the template to load
81
     *
82
     * @return string The cache key
83
     *
84
     * @throws Twig_Error_Loader When $name is not found
85
     */
86 3
    public function getCacheKey($name)
87
    {
88 3
        $this->getFileOrFail($name);
89
90 3
        return $name;
91
    }
92
93
    /**
94
     * Returns true if the template is still fresh.
95
     *
96
     * @param string $name The template name
97
     * @param int    $time Timestamp of the last modification time of the
98
     *                     cached template
99
     *
100
     * @return bool true if the template is fresh, false otherwise
101
     *
102
     * @throws Twig_Error_Loader When $name is not found
103
     */
104 3
    public function isFresh($name, $time)
105
    {
106 3
        $object = $this->getFileOrFail($name);
107
108 3
        return (int)$time >= (int)$object->getTimestamp();
109
    }
110
111
    /**
112
     * @param string $name
113
     *
114
     * @return string
115
     */
116 15
    protected function resolveTemplateName($name)
117
    {
118 15
        $prefix = $this->templatePath;
119 15
        if ($this->templatePath !== null && $this->templatePath !== '') {
120 3
            $prefix = rtrim($prefix, '/').'/';
121 2
        }
122
123 15
        return $prefix.$name;
124
    }
125
}
126