Completed
Push — master ( 0aef32...b40f7e )
by Marko
02:52
created

Config::getConfigRealPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/** @formatter:off
3
 * ******************************************************************
4
 * Created by   Marko Kungla on Jun 27, 2016 - 6:28:45 AM
5
 * Contact      [email protected]
6
 * @copyright   2016 Marko Kungla - https://github.com/mkungla
7
 * @license     The MIT License (MIT)
8
 * 
9
 * @category       AframeVR
10
 * @package        aframe-php
11
 * 
12
 * Lang         PHP (php version >= 7)
13
 * Encoding     UTF-8
14
 * File         Config.php
15
 * Code format  PSR-2 and 12
16
 * @link        https://github.com/mkungla/aframe-php
17
 ^ @issues      https://github.com/mkungla/aframe-php/issues
18
 * ********************************************************************
19
 * Contributors:
20
 * @author Marko Kungla <[email protected]>
21
 * ********************************************************************
22
 * Comments:
23
 * @formatter:on */
24
namespace AframeVR\Core;
25
26
final class Config
27
{
28
    /**
29
     * Path to package composer.json
30
     * 
31
     * @var string|null
32
     */
33
    private $config_path;
34
    
35
    /**
36
     * Array with contents of composer.json
37
     * 
38
     * @var array|null
39
     */
40
    private $config_data;
41
    
42
    /**
43
     * Config arguments starting with (aframe-)
44
     * 
45
     * @var array|null
46
     */
47
    private $config_args;
48
    
49
    /**
50
     * Useful config vars
51
     * 
52
     * @var array
53
     */
54
    private $config_vars;
55
    
56
    /**
57
     * Configuration constructor
58
     */
59 69
    public function __construct()
60
    {
61 69
        $this->loadComposerJson();
62 69
        $this->defineVars();
63 69
    }
64
    
65
    /**
66
     * Get configuration value by key
67
     * 
68
     * @param string $prop
69
     * @return string|null
70
     */
71 67
    public function get(string $prop)
72
    {
73 67
        return $this->config_vars[$prop] ?? null;
74
    }
75
    
76
    /**
77
     * Set configuration value
78
     * 
79
     * @param string $key
80
     * @param string $val
81
     */
82 7
    public function set(string $key, string $val)
83
    {
84 7
        $this->config_vars[$key] = $val;
85 7
    }
86
    
87
    /**
88
     * Real path to aframe-php composer.json
89
     * 
90
     * @return string
91
     */
92 69
    protected function getConfigRealPath()
93
    {
94 69
        return $this->config_path ?? $this->config_path = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'composer.json';
95
    }
96
    
97
    /**
98
     * Whether config file can be found
99
     * 
100
     * @return bool
101
     */
102 69
    protected function configExists()
103
    {
104 69
        return file_exists($this->getConfigRealPath());
105
    }
106
    
107
    /**
108
     * Load contents of aframe-php cmposer.json
109
     * 
110
     * @return array|null
111
     */
112 69
    protected function loadComposerJson()
113
    {
114 69
        return $this->configExists() ? $this->config_data = json_decode(file_get_contents($this->getConfigRealPath()),true) : null;
115
    }
116
    
117
    /**
118
     * Define Configuration constants
119
     * 
120
     * @return void
121
     */
122 69
    protected function defineVars()
123
    {
124 69
        $this->config_vars = array();
125 69
        $this->config_vars['DIR'] = $this->config_data['config']['aframe-dir'] ?? 'public/aframe';
126 69
        $this->config_vars['URL'] = $this->config_data['config']['aframe-url'] ?? '/aframe';
127 69
        $this->config_vars['CDN'] = $this->config_data['config']['aframe-cdn'] ?? 'https://aframe.io/releases/latest/aframe.min.js';
128
        
129 69
        $this->config_vars['formatOutput'] = !empty($this->config_data['config']['aframe-formatOutput']) ? true : false;
130 69
        $this->config_vars['formatOutput'] = !empty($this->config_data['config']['aframe-formatOutput']) ? true : false;
131 69
    }
132
}
133