Completed
Push — master ( 3a89f5...c6da09 )
by Neomerx
04:08
created

LaravelServiceProvider::getConfigPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php namespace Neomerx\CorsIlluminate\Providers;
2
3
/**
4
 * Copyright 2015 [email protected] (www.neomerx.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use \Closure;
20
use \Neomerx\Cors\Analyzer;
21
use \Psr\Log\LoggerInterface;
22
use \Illuminate\Support\ServiceProvider;
23
use \Illuminate\Contracts\Config\Repository;
24
use \Neomerx\Cors\Contracts\AnalyzerInterface;
25
use \Neomerx\CorsIlluminate\Settings\Settings;
26
use \Neomerx\Cors\Contracts\AnalysisStrategyInterface;
27
use \Illuminate\Contracts\Foundation\Application as ApplicationInterface;
28
29
/**
30
 * @package Neomerx\CorsIlluminate
31
 */
32
class LaravelServiceProvider extends ServiceProvider
33
{
34
    /** Config file name without extension */
35
    const CONFIG_FILE_NAME_WO_EXT = 'cors-illuminate';
36
37
    /**
38
     * @inheritdoc
39
     */
40
    protected $defer = false;
41
42
    /**
43
     * @var bool|null|LoggerInterface
44
     */
45
    private $logger = false;
46
47
    /**
48
     * @var bool|array
49
     */
50
    private $settings = false;
51
52
    /**
53
     * @inheritdoc
54
     */
55 1
    public function register()
56
    {
57 1
        $this->mergeConfigFrom($this->getConfigPath(), static::CONFIG_FILE_NAME_WO_EXT);
58 1
        $this->configureCorsAnalyzer();
59 1
    }
60
61
    /**
62
     * Perform post-registration booting of services.
63
     *
64
     * @return void
65
     */
66 1
    public function boot()
67 1
    {
68 1
        $this->registerPublishConfig();
69 1
    }
70
71
    /**
72
     * @return void
73
     */
74 1
    protected function registerPublishConfig()
75
    {
76 1
        $publishPath = $this->app['path.config'] . DIRECTORY_SEPARATOR . static::CONFIG_FILE_NAME_WO_EXT . '.php';
77 1
        $this->publishes([
78 1
            $this->getConfigPath() => $publishPath,
79 1
        ]);
80 1
    }
81
82
    /**
83
     * @return void
84
     */
85 2
    protected function configureCorsAnalyzer()
86
    {
87 2
        $this->app->bind(AnalysisStrategyInterface::class, $this->getCreateAnalysisStrategyClosure());
88 2
        $this->app->bind(AnalyzerInterface::class, $this->getCreateAnalyzerClosure());
89 2
    }
90
91
    /**
92
     * @return Closure
93
     */
94 3
    protected function getCreateAnalysisStrategyClosure()
95
    {
96
        return function ($app) {
97 1
            $settings = $this->getSettings($app);
98 1
            $strategy = new Settings($settings);
99
100 1
            return $strategy;
101 3
        };
102
    }
103
104
    /**
105
     * @return Closure
106
     */
107
    protected function getCreateAnalyzerClosure()
108
    {
109 3
        return function ($app) {
110
            /** @var AnalysisStrategyInterface $strategy */
111 1
            $strategy = $app[AnalysisStrategyInterface::class];
112 1
            $analyzer = Analyzer::instance($strategy);
113
114 1
            $logger = $this->getLoggerIfEnabled($app);
115 1
            $logger === null ?: $analyzer->setLogger($logger);
116
117 1
            return $analyzer;
118 3
        };
119
    }
120
121
    /**
122
     * @return string
123
     */
124 2
    private function getConfigPath()
125
    {
126 2
        $root = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR;
127 2
        $path = $root . 'config' . DIRECTORY_SEPARATOR . static::CONFIG_FILE_NAME_WO_EXT . '.php';
128
129 2
        return $path;
130
    }
131
132
    /**
133
     * @param ApplicationInterface $app
134
     *
135
     * @return null|LoggerInterface
136
     */
137 1
    private function getLoggerIfEnabled($app)
138
    {
139
        /** @var ApplicationInterface $app */
140
141 1
        if ($this->logger === false) {
142 1
            $settings       = $this->getSettings($app);
143
            $loggingEnabled =
144 1
                array_key_exists(Settings::KEY_LOGS_ENABLED, $settings) === true &&
145 1
                $settings[Settings::KEY_LOGS_ENABLED] === true;
146
147 1
            $this->logger = $loggingEnabled === true ? $app[LoggerInterface::class] : null;
148 1
        }
149
150 1
        return $this->logger;
151
    }
152
153
    /**
154
     * @param ApplicationInterface $app
155
     *
156
     * @return array
157
     */
158 2
    private function getSettings($app)
159
    {
160
        /** @var ApplicationInterface $app */
161
162 2
        if ($this->settings === false) {
163
            /** @var Repository $config */
164 2
            $config         = $app['config'];
165 2
            $this->settings = $config->get(static::CONFIG_FILE_NAME_WO_EXT, []);
166 2
        }
167
168 2
        return $this->settings;
169
    }
170
}
171