GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Parser::parseConfigFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of WebHelper Parser.
5
 *
6
 * (c) James <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace WebHelper\Parser;
12
13
use WebHelper\Parser\Exception\InvalidConfigException;
14
use WebHelper\Parser\Exception\ParserException;
15
use WebHelper\Parser\Parser\After;
16
use WebHelper\Parser\Parser\Before;
17
use WebHelper\Parser\Server\ServerInterface;
18
19
/**
20
 * Web server configuration generic parser.
21
 *
22
 * @author James <[email protected]>
23
 */
24
class Parser implements ParserInterface
25
{
26
    /** @var Server\ServerInterface a server instance */
27
    private $server;
28
29
    /** @var Compiler a Compiler instance */
30
    private $compiler;
31
32
    /** @var string configuration file */
33
    private $configFile = '';
34
35
    /** @var array active directives in an array */
36
    protected $activeConfig = [];
37
38
    /**
39
     * Setter for the server instance.
40
     *
41
     * @see Server\ServerInterface Server Documentation
42
     *
43
     * @param Server\ServerInterface $server the server instance
44
     */
45 13
    public function setServer(ServerInterface $server)
46
    {
47 13
        $this->server = $server;
48
49 13
        return $this;
50
    }
51
52
    /**
53
     * Setter for the compiler instance.
54
     *
55
     * @param Compiler $compiler the compiler instance
56
     */
57 7
    public function setCompiler(Compiler $compiler)
58
    {
59 7
        $this->compiler = $compiler;
60 7
        $this->compiler->setParser($this);
61
62 7
        return $this;
63
    }
64
65
    /**
66
     * Setter for the config file to parse.
67
     *
68
     * @param string $configFile configuration file
69
     *
70
     * @throws Exception\ParserException        if configuration file is not readable
71
     * @throws Exception\InvalidConfigException if active configuration is empty
72
     */
73 15
    public function setConfigFile($configFile = '')
74
    {
75 15
        if (!is_readable($configFile)) {
76 1
            $this->activeConfig = [];
77
78 1
            throw ParserException::forFileUnreadable($configFile);
79
        }
80
81 14
        $this->configFile = $configFile;
82 14
        if (!$this->parseConfigFile()) {
83 2
            throw InvalidConfigException::forEmptyConfig($configFile);
84
        }
85
86 12
        return $this;
87
    }
88
89
     /**
90
      * Getter for the server instance.
91
      *
92
      * @see Server\ServerInterface Server Documentation
93
      *
94
      * @param Server\ServerInterface the server instance
95
      */
96 11
     public function getServer()
97
     {
98 11
         return $this->server;
99
     }
100
101
    /**
102
     * Getter for the active config main context.
103
     *
104
     * @return Directive\DirectiveInterface the active config
105
     */
106 10
    public function getActiveConfig()
107
    {
108 10
        return $this->compiler->doCompile($this->activeConfig);
109
    }
110
111
    /**
112
     * Getter for the content of the configuration file.
113
     *
114
     * @return string content of the configuration file
115
     */
116 14
    public function getOriginalConfig()
117
    {
118 14
        return file_get_contents($this->configFile);
119
    }
120
121
    /**
122
     * Does some extra parsing before the active config turns into an array.
123
     *
124
     * @param string $config a config file content
125
     *
126
     * @return string a config file content
127
     */
128 14
    protected function beforeExplode($config)
129
    {
130 14
        foreach ($this->server->getBeforeMethods() as $beforeMethod) {
131 14
            $config = Before::$beforeMethod($config);
132 14
        }
133
134 14
        return $config;
135
    }
136
137
    /**
138
     * Does some extra parsing after the active config has turned into an array.
139
     *
140
     * @param array $activeConfig an active config
141
     *
142
     * @return array an active config
143
     */
144 14
    protected function afterExplode(array $activeConfig)
145
    {
146 14
        foreach ($this->server->getAfterMethods() as $afterMethod) {
147 14
            $activeConfig = After::$afterMethod($activeConfig);
148 14
        }
149
150 14
        return $activeConfig;
151
    }
152
153
    /**
154
     * Comon parsing to both apache and nginx.
155
     *
156
     * @return bool true if active lines were found
157
     */
158 14
    private function parseConfigFile()
159
    {
160 14
        $activeConfig = $this->getOriginalConfig();
161 14
        $activeConfig = $this->beforeExplode($activeConfig);
162
163
        //convert into an array
164 14
        $activeConfig = explode("\n", $activeConfig);
165
166 14
        $this->activeConfig = $this->afterExplode($activeConfig);
167
168 14
        return !empty($this->activeConfig);
169
    }
170
}
171