Completed
Push — master ( f8571a...f0770d )
by Marko
04:14
created

Config::get()   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 1
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
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
60
     */
61 62
    public function __construct()
62
    {
63 62
        $this->loadComposerJson();
64 62
        $this->defineVars();
65 62
    }
66
    
67
    /**
68
     * Get configuration value by key
69
     * 
70
     * @param string $prop
71
     * @return string|null
72
     */
73 59
    public function get(string $prop)
74
    {
75 59
        return $this->config_vars[$prop] ?? null;
76
    }
77
    
78
    /**
79
     * Real path to aframe-php composer.json
80
     * 
81
     * @return string
82
     */
83 62
    protected function getConfigRealPath()
84
    {
85 62
        return $this->config_path ?? $this->config_path = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'composer.json';
86
    }
87
    
88
    /**
89
     * Whether config file can be found
90
     * 
91
     * @return bool
92
     */
93 62
    protected function configExists()
94
    {
95 62
        return file_exists($this->getConfigRealPath());
96
    }
97
    
98
    /**
99
     * Load contents of aframe-php cmposer.json
100
     * 
101
     * @return array|null
102
     */
103 62
    protected function loadComposerJson()
104
    {
105 62
        return $this->configExists() ? $this->config_data = json_decode(file_get_contents($this->getConfigRealPath()),true) : null;
106
    }
107
    
108
    /**
109
     * Define Configuration constants
110
     * 
111
     * @return void
112
     */
113 62
    protected function defineVars()
114
    {
115 62
        $this->config_vars = array();
116 62
        $this->config_vars['DIR'] = $this->config_data['config']['aframe-dir'] ?? 'public/aframe';
117 62
        $this->config_vars['URL'] = $this->config_data['config']['aframe-url'] ?? '/aframe';
118 62
        $this->config_vars['CDN'] = $this->config_data['config']['aframe-cdn'] ?? 'https://aframe.io/releases/latest/aframe.min.js';
119 62
    }
120
}
121