Completed
Pull Request — staging (#840)
by
unknown
17:30
created

TemplatedView::validate()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 1
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * YIKES Inc. Easy Forms.
4
 *
5
 * @package   YIKES\EasyForms
6
 * @author    Freddie Mixell
7
 * @license   GPL2
8
 */
9
10
namespace YIKES\EasyForms\View;
11
12
use YIKES\EasyForms\Exception\InvalidURI;
13
use YIKES\EasyForms\PluginHelper;
14
15
/**
16
 * Class TemplatedView.
17
 *
18
 * Looks within the child theme and parent theme folders first for a view,
19
 * before defaulting to the plugin folder.
20
 *
21
 * @since   %VERSION%
22
 *
23
 * @package YIKES\EasyForms
24
 * @author  Freddie Mixell
25
 */
26
class TemplatedView extends BaseView {
27
28
	use PluginHelper;
29
30
	/**
31
	 * Validate an URI.
32
	 *
33
	 * @since %VERSION%
34
	 *
35
	 * @param string $uri URI to validate.
36
	 *
37
	 * @return string Validated URI.
38
	 * @throws InvalidURI If an invalid URI was passed into the View.
39
	 */
40 View Code Duplication
	protected function validate( $uri ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
		$uri = $this->check_extension( $uri, static::VIEW_EXTENSION );
42
43
		foreach ( $this->get_locations( $uri ) as $location ) {
44
			if ( is_readable( $location ) ) {
45
				return $location;
46
			}
47
		}
48
49
		if ( ! is_readable( $uri ) ) {
50
			throw InvalidURI::from_uri( $uri );
51
		}
52
53
		return $uri;
54
	}
55
56
	/**
57
	 * Get the possible locations for the view.
58
	 *
59
	 * @since %VERSION%
60
	 *
61
	 * @param string $uri URI of the view to get the locations for.
62
	 *
63
	 * @return array Array of possible locations.
64
	 */
65
	protected function get_locations( $uri ) {
66
		/**
67
		 * Filter the available locations for view templates to be found.
68
		 *
69
		 * Locations will be tried in the order provided by the array,
70
		 * so locations with higher priority should be first.
71
		 *
72
		 * @param array $locations
73
		 */
74
		$locations = (array) apply_filters( 'lpf_templated_view_locations', [
75
			trailingslashit( get_stylesheet_directory() ) . "lpf/{$uri}",
76
			trailingslashit( get_template_directory() ) . "lpf/{$uri}",
77
		] );
78
79
		// Ensure the plugin folder is always available.
80
		$locations[] = trailingslashit( $this->get_root_dir() ) . $uri;
81
82
		return $locations;
83
	}
84
}
85