FileArrayLoader   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 71
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 4
A load() 0 10 3
A getFileName() 0 10 2
1
<?php
2
3
namespace RamRacing\Mustache;
4
5
use Mustache_Exception_UnknownTemplateException;
6
use Mustache_Loader;
7
8
class FileArrayLoader implements Mustache_Loader
9
{
10
    /**
11
     * @property string
12
     */
13
    protected $base_dir;
14
15
    /**
16
     * @property string
17
     */
18
    protected $extension = '.mustache';
19
    
20
    /**
21
     * @property array
22
     */
23
    protected $files;
24
25
    /**
26
     * @param array $files
27
     */
28
    public function __construct(array $files, array $options = array())
29
    {
30
        $this->files = $files;
31
        
32
        if (array_key_exists('base_dir', $options)) {
33
            $this->base_dir = $options['base_dir'];
34
        }
35
        
36
        if (array_key_exists('extension', $options)) {
37
            if (empty($options['extension'])) {
38
                $this->extension = '';
39
            } else {
40
                $this->extension = '.' . ltrim($options['extension'], '.');
41
            }
42
        }
43
    }
44
45
    /**
46
     * Load a Template by name.
47
     *
48
     * @throws Mustache_Exception_UnknownTemplateException If a template file is not found.
49
     *
50
     * @param string $name
51
     *
52
     * @return string Mustache Template source
53
     */
54
    public function load($name)
55
    {
56
        $key = $this->getFileName($name);
57
        
58
        if (!isset($this->files[$key]) || !file_exists($this->files[$key])) {
59
            throw new Mustache_Exception_UnknownTemplateException($name);
60
        }
61
62
        return file_get_contents($this->files[$key]);
63
    }
64
    
65
    /**
66
     * @param string $name
67
     */
68
    protected function getFileName($name)
69
    {
70
        $file = $name . $this->extension;
71
        
72
        if ($this->base_dir) {
73
            $file = $this->base_dir . '/' . $key;
0 ignored issues
show
Bug introduced by
The variable $key does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
74
        }
75
        
76
        return $file;
77
    }
78
}
79