Passed
Push — master ( 891229...7f6823 )
by Alain
03:17
created

TemplatedShortcode::init_template_loader()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 6
eloc 19
nc 17
nop 0
1
<?php
2
/**
3
 * Templated Shortcode Implementation.
4
 *
5
 * This version of the shortcode uses Gamajo/TemplateLoader to let you override
6
 * the shortcode views from your theme.
7
 *
8
 * @package   BrightNucleus\Shortcode
9
 * @author    Alain Schlesser <[email protected]>
10
 * @license   GPL-2.0+
11
 * @link      http://www.brightnucleus.com/
12
 * @copyright 2015-2016 Alain Schlesser, Bright Nucleus
13
 */
14
15
namespace BrightNucleus\Shortcode;
16
17
use Assert;
18
use BrightNucleus\Config\ConfigInterface;
19
use BrightNucleus\Dependency\DependencyManagerInterface as DependencyManager;
20
use BrightNucleus\Exception\RuntimeException;
21
use Gamajo_Template_Loader;
22
23
/**
24
 * Templated Implementation of the Shortcode Interface.
25
 *
26
 * This version of the Shortcode
27
 *
28
 * @since   0.2.6
29
 *
30
 * @package BrightNucleus\Shortcode
31
 * @author  Alain Schlesser <[email protected]>
32
 */
33
class TemplatedShortcode extends Shortcode {
34
35
	/**
36
	 * Template loader that allows a theme to override the shortcode's views.
37
	 *
38
	 * @var Gamajo_Template_Loader|null
39
	 */
40
	protected $template_loader;
41
42
	/**
43
	 * Instantiate Basic Shortcode.
44
	 *
45
	 * @since 0.2.6
46
	 *
47
	 * @param string                 $shortcode_tag Tag that identifies the
48
	 *                                              shortcode.
49
	 * @param ConfigInterface        $config        Configuration settings.
50
	 * @param ShortcodeAttsParser    $atts_parser   Attributes parser and
51
	 *                                              validator.
52
	 * @param DependencyManager|null $dependencies  Optional. Dependencies of
53
	 *                                              the shortcode.
54
	 * @throws RuntimeException If the config could not be processed.
55
	 */
56
	public function __construct(
57
		$shortcode_tag,
58
		ConfigInterface $config,
59
		ShortcodeAttsParser $atts_parser,
60
		DependencyManager $dependencies = null
61
	) {
62
63
		parent::__construct(
64
			$shortcode_tag,
65
			$config,
66
			$atts_parser,
67
			$dependencies
68
		);
69
70
		$this->template_loader = $this->init_template_loader();
71
	}
72
73
	/**
74
	 * Initialize the template loader class.
75
	 *
76
	 * @since 0.2.6
77
	 *
78
	 * @return Gamajo_Template_Loader
0 ignored issues
show
Documentation introduced by
Should the return type not be Gamajo_Template_Loader|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
79
	 */
80
	public function init_template_loader() {
81
		if ( ! $this->hasConfigKey( 'template' ) ) {
82
			return null;
83
		}
84
		$loader_class  = $this->hasConfigKey( 'template', 'custom_loader' )
85
			? $this->getConfigKey( 'template', 'custom_loader' )
86
			: $this->get_default_template_loader_class();
87
		$filter_prefix = $this->hasConfigKey( 'template', 'filter_prefix' )
88
			? $this->getConfigKey( 'template', 'filter_prefix' )
89
			: $this->get_default_filter_prefix();
90
		$template_dir  = $this->hasConfigKey( 'template', 'template_directory' )
91
			? $this->getConfigKey( 'template', 'theme_template_directory' )
92
			: $this->get_default_template_directory();
93
		$view_dir      = $this->hasConfigKey( 'view' )
94
			? $this->getConfigKey( 'view' )
95
			: $this->get_default_view_directory();
96
97
		return new $loader_class(
98
			$filter_prefix,
99
			$template_dir,
100
			$view_dir
101
		);
102
	}
103
104
	/**
105
	 * Get the default template loader class that is used when none is defined
106
	 * in the config file.
107
	 *
108
	 * @since 0.2.6
109
	 *
110
	 * @return string The default template laoder class to use.
111
	 */
112
	protected function get_default_template_loader_class() {
113
		return 'BrightNucleus\Shortcode\ShortcodeTemplateLoader';
114
	}
115
116
	/**
117
	 * Get the default filter prefix that is used when none is defined in the
118
	 * config file.
119
	 *
120
	 * Defaults to 'bn_shortcode.'.
121
	 *
122
	 * @since 0.2.6
123
	 *
124
	 * @return string Default filter prefix to use.
125
	 */
126
	protected function get_default_filter_prefix() {
127
		return 'bn_shortcode';
128
	}
129
130
	/**
131
	 * Get the default template directory that is used when none is defined in
132
	 * the config file.
133
	 *
134
	 * Defaults to 'bn_shortcode'.
135
	 *
136
	 * @since 0.2.6
137
	 *
138
	 * @return string Default template directory to use.
139
	 */
140
	protected function get_default_template_directory() {
141
		return 'bn_shortcode';
142
	}
143
144
	/**
145
	 * Get the default view directory that is used when none is defined in the
146
	 * config file.
147
	 *
148
	 * Defaults to 'views/shortcodes'. Will probably need to be changed into an
149
	 * absolute path if the shortcodes package is pulled in through Composer.
150
	 *
151
	 * @since 0.2.6
152
	 *
153
	 * @return string Default view directory to use.
154
	 */
155
	protected function get_default_view_directory() {
156
		return 'views/shortcodes';
157
	}
158
159
	/**
160
	 * Get the rendered HTML for a given view.
161
	 *
162
	 * @since 0.2.6
163
	 *
164
	 * @param string $view The view to render.
165
	 * @return string HTML rendering of the view.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
166
	 */
167
	protected function render_view( $view ) {
168
		if ( empty( $view ) ) {
169
			return '';
170
		}
171
172
		$this->maybe_strip_extension( $view );
173
174
		$template = $this->template_loader->get_template_part( $view );
0 ignored issues
show
Unused Code introduced by
$template is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
175
	}
176
177
	/**
178
	 * Strip the extension of a given view if it includes an extension.
179
	 *
180
	 * @since 0.2.6
181
	 *
182
	 * @param string $view The view that maybe needs its extension stripped.
183
	 * @return string Extension-less view.
184
	 */
185
	protected function maybe_strip_extension( $view ) {
186
		$pathinfo = pathinfo( $view );
187
		return $pathinfo['dirname'] . PATH_SEPARATOR . $pathinfo['filename'];
188
	}
189
}
190