Settings   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 103
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isLogEnabled() 0 4 1
A enableLog() 0 6 1
A disableLog() 0 6 1
A getData() 0 11 1
A setData() 0 9 2
1
<?php declare(strict_types = 1);
2
3
namespace Neomerx\CorsIlluminate\Settings;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Neomerx\Cors\Strategies\Settings as CorsSettings;
22
23
/**
24
 * @package Neomerx\CorsIlluminate
25
 */
26
class Settings extends CorsSettings
27
{
28
    /** @var int Settings key */
29
    public const KEY_SERVER_ORIGIN = 0;
30
31
    /** @var int Settings key */
32
    public const KEY_ALLOWED_ORIGINS = self::KEY_SERVER_ORIGIN + 1;
33
34
    /** @var int Settings key */
35
    public const KEY_ALLOWED_METHODS = self::KEY_ALLOWED_ORIGINS + 1;
36 1
37
    /** @var int Settings key */
38 1
    public const KEY_ALLOWED_HEADERS = self::KEY_ALLOWED_METHODS + 1;
39 1
40
    /** @var int Settings key */
41
    public const KEY_EXPOSED_HEADERS = self::KEY_ALLOWED_HEADERS + 1;
42
43
    /** @var int Settings key */
44
    public const KEY_IS_USING_CREDENTIALS = self::KEY_EXPOSED_HEADERS + 1;
45
46
    /** @var int Settings key */
47 1
    public const KEY_FLIGHT_CACHE_MAX_AGE = self::KEY_IS_USING_CREDENTIALS + 1;
48
49 1
    /** @var int Settings key */
50
    public const KEY_IS_FORCE_ADD_METHODS = self::KEY_FLIGHT_CACHE_MAX_AGE + 1;
51 1
52
    /** @var int Settings key */
53
    public const KEY_IS_FORCE_ADD_HEADERS = self::KEY_IS_FORCE_ADD_METHODS + 1;
54
55
    /** @var int Settings key */
56
    public const KEY_IS_CHECK_HOST = self::KEY_IS_FORCE_ADD_HEADERS + 1;
57
58
    /** @var int Settings key */
59
    public const KEY_LOGS_ENABLED = self::KEY_IS_CHECK_HOST + 1;
60
61
    /** @var int Cached settings key */
62
    protected const CORS_ILLUMINATE_SETTINGS_CACHE_KEY_IS_LOG_ENABLED = 25; // more than the base class has properties.
63
64
    /** @var int Cached settings key */
65
    protected const CORS_ILLUMINATE_SETTINGS_CACHE_KEY_LAST = self::CORS_ILLUMINATE_SETTINGS_CACHE_KEY_IS_LOG_ENABLED;
66
67
    /** @return bool */
68
    private const DEFAULT_IS_LOG_ENABLED = false;
69
70
    /** @var bool If logging is enabled */
71
    private $isLogEnabled = self::DEFAULT_IS_LOG_ENABLED;
72
73
    /**
74
     * @return bool
75
     */
76
    public function isLogEnabled(): bool
77
    {
78
        return $this->isLogEnabled;
79
    }
80
81
    /**
82
     * @return self
83
     */
84
    public function enableLog(): self
85
    {
86
        $this->isLogEnabled = true;
87
88
        return $this;
89
    }
90
91
    /**
92
     * @return self
93
     */
94
    public function disableLog(): self
95
    {
96
        $this->isLogEnabled = false;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    public function getData(): array
105
    {
106
        $data = parent::getData();
107
108
        // check we won't override any parent properties
109
        \assert(\array_key_exists(static::CORS_ILLUMINATE_SETTINGS_CACHE_KEY_IS_LOG_ENABLED, $data) === false);
110
111
        $data[static::CORS_ILLUMINATE_SETTINGS_CACHE_KEY_IS_LOG_ENABLED] = $this->isLogEnabled();
112
113
        return $data;
114
    }
115
116
    /**
117
     * @inheritDoc
118
     */
119
    public function setData(array $data): CorsSettings
120
    {
121
        $isLogEnabled = \boolval(
122
            $data[static::CORS_ILLUMINATE_SETTINGS_CACHE_KEY_IS_LOG_ENABLED] ?? static::DEFAULT_IS_LOG_ENABLED
123
        );
124
        $isLogEnabled === true ? $this->enableLog() : $this->disableLog();
125
126
        return parent::setData($data);
127
    }
128
}
129