Config::getConfigRealPath()   A
last analyzed

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
    /**
30
     * Path to package composer.json
31
     *
32
     * @var string|null
33
     */
34
    private $config_path;
35
36
    /**
37
     * Array with contents of composer.json
38
     *
39
     * @var array|null
40
     */
41
    private $cfg_data;
42
43
    /**
44
     * Config vars
45
     *
46
     * @var array
47
     */
48
    private $config_vars;
49
50
    /**
51
     * Configuration constructor
52
     */
53 105
    public function __construct()
54
    {
55 105
        $this->loadComposerJson();
56 105
        $this->defineVars();
57 105
    }
58
59
    /**
60
     * Get configuration value by key
61
     *
62
     * @param string $prop            
63
     * @return string|null
64
     */
65 103
    public function get(string $prop)
66
    {
67 103
        return $this->config_vars[$prop] ?? null;
68
    }
69
70
    /**
71
     * Set configuration value
72
     *
73
     * @param string $key            
74
     * @param mixed $val    
75
     * @return Config        
76
     */
77 105
    public function set(string $key, $val) : Config
78
    {
79 105
        $this->config_vars[$key] = $val;
80 105
        return $this;
81
    }
82
83
    /**
84
     * Real path to aframe-php composer.json
85
     *
86
     * @return string
87
     */
88 105
    protected function getConfigRealPath()
89
    {
90 105
        return $this->config_path ?? $this->config_path = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'composer.json';
91
    }
92
93
    /**
94
     * Whether config file can be found
95
     *
96
     * @return bool
97
     */
98 105
    protected function configExists()
99
    {
100 105
        return file_exists($this->getConfigRealPath());
101
    }
102
103
    /**
104
     * Load contents of aframe-php cmposer.json
105
     * and read aframe config
106
     *
107
     * @return void
108
     */
109 105
    protected function loadComposerJson()
110
    {
111 105
        if($this->configExists()) {
112 105
            $cfg_data       = json_decode(file_get_contents($this->getConfigRealPath()), true);
113 105
            $this->cfg_data = $cfg_data['config']['aframe'] ?? null;
114
        }
115 105
    }
116
117
    /**
118
     * Define Configuration constants
119
     *
120
     * @return void
121
     */
122 105
    protected function defineVars()
123
    {
124 105
        $this->set('assets_uri', $this->cfg_data['assets_uri'] ?? '/aframe');
125 105
        $this->set('cdn_url', $this->cfg_data['cdn_url'] ?? '');
126 105
        $this->set('format_output',! empty($this->cfg_data['format_output']) ? true : false);
127 105
        $this->set('use_cdn',! empty($this->cfg_data['use_cdn']) ? true : false);
128 105
    }
129
}
130