Completed
Push — develop ( c6a956...40134e )
by Steven
9s
created

Config   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 6
c 3
b 0
f 2
lcom 1
cbo 3
dl 0
loc 52
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __get() 0 4 1
A getConfigFile() 0 11 2
A readConfigFile() 0 8 2
1
<?php namespace Magestead\Helper;
2
3
use Magestead\Exceptions\MissingConfigFileException;
4
use Symfony\Component\Console\Output\OutputInterface;
5
use Symfony\Component\Yaml\Exception\ParseException;
6
use Symfony\Component\Yaml\Parser;
7
8
class Config
9
{
10
    protected $_projectPath;
11
12
    /**
13
     * Config constructor.
14
     * @param OutputInterface $output
15
     */
16
    public function __construct(OutputInterface $output)
17
    {
18
        $this->_projectPath = getcwd();
19
        $this->_config      = $this->getConfigFile($output);
0 ignored issues
show
Bug introduced by
The property _config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
    }
21
22
    /**
23
     * @param $name
24
     * @return mixed
25
     */
26
    function __get($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
    {
28
        return $this->_config['magestead']['apps']['mba_12345'][$name];
29
    }
30
31
    /**
32
     * @param OutputInterface $output
33
     * @return bool|mixed
34
     */
35
    protected function getConfigFile(OutputInterface $output)
36
    {
37
        $config = new Parser();
38
        try {
39
            return $config->parse($this->readConfigFile());
40
        } catch (ParseException $e) {
41
            $output->writeln('<error>Unable to parse the config file</error>');
42
        }
43
44
        return false;
45
    }
46
47
    /**
48
     * @return string
49
     * @throws MissingConfigFileException
50
     */
51
    protected function readConfigFile()
52
    {
53
        if (!file_exists($this->_projectPath . '/magestead.yaml')) {
54
            throw new MissingConfigFileException('No config file was found, are you in the project root?');
55
        }
56
57
        return file_get_contents($this->_projectPath . '/magestead.yaml');
58
    }
59
}
60