Passed
Push — develop ( 73b4e9...8d0058 )
by Daniel
05:19
created

loader::setPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\services\template;
11
12
/**
13
 * Class template_loader
14
 *
15
 * @package blitze\content\services
16
 */
17
class loader implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface, \Twig_SourceContextLoaderInterface
0 ignored issues
show
Bug introduced by
The type Twig_ExistsLoaderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Twig_LoaderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Twig_SourceContextLoaderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
{
19
	/* @var array */
20
	protected $blocks_data = array();
21
22
	/* @var array */
23
	protected $content_types = array();
24
25
	/**
26
	 * Constructor
27
	 *
28
	 * @param \blitze\content\services\types				$content			Content types object
29
	 * @param \blitze\sitemaker\model\mapper_factory		$mapper_factory		Mapper factory object
30
	 */
31
	public function __construct(\blitze\content\services\types $content, \blitze\sitemaker\model\mapper_factory $mapper_factory)
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\model\mapper_factory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
	{
33
		$types = $content->get_all_types();
34
		foreach ($types as $type => $entity)
35
		{
36
			/** @var \blitze\content\model\entity\type $entity */
37
			$this->content_types[$type] = array(
38
				'summary_tpl'	=> $entity->get_summary_tpl(),
39
				'detail_tpl'	=> $entity->get_detail_tpl(),
40
				'last_modified'	=> $entity->get_last_modified(),
41
			);
42
		}
43
44
		$block_mapper = $mapper_factory->create('blocks', 'blocks');
45
		$collection = $block_mapper->find(array('name', '=', 'blitze.content.block.recent'));
46
47
		foreach ($collection as $entity)
48
		{
49
			/** @var \blitze\sitemaker\model\entity\block $entity */
50
			$settings = $entity->get_settings();
51
			$this->blocks_data[$entity->get_bid()] = array(
52
				'block_tpl'		=> htmlspecialchars_decode($settings['block_tpl']),
53
				'last_modified'	=> $settings['last_modified'],
54
			);
55
		}
56
	}
57
58
	public function setPaths()
59
	{
60
		// do nothing
61
	}
62
63
	/**
64
	 * @param string $name
65
	 * @return mixed
66
	 * @throws \Twig_Error_Loader
67
	 */
68
	public function getSource($name)
69
	{
70
		if (false === $source = $this->getValue('source', $name))
71
		{
72
			throw new \Twig_Error_Loader(sprintf('Template "%s" does not exist.', $name));
0 ignored issues
show
Bug introduced by
The type Twig_Error_Loader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
73
		}
74
75
		return $source;
76
	}
77
78
	/**
79
	 * Twig_SourceContextLoaderInterface as of Twig 1.27
80
	 * @param string $name
81
	 * @return mixed
82
	 * @throws \Twig_Error_Loader
83
	 */
84
	public function getSourceContext($name)
85
	{
86
		if (false === $source = $this->getValue('source', $name))
87
		{
88
			throw new Twig_Error_Loader(sprintf('Template "%s" does not exist.', $name));
0 ignored issues
show
Bug introduced by
The type blitze\content\services\template\Twig_Error_Loader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
89
		}
90
91
		return new \Twig_Source($source, $name);
0 ignored issues
show
Bug introduced by
The type Twig_Source was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
92
	}
93
94
	/**
95
	 * Twig_ExistsLoaderInterface as of Twig 1.11
96
	 * @param string $name
97
	 * @return bool
98
	 */
99
	public function exists($name)
100
	{
101
		return $name === $this->getValue('name', $name);
102
	}
103
104
	/**
105
	 * @param string $name
106
	 * @return string
107
	 */
108
	public function getCacheKey($name)
109
	{
110
		return $name;
111
	}
112
113
	/**
114
	 * @param string $name
115
	 * @param int $time
116
	 * @return bool
117
	 */
118
	public function isFresh($name, $time)
119
	{
120
		if (false === $last_modified = $this->getValue('last_modified', $name))
121
		{
122
			return false;
123
		}
124
125
		return $last_modified <= $time;
126
	}
127
128
	/**
129
	 * @param string $what
130
	 * @param string $name
131
	 * @return mixed
132
	 */
133
	protected function getValue($what, $name)
134
	{
135
		preg_match('/(.*)_(summary|detail|block)$/is', $name, $match);
136
		list(, $id, $view) = $match;
137
138
		if ($view === 'block')
139
		{
140
			$data = $this->blocks_data[$id];
141
			$column_name = 'block_tpl';
142
		}
143
		else
144
		{
145
			$data = $this->content_types[$id];
146
			$column_name = $view . '_tpl';
147
		}
148
149
		$return = array(
150
			'name'			=> $name,
151
			'source'		=> $data[$column_name],
152
			'last_modified'	=> $data['last_modified'],
153
		);
154
155
		return $return[$what];
156
	}
157
}
158