Completed
Push — master ( 8e7ddb...d39b5e )
by Marko
02:50
created

Config::defineVars()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 1
nop 0
crap 3
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 73
    public function __construct()
54
    {
55 73
        $this->loadComposerJson();
56 73
        $this->defineVars();
57 73
    }
58
59
    /**
60
     * Get configuration value by key
61
     *
62
     * @param string $prop            
63
     * @return string|null
64
     */
65 71
    public function get(string $prop)
66
    {
67 71
        return $this->config_vars[$prop] ?? null;
68
    }
69
70
    /**
71
     * Set configuration value
72
     *
73
     * @param string $key            
74
     * @param mixed $val            
75
     */
76 73
    public function set(string $key, $val)
77
    {
78 73
        $this->config_vars[$key] = $val;
79 73
    }
80
81
    /**
82
     * Real path to aframe-php composer.json
83
     *
84
     * @return string
85
     */
86 73
    protected function getConfigRealPath()
87
    {
88 73
        return $this->config_path ?? $this->config_path = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'composer.json';
89
    }
90
91
    /**
92
     * Whether config file can be found
93
     *
94
     * @return bool
95
     */
96 73
    protected function configExists()
97
    {
98 73
        return file_exists($this->getConfigRealPath());
99
    }
100
101
    /**
102
     * Load contents of aframe-php cmposer.json
103
     *
104
     * @return array|null
105
     */
106 73
    protected function loadComposerJson()
107
    {
108 73
        return $this->configExists() 
109 73
        ? $this->cfg_data = json_decode(file_get_contents($this->getConfigRealPath()), true) 
110 73
        : null;
111
    }
112
113
    /**
114
     * Define Configuration constants
115
     *
116
     * @return void
117
     */
118 73
    protected function defineVars()
119
    {
120 73
        $this->set('DIR', $this->cfg_data['config']['aframe-dir'] ?? 'public/aframe');
121 73
        $this->set('URL', $this->cfg_data['config']['aframe-url'] ?? '/aframe');
122 73
        $this->set('CDN', $this->cfg_data['config']['aframe-cdn'] ?? 'https://aframe.io/releases/latest/aframe.min.js');
123 73
        $this->set('formatOutput',! empty($this->cfg_data['config']['aframe-formatOutput']) ? true : false);
124 73
        $this->set('useCDN',! empty($this->cfg_data['config']['aframe-useCDN']) ? true : false);
125 73
    }
126
}
127