Completed
Push — master ( 6167e8...c23e1b )
by Sam
02:58
created

Configuration::getGlobalSection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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. The specified preface will be trimmed.
46
	 *
47
	 * @param string $preface
48
	 */
49
	public function setPreface($preface)
50
	{
51
		$this->preface = trim($preface);
52
	}
53
54
55
	/**
56
	 * @param AbstractSection $section
57
	 */
58
	public function addSection(AbstractSection $section)
59
	{
60
		$this->sections[] = $section;
61
	}
62
63
64
	/**
65
	 * @return AbstractSection[]
66
	 */
67
	public function getSections()
68
	{
69
		return $this->sections;
70
	}
71
72
73
	/**
74
	 * @return GlobalSection|null
75
	 */
76
	public function getGlobalSection()
77
	{
78
		return $this->getSingleSection(Sections::SECTION_GLOBAL);
79
	}
80
81
82
	/**
83
	 * @return DefaultsSection|null
84
	 */
85
	public function getDefaultsSection()
86
	{
87
		return $this->getSingleSection(Sections::SECTION_DEFAULTS);
88
	}
89
90
91
	/**
92
	 * @return FrontendSection[]
93
	 */
94
	public function getFrontendSections()
95
	{
96
		return $this->getMultipleSections(Sections::SECTION_FRONTEND);
97
	}
98
99
100
	/**
101
	 * @return BackendSection[]
102
	 */
103
	public function getBackendSections()
104
	{
105
		return $this->getMultipleSections(Sections::SECTION_BACKEND);
106
	}
107
108
109
	/**
110
	 * @return ListenSection[]
111
	 */
112
	public function getListenSections()
113
	{
114
		return $this->getMultipleSections(Sections::SECTION_LISTEN);
115
	}
116
117
118
	/**
119
	 * @param string $type
120
	 *
121
	 * @return AbstractSection|null
122
	 */
123
	private function getSingleSection($type)
124
	{
125
		foreach ($this->sections as $section) {
126
			if ($section->getType() === $type) {
127
				return $section;
128
			}
129
		}
130
131
		return null;
132
	}
133
134
135
	/**
136
	 * @param string $type
137
	 *
138
	 * @return AbstractSection[]
139
	 */
140
	private function getMultipleSections($type)
141
	{
142
		$sections = [];
143
144
		foreach ($this->sections as $section) {
145
			if ($section->getType() === $type) {
146
				$sections[] = $section;
147
			}
148
		}
149
150
		return $sections;
151
	}
152
153
}
154