Completed
Push — master ( 1776d4...322507 )
by Neomerx
02:47
created

CorsSettings::getAppConfig()   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 0
crap 1
1
<?php namespace Limoncello\Application\Packages\Cors;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
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 Limoncello\Contracts\Application\ApplicationConfigurationInterface as A;
20
use Limoncello\Contracts\Settings\SettingsInterface;
21
use Neomerx\Cors\Strategies\Settings;
22
23
/**
24
 * @package Limoncello\Application
25
 */
26
class CorsSettings implements SettingsInterface
27
{
28
    /** @see Settings */
29
    const VALUE_ALLOW_ORIGIN_ALL = Settings::VALUE_ALLOW_ORIGIN_ALL;
30
31
    /** @see Settings */
32
    const KEY_SERVER_ORIGIN = Settings::KEY_SERVER_ORIGIN;
33
34
    /** @see Settings */
35
    const KEY_SERVER_ORIGIN_SCHEME = Settings::KEY_SERVER_ORIGIN_SCHEME;
36
37
    /** @see Settings */
38
    const KEY_SERVER_ORIGIN_HOST = Settings::KEY_SERVER_ORIGIN_HOST;
39
40
    /** @see Settings */
41
    const KEY_SERVER_ORIGIN_PORT = Settings::KEY_SERVER_ORIGIN_PORT;
42
43
    /** @see Settings */
44
    const KEY_ALLOWED_ORIGINS = Settings::KEY_ALLOWED_ORIGINS;
45
46
    /** @see Settings */
47
    const KEY_ALLOWED_METHODS = Settings::KEY_ALLOWED_METHODS;
48
49
    /** @see Settings */
50
    const KEY_ALLOWED_HEADERS = Settings::KEY_ALLOWED_HEADERS;
51
52
    /** @see Settings */
53
    const KEY_EXPOSED_HEADERS = Settings::KEY_EXPOSED_HEADERS;
54
55
    /** @see Settings */
56
    const KEY_IS_USING_CREDENTIALS = Settings::KEY_IS_USING_CREDENTIALS;
57
58
    /** @see Settings */
59
    const KEY_FLIGHT_CACHE_MAX_AGE = Settings::KEY_FLIGHT_CACHE_MAX_AGE;
60
61
    /** @see Settings */
62
    const KEY_IS_FORCE_ADD_METHODS = Settings::KEY_IS_FORCE_ADD_METHODS;
63
64
    /** @see Settings */
65
    const KEY_IS_FORCE_ADD_HEADERS = Settings::KEY_IS_FORCE_ADD_HEADERS;
66
67
    /** @see Settings */
68
    const KEY_IS_CHECK_HOST = Settings::KEY_IS_CHECK_HOST;
69
70
    /** Settings key */
71
    const KEY_LOG_IS_ENABLED = self::KEY_IS_CHECK_HOST + 10;
72
73
    /** Settings key */
74
    protected const KEY_LAST = self::KEY_LOG_IS_ENABLED;
75
76
    /**
77
     * @var array
78
     */
79
    private $appConfig;
80
81
    /**
82
     * @inheritdoc
83
     */
84 1
    final public function get(array $appConfig): array
85
    {
86 1
        $this->appConfig = $appConfig;
87
88 1
        return $this->getSettings();
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    protected function getSettings(): array
95
    {
96
        // Settings do not provide any public methods to get default settings so we use this trick
97
        $defaults = (new class extends Settings
98
        {
99
            /**
100
             * @return array
101
             */
102 1
            public function getHiddenDefaults(): array
103
            {
104 1
                return $this->getDefaultSettings();
105
            }
106
        })->getHiddenDefaults();
107
108 1
        $appConfig = $this->getAppConfig();
109
110 1
        $defaults[static::KEY_LOG_IS_ENABLED] = (bool)($appConfig[A::KEY_IS_LOG_ENABLED] ?? false);
111
112 1
        if (array_key_exists(A::KEY_APP_ORIGIN_SCHEME, $appConfig) === true &&
113 1
            array_key_exists(A::KEY_APP_ORIGIN_HOST, $appConfig) === true &&
114 1
            array_key_exists(A::KEY_APP_ORIGIN_PORT, $appConfig) === true
115
        ) {
116
            /**
117
             * Array should be in parse_url() result format.
118
             *
119
             * @see http://php.net/manual/function.parse-url.php
120
             */
121 1
            $defaults[static::KEY_SERVER_ORIGIN] = [
122 1
                static::KEY_SERVER_ORIGIN_SCHEME => (string)$appConfig[A::KEY_APP_ORIGIN_SCHEME],
123 1
                static::KEY_SERVER_ORIGIN_HOST   => (string)$appConfig[A::KEY_APP_ORIGIN_HOST],
124 1
                static::KEY_SERVER_ORIGIN_PORT   => (string)$appConfig[A::KEY_APP_ORIGIN_PORT],
125
            ];
126
        }
127
128 1
        return $defaults;
129
    }
130
131
    /**
132
     * @return mixed
133
     */
134 1
    protected function getAppConfig()
135
    {
136 1
        return $this->appConfig;
137
    }
138
}
139