Completed
Pull Request — master (#21)
by James
08:04
created

Config   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 44.44%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 102
ccs 12
cts 27
cp 0.4444
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 4
A get_config_json() 0 19 4
1
<?php
2
namespace Intraxia\Jaxion\Core;
3
4
/**
5
 * Class Config
6
 *
7
 * Configuration service to manage the
8
 * configuration data of the plugin or theme.
9
 *
10
 * @package    Intraxia\Jaxion
11
 * @subpackage Core
12
 */
13
class Config {
14
	/**
15
	 * Configuration type.
16
	 *
17
	 * @var ConfigType
18
	 */
19
	public $type;
20
21
	/**
22
	 * App entry file.
23
	 *
24
	 * @var string
25
	 */
26
	public $file;
27
28
	/**
29
	 * App url.
30
	 *
31
	 * @var string
32
	 */
33
	public $url;
34
35
	/**
36
	 * App path.
37
	 *
38
	 * @var string
39
	 */
40
	public $path;
41
42
	/**
43
	 * App slug.
44
	 *
45
	 * @var string
46
	 */
47
	public $slug;
48
49
	/**
50
	 * App basename.
51
	 *
52
	 * @var string
53
	 */
54
	public $basename;
55
56
	/**
57
	 * Loaded configuration files.
58
	 *
59
	 * @var array
60
	 */
61
	private $loaded = array();
62
63
	/**
64
	 * Config constructor.
65
	 *
66
	 * @param string $type
67
	 * @param string $file
68
	 */
69 27
	public function __construct( $type, $file ) {
70 27
		$this->type = new ConfigType( $type );
71 27
		$this->file = $file;
72
73 27
		switch ( $this->type->getValue() ) {
74 27
			case ConfigType::PLUGIN:
75 18
			case ConfigType::MU_PLUGIN:
76 27
				$this->url = plugin_dir_url( $file );
77 27
				$this->path = plugin_dir_path( $file );
78 27
				$this->slug = dirname( $this->basename = plugin_basename( $file ) );
79 27
				break;
80
			case ConfigType::THEME:
81
				$this->url = get_stylesheet_directory_uri() . '/';
82
				$this->path = get_stylesheet_directory() . '/';
83
				$this->slug = dirname( $this->basename = plugin_basename( $file ) );
84
				break;
85 18
		}
86 27
	}
87
88
	/**
89
	 * Load a configuration JSON file from the config folder.
90
	 *
91
	 * @param string $filename
92
	 *
93
	 * @return array|null
94
	 */
95
	public function get_config_json( $filename ) {
96
		if ( isset( $this->loaded[ $filename ] ) ) {
97
			return $this->loaded[ $filename ];
98
		}
99
100
		$config = $this->path . 'config/' . $filename . '.json';
101
102
		if ( ! file_exists( $config ) ) {
103
			return null;
104
		}
105
106
		$contents = file_get_contents( $config );
0 ignored issues
show
introduced by
file_get_contents is highly discouraged, please use wpcom_vip_file_get_contents() instead.
Loading history...
107
108
		if ( false === $contents ) {
109
			return null;
110
		}
111
112
		return $this->loaded[ $filename ] = json_decode( $contents, true );
113
	}
114
}
115