Configuration   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getPreface() 0 4 1
A setPreface() 0 9 2
A addSection() 0 4 1
A getSections() 0 4 1
A getGlobalSection() 0 4 1
A getDefaultsSection() 0 4 1
A getFrontendSections() 0 4 1
A getBackendSections() 0 4 1
A getListenSections() 0 4 1
A getSingleSection() 0 10 3
A getMultipleSections() 0 12 3
1
<?php
2
3
namespace Jalle19\HaPHProxy;
4
5
use Jalle19\HaPHProxy\Section\AbstractSection;
6
use Jalle19\HaPHProxy\Section\BackendSection;
7
use Jalle19\HaPHProxy\Section\DefaultsSection;
8
use Jalle19\HaPHProxy\Section\FrontendSection;
9
use Jalle19\HaPHProxy\Section\GlobalSection;
10
use Jalle19\HaPHProxy\Section\ListenSection;
11
use Jalle19\HaPHProxy\Section\Sections;
12
13
/**
14
 * Class Configuration
15
 * @package Jalle19\HaPHProxy
16
 * @author  Sam Stenvall <[email protected]>
17
 * @license GNU General Public License 2.0+
18
 */
19
class Configuration
20
{
21
22
	const DEFAULT_PREFACE = '# Generated with Jalle19/haphproxy';
23
24
	/**
25
	 * @var string
26
	 */
27
	private $preface = self::DEFAULT_PREFACE;
28
29
	/**
30
	 * @var AbstractSection[]
31
	 */
32
	private $sections = [];
33
34
35
	/**
36
	 * @return string
37
	 */
38
	public function getPreface()
39
	{
40
		return $this->preface;
41
	}
42
43
44
	/**
45
	 * Sets the preface. If the preface is empty after being trimmed, it won't be set.
46
	 *
47
	 * @param string $preface
48
	 */
49
	public function setPreface($preface)
50
	{
51
		// Don't override the default preface with an empty one
52
		$preface = trim($preface);
53
54
		if (!empty($preface)) {
55
			$this->preface = trim($preface);
56
		}
57
	}
58
59
60
	/**
61
	 * @param AbstractSection $section
62
	 */
63
	public function addSection(AbstractSection $section)
64
	{
65
		$this->sections[] = $section;
66
	}
67
68
69
	/**
70
	 * @return AbstractSection[]
71
	 */
72
	public function getSections()
73
	{
74
		return $this->sections;
75
	}
76
77
78
	/**
79
	 * @return GlobalSection|null
80
	 */
81
	public function getGlobalSection()
82
	{
83
		return $this->getSingleSection(Sections::SECTION_GLOBAL);
84
	}
85
86
87
	/**
88
	 * @return DefaultsSection|null
89
	 */
90
	public function getDefaultsSection()
91
	{
92
		return $this->getSingleSection(Sections::SECTION_DEFAULTS);
93
	}
94
95
96
	/**
97
	 * @return FrontendSection[]
98
	 */
99
	public function getFrontendSections()
100
	{
101
		return $this->getMultipleSections(Sections::SECTION_FRONTEND);
102
	}
103
104
105
	/**
106
	 * @return BackendSection[]
107
	 */
108
	public function getBackendSections()
109
	{
110
		return $this->getMultipleSections(Sections::SECTION_BACKEND);
111
	}
112
113
114
	/**
115
	 * @return ListenSection[]
116
	 */
117
	public function getListenSections()
118
	{
119
		return $this->getMultipleSections(Sections::SECTION_LISTEN);
120
	}
121
122
123
	/**
124
	 * @param string $type
125
	 *
126
	 * @return AbstractSection|null
127
	 */
128
	private function getSingleSection($type)
129
	{
130
		foreach ($this->sections as $section) {
131
			if ($section->getType() === $type) {
132
				return $section;
133
			}
134
		}
135
136
		return null;
137
	}
138
139
140
	/**
141
	 * @param string $type
142
	 *
143
	 * @return AbstractSection[]
144
	 */
145
	private function getMultipleSections($type)
146
	{
147
		$sections = [];
148
149
		foreach ($this->sections as $section) {
150
			if ($section->getType() === $type) {
151
				$sections[] = $section;
152
			}
153
		}
154
155
		return $sections;
156
	}
157
158
}
159